Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

If you use --delete and --exclude together what is in the excluded location wont get deleted even if the source files are removed.

But that raises the issue that the folder wont 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.

share|improve this answer
    
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? –  user815423426 Aug 11 '11 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. –  Richard Holloway Aug 11 '11 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. –  Gilles Aug 11 '11 at 21:56

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.)

Explaination
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.

Hope this helps.

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 don't have enough "reputation" to add this as a comment to the other post)

I am using "rsync version 3.0.9 protocol version 30".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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