rsync to copy only particular folders - linux

I have a dir with files and dirs in it. What I want is to make rsync copy of several folders from that dir. For example, lets say I have this:
/home/user
-- drwxr-xr-x folderA
-- drwxr-xr-x folderB
-- drwxr-xr-x folderC
-- -rw-r--r-- file.1
-- -rw-r--r-- file.2
I want to copy folderA and folerB using rsync. I have created file rsync_folders.txt
+ /folderA/**
+ /folderB/**
My problem is that rsync will always copy file unless it matches exclude pattern. But if I add
- /**
nothing is copied because rsync first matches against exclude patterns.
Any ideas?
Note: I cannot list all folders and files I want to exclude. It will be changing from time to time.

Either use rsync -av src1 src2 src3 ... dst or put all the folders you want to rsync in a text file (each folder in a separate line) and use rsync -arv --files-from=sources.txt dst.
Note that by default -a implies --recursive but not when --files-from is used, so in this case -r must be specified explicitly.

Related

Rsync - How to ignore for one of same name folder

I have two folders
frontend/static/
static/
I want to rsync first folder but want to ignore second folder
rsync -av --delete --exclude-from 'rsync_exclude.txt'
I write in rsyng_exclude.txt
static
igonore both
or
./static/
ignore neither.
Is there any method to ignore only static/ and rsync frontend/static/

rsync files (name starting with string* of a list) from multiple directories in server to local client

I have a list.txt of names/ids
13089
13090
13091...
for which I want to find and rsync/copy over the files starting with these names from several remote directories. The directories may or may not contain these files.
Right now I am using the following command...but it is not finding the files even when it does exist. Also is it correct to repeat the rsync command for each possible directory?
for ID_NAME in $(cat ./list_ids.csv)
do
rsync -PEav --rsh=ssh admin#123:'/folder1/subfolder1/${ID_NAME}**' /destinationfolder1
rsync -PEav --rsh=ssh admin#123:'/folder2/subfolder2/${ID_NAME}**' /destinationfolder2
rsync -PEav --rsh=ssh admin#123:'/folder3/subfolder3/${ID_NAME}**' /destinationfolder3
done

Linux - Copy the files (not subfolders) from origin to destination while overwriting/deleting the initial contents in the destination folder

I need to run a command in Linux where I would copy the files (not folders) in ~/folder1/subfolder1 to ~/folder2/subfolder2 while deleting the initial contents/files in folder2?
Command cp copies files from one folder to another:
cp ~/folder1/* ~/folder2/
But, how can I also delete files that were initially in the folder2, while copying only files from folder1?
Also, is there a rsync command instead of cp that would only copy files and not subfolders?
I have tried with this:
rsync --delete-during folder1/* folder2/
But, I got an error:
rsync: --delete does not work without -r or -d.
And I don't want to use -r or -d flag since that would mean the subfolders would get copied as well, and I only want to copy files.
You want to delete everything in folder2, then copy every file from folder1 ?
Try something like this:
rm folder2/* && cp folder1/* folder2/
(cp will not copy directories by default)

How to rsync all folders present on the destination folder

I have two folders FolderA and FolderB as below. I want to rsync the common subfolders. For example, I can do rsync -avzP /path/to/FolderB/* /path/to/FolderA/, which will keep SubFolder1 and SubFolder3 mirrored. My question is how I can achieve the same if FolderB is the destination without explicitly --include or --exclude individual subfolders (e.g., in case there are too many of them).
FolderA
|--SubFolder1
|--SubFolder2
|--SubFolder3
|--SubFolder4
FolderB
|--SubFolder1
|--SubFolder3
You can update all files in FolderA using the contents FolderB as the source with your normal:
rsync -uav /path/to/FolderB/ /path/to/FolderA
(note: the trailing '/' after FolderB/ is mandatory to copy the contents of FolderB rather than FolderB itself)
To do it in reverse and update FolderB from FolderA and not copy SubFolder2 and SubFolder4 with the --existing option which will "skip creating new files on receiver", but that will also prevent new files and directories within SubFolder1 and SubFolder3 from being created as well.
You best option to not copy SubFolder2 and SubFolder4 while allowing new files and directories within SubFolder1 and SubFolder3 be created is to use the --filter option. See rsync(1) - Linux manual page.
A typical way to use --filter to exclude SubFolder2 and SubFolder4 on a copy from FolderA to FolderB would be:
rsync -uav --filter -_SubFolder2/ --filter -_SubFolder4/ /path/to/FolderA/ /path/to/FolderB
That will allow you to copy the complete contents of /path/to/FolderA/ to /path/to/FolderB/ without including SubFolder2 and SubFolder4.
Edit Per-Comment On Large Number of SubFolders
If you have a large number of folders under FolderA that you do not want to sync under FolderB, then your other option is to create a text file holding the absolute path to only those SubFolderX under FolderA you want to rsync to FolderB and then use the --no-R and --files-from=folderlist options to only rsync the wanted SubFolders. This will eliminate having to specify a large number of --filter options on the command line.
For example, you can create your folderlist with:
find /path/to/FolderA -maxdepth 1 -type d > folderlist
(note: specify the absolute path above and find will produce the folderlist file containing absolute paths)
Now edit your folderlist file and remove the parent directory (e.g. /path/to/FolderA) and any SubFolders you don't want to sync under FolderB. You can now use the folderlist file to control which SubFolders under FolderA are sync'ed to FolderB without having to include a long list of filters on the command line. Your command line then becomes
rsync -uai -r --no-R --files-from=folderlist / /path/to/FolderB
(note: the '/' as source serves as a base for the paths contained in folderlist. You can change the -i option to control the level of information dumped to the screen, e.g. -v, etc... or remove it altogether to suppress any reporting other than errors)
(also note: when using --files-from, -a does not imply -r (recursive), so you will need to explicitly add -r if you need a recursive transfer)

Slashes and the rsync command

I am trying to do something along the same lines as what is asked in this question: How do I synchronize in both directions?
However, what I can't figure out is whether or not I should add slashes to the end of the file path. Basically I'm trying to create an alias command that syncs the contents of two directories that go by the same name but are on two different servers. What I don't want is for one directory to be copied into the other (which I am aware is a possibility depending on how the slashes at the end are done).
What I have currently is:
alias syncDirectories1 = 'rsync -tvur name#host:/Users/me/directory/ /Users/me/directory/'
alias syncDirectories2 = 'rsync -tvur /Users/me/directory/ name#host:/Users/me/directory/'
For what I am trying to accomplish, should there be slashes at the end of both file paths?
It's described in the rsync(1) manpage:
A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a
trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the
attributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following commands copies the files in the same way, including their setting of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo
As to the destination, I don't think it has any major consequences. There is a difference if the source is a file and destination doesn't exist — this will make a copy of SRC called DEST:
rsync SRC DEST
, whereas, this will create directory DEST and copy the SRC file into it:
rsync SRC DEST/
I tested it with rsync 3.1.3 on Arch Linux, the results are below:
1. rsync -avPzu test login#remote:/home/login/test "test" directory is copied inside of existing "test" on remote (structure is then test/test/...)
2. rsync -avPzu test login#remote:/home/login/test/ same as above
3. rsync -avPzu test/ login#remote:/home/login/test content of "test" directory is synchronized with the remote "test" directory
4. rsync -avPzu test/ login#remote:/home/login/test/ same as above
5. rsync -avPzu test login#remote:/home/login/ same as above
6. rsync -avPzu test login#remote:/home/login same as above
The methods 3-6 are the correct ones in this case, contrary to the accepted answer.
keep the slashes on the source, and remove them from the destination.
Like this:
alias syncDirectories1 = 'rsync -tvur name#host:/Users/me/directory/ /Users/me/directory'
alias syncDirectories2 = 'rsync -tvur /Users/me/directory/ name#host:/Users/me/directory'
I had different results from accepted answer on Ubuntu 17.04. It seems that the destination / does not have an effect.
I did the following four commands:
rsync -av src nslash # No slashes on either src or dest
rsync -av src dslash/ # Slash on Dest only
rsync -av src/ sslash # Slash on src only
rsync -av src/ sdslash/ # Slash on both src and dest
Conclusions:
once there is a / on the src, then the contents of the src only will be copied over to the dest, regardless of whether the dest has a slash or not.
no slash on the src, then the src and its content will be copied to dest.
Slashes on dest has no effect.

Resources