31

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?

2 Answers 2

35

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.

3
  • Thanks @Richard. Your answer is making me think that my original question may have been easier than I thought. If I use the single line: rsync -av --exclude=dont_delete_me --delete /sending/path /receiving/path, wouldn't that make rsync delete everything on the receiving path that does not exist on the sending path, except things matched by dont_delete_me? Most importantly, would rsync keep things on the receiving side matched by dont_delete_me even if nothing on the sending side matches dont_delete_me? If that's the case, I think that's the only line I need, right? Commented Aug 11, 2011 at 16:59
  • You are correct but you will also not get new files and changes to the dont_delete_me folder if you do it in one line. If you don't want to sync changes and additions, then you can just use the --exclude flag as you described here. Commented Aug 11, 2011 at 21:46
  • @AmV Excluded files are not deleted by --delete, unless you explicitly specify --delete-excluded. See the description of --delete and --delete-excluded in the rsync manual. Commented Aug 11, 2011 at 21:56
4

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.
  • (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".

1
  • There is a slight misconception in your understanding of --exclude-from=XXX. It does not exclude files in the source (from), it simply excludes the files provided in the XXX file, whether from creation, modification or deletion. You can include multiple files though, from which the exclusions are provided.
    – Martin
    Commented Aug 2, 2023 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .