rsync options so that source and dest are exactly the same?

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

while

dest/docs/2021/february.pdf

I 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.pdf

What 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?

2

2 Answers

You have to do two rsyncs, one in each direction.

1

You 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 B because it might be a file that needs to be copied to A.
  • Copy all the files form B to A (synchronisation the other way). We still can't delete anything in A because it might have been created just now and needs to be copied back to B.

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 B

There are options available to determine how to act when the same file has been updated on both sides, etc.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like