bi-directional sync with rsync
rsync
is the wrong tool for this task, for exactly the reasons that you have encountered. Instead, consider using unison
:
unison A/ B/
The first time you run this it will identify files that are uniquely in A
, and those that are uniquely in B
. It will also flag those that are in both places and ask you to identify which is to be overwritten.
The next time you run this it will copy changes from A
to B
and also B
to A
, flagging any files that have been changed in both places for manual resolution.
mkdir A B
date > A/date
who > B/who
unison A/ B/
# Lots of output from unison, showing synchronisation
ls A
date who
ls B
date who
date > A/date
unison A/ B/
# Lots of output from unison, showing synchronisation
There are a number of useful flags available for unison
which help automate the process by defining assumptions and thereby reducing the number of questions you're asked during the synchronisation.
You could try osync which is designed for exactly this task. I once set up a complex sequence of rsync commands to do the job, but I now use osync.
https://github.com/deajan/osync
It uses rsync internally, so it should be suitable for any situation where you could use rsync.