linux - How to have a complicated two way sync using rsync preserving deletions

04
2013-09
  • Goje87

    I have gone thorough similar questions on stackoverflow but they dont cover the situation that I would like to ask here.

    I have all my music files on my remote machine. I would like to sync them to my local devices. Lets say that I want to sync them over my two laptops. Initially both the laptops have none of the music files. I run a simple rsync on both of them and get all the music files.

      Laptop 1 [a.mp3,b.wav,c.mp4] {-------\
                                            \----|
                                                 | Server [a.mp3,b.wav,c.mp4]
                                            /----|
      Laptop 2 [a.mp3,b.wav,c.mp4] {-------/
    

    Now I would like to maintain following rules during syncing.

    • If I delete a file on any of the laptops, the files should not get deleted on the server.
    • If I delete a file on any of the laptops, it should not get added back just because its not deleted on server.
    • If I delete a file on any of the laptops, the files should not get deleted on the other laptops. The deletion of a file is specific only to the laptop on which I deleted.
    • If I add a new file on any of the laptops, Server should get the new file. And eventually every other laptop should get this new file.
    • If I delete a file on server, it should get deleted on all the laptops.

    How can I achieve the above rules with rsync? I am ready to execute more than one command on all the devices/laptops to have syncing following above rules.

    Do let me know if I have not made any point clear. I will edit the question to make it more clear.

  • Answers
  • prodigitalson

    Youre going to have to manually keep track of an exclusion on the laptops and then run the commands only from the laptops.

    So if on laptop1 you delete a.mp3 you would add the to a file we'll call it ~/.music_exclude

    # this is the file .music_exclude on laptop1
    a.mp3
    

    So then to sync any added songs TO the server except those in .music_exclude:

    laptop1$ rsync -azC --exclude-from=~/.music_exclude path/to/music/ user@server:path/to/music/
    

    To get all new music FROM the server, and process any deletions:

    laptop1$ rsync -azC --exclude-from=~/.music_exclude --delete user@server:path/to/music/ path/to/music/
    

  • Related Question

    Rsync, two-way sync, and syncing file-deletion operations
  • David Zureick-Brown

    I use rsync in the following manner:

    rsync -Pvaz [email protected]*.***:Documents/ /Users/me/Documents/
    rsync -Pvaz /Users/me/Documents/ [email protected]*.***:Documents/ 
    

    The problem: if I delete a file on my home computer and sync, it doesn't get deleted on the remote computer. Is there a way to use rsync to literally sync two folders?


  • Related Answers
  • Richard Hoskins

    Man page for rsync:

     --delete                delete extraneous files from dest dirs
     --delete-before         receiver deletes before transfer (default)
     --delete-during         receiver deletes during xfer, not before
     --delete-delay          find deletions during, delete after
     --delete-after          receiver deletes after transfer, not before
     --delete-excluded       also delete excluded files from dest dirs
    
  • William Hilsum

    I am not a rsync expert and there may be a switch that can do what you want and someone else may say.

    However, personally, I use rsync when I need one way copying. When it comes to synchronise two locations, I use Unison which is built on top of Rsync and basically makes it easier.