My src folder is my local laptop.
I have a dest folder which I use for backup (and is NAS mounted) but for historical reasons I have sometimes used the dest folder for exactly what I use now src (bear with me).
The result is that I have for example
src/docs/2021/january.pdfwhile
dest/docs/2021/february.pdfI just happen to run rsync -av src/ dest, and the result now is
src/docs/2021/january.pdf
src/docs/2021/february.pdf
dest/docs/2021/february.pdfWhat I'd like to achieve though is that the 2 are completely synced, so that both have the january and february files (in other words that the dest files are also synced to src).
How do I do that?
22 Answers
You have to do two rsyncs, one in each direction.
1You can't use rsync for this case unless you can guarantee never to delete any files.
Consider two trees, A and B.
- Copy all the files from A to B (synchronisation). We can't delete anything in
Bbecause it might be a file that needs to be copied toA. - Copy all the files form B to A (synchronisation the other way). We still can't delete anything in
Abecause it might have been created just now and needs to be copied back toB.
Now A and B are synchronised, with a copy of everything that was in A now in B, and everything that was in B now in A.
Let's assume you want to delete a file from A. Now repeat the synchronisation and notice that the deleted file has reappeared in A. This is because it was still present in B and got copied across as if it were a new file.
One solution to this is to use a different tool. I've had good results with unison (also ), which was created as a bi-directional synchronisation tool. (There are some other tools around but I can't remember them offhand.)
Synchronise two trees A and B as simply as with this command
unison A BThere are options available to determine how to act when the same file has been updated on both sides, etc.
3