Asking rsync to delete files on the receiving side that don't exist on the sending side, with exceptions on the receiving side
If you use --delete
and --exclude
together what is in the excluded location won't get deleted even if the source files are removed.
But that raises the issue that the folder won't be rsync
'd at all. So you will need another rsync
job to sync
that folder.
Eg.
rsync -nav /home/richardjh/keepall/ /home/backup/richardjh/keepall/
rsync -nav --exclude=keepall --delete /home/richardjh /home/backup/richardjh
You could run these the other way around, but then it would delete all removed files and then replace them, which is not as efficient.
You can't do it as a one liner.
I found that the following solution works for my purposes:
rsync -r --exclude-from=do_not_send_to_dest --delete --exclude-from=do_not_modify_in_dest src/ dest
(Note: Add the -n
option to do a dry-run, and do not change --delete
to --delete-excluded
!)
Running the command causes the following properties:
- The source
src
directory remains unmodified The destination
dest
folder matches the source folder except that:- items on the
do_not_send_to_dest
list are not sent to the destination, and - items on the
do_not_modify_in_dest
list are left unmodified in the destination.
- items on the
- (Note: It's no problem for non-existent items to be listed in either of the "
do_not_
" files.)
Explanation
I guess as rsync reads the command line arguments from left to right, the --delete
argument seems to cause rsync to internally "switch modes" (in a sense), allowing the second use of the --exclude-from
option again but with a different meaning.
Caveat:
If an item on the
do_not_send_to_dest
list already exists in the destination, then:- this command doesn't remove that item from the destination (unless the item is first removed from the source directory)
- Caution: changing
--delete
to--delete-excluded
will delete everything in the destination that is listed on either of your two lists. xD (Perhaps rsync was writen to be open / flexible, but has some with nuances?)
Lightly usage tested.
I am using "rsync version 3.0.9 protocol version 30
".