Rsync: How to Delete Files on Receiving Side with Exceptions

rsync

I have read these threads:

But, as far as I can tell (maybe I am missing something), they don't cover the following question:

How do you ask rsync to copy files and delete those on the receiving side that do not exist on the sending side, with exceptions? (e.g. don't remove a mercurial repository .hg on the receiving side, even if there is no repository on the sending side).

One possibility?

Borrowing from @Richard Holloway's answer below. Say I have the following line:

rsync -av --exclude=dont_delete_me --delete /sending/path /receiving/path

As far as I understand, this line would make rsync delete everything on the receiving path that does not exist on the sending path, except those things matched by dont_delete_me. My question now is: Would rsync keep files on the receiving side that are matched by dont_delete_me even if nothing on the sending side matches dont_delete_me?

Best Answer

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.

Related Question