0

I try to prepare the rsync command that will: 1. backup all files in the directory but not nested files 2. backup certain directories 3. delete files in the target directory when they are deleted in source

rsync -vtr --delete --exclude='*/' --include='includes/*' --include='js/*' --include='css/*' /home/gameboy/abc/* /home/gameboy/PartialMirror/abc/

But this only copies files and doesn't copy any folders and doesn't do deleting. Could you help me to improve the command?

1

Strictly speaking, the following can be interpreted to be contradictory goals.

  1. backup all files in the directory but not nested files i.e. --exclude='*/'
  2. backup certain directories - i.e. include some nested files

Please see below for an explanation on why your arguments did not work as you intended them. Further below, see a suitable alternative that might suit your purpose (it satisfies #1, #3 and a version of #2).

Why your command did not work:

The way rsync processes your commandline arguments, the following arguments are equivalent to saying --exclude='*/'

--exclude='/' --include='desired_subfolder/'

i.e., your command excluded all nested files, thus excluding files in desired subfolders as well.

This problem affects your second observation, namely that files in the target that are deleted in the source are not being deleted. Actually, your command works perfectly well in deleting such files at the top level. However, files one level below are not deleted since they are already excluded. It helps to remember that --delete refers to --delete-during, not --delete-before.

Solutions that satisfies #1, #3 and version of #2: The following solution might suit your needs

rsync -avz --delete --include='includes/*' --include='js/*' --include='css/*' --exclude='*/*' /home/gameboy/abc/ /home/gameboy/PartialMirror/abc/

It satisfies the following: 1. Backs up all files and directories at depth 1, but not nested files. 2. Back up desired folders 3. Deletes files in destination that are not in source.

Another more cumbersome choice is as follows

  rsync -vlptgoDd --delete src/ dest/

This satisfies #1 and #3, but avoids all nested files (remember that folders are also files in *nix). I avoided giving the -r option, so it is not recursive. Delete works and folders at depth 1 are copied. In addition to preserving timestamps (-t), I included the other typical options for archive copying.

A more standard way to get different rsync behaviors in different folders is to sprinkle filter rules in a per-directory fashion through the hierarchy; i.e. create .rsync-filter files specific to each folder that demands a unique behavior. For example

   rsync -avF src/ dest/
  • I used the first solution first. I added double asterisks to get nesting for subdirectories and -prune-empty-dirs option to get rid of subdirectoies not being included. It worked. Although finally I used the .rsync-filter solution that seemed most convenient since I added the command to crontab. – trzczy Apr 25 '17 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.