Linux – Sync directory across two servers using rsync (or alternative for syncing directories)

copylinuxreplicationrsync

I have two VMs that should have identical files in this directory:

/var/www/owncloud/data/admin/files/

I initially thought that I could use rsync with cron to copy the files every two minutes:

On VM1:

*/2 * * * * rsync -auz --delete-after /var/www/owncloud/data/admin/files root@VM2:/var/www/owncloud/data/admin

On VM2

*/2 * * * * rsync -auz --delete-after /var/www/owncloud/data/admin/files root@VM1:/var/www/owncloud/data/admin

The problem I'm running into is if I upload a file to VM1, after two minutes, the cronjob on VM2 overwrites the directory on VM1, deleting the file.

The reason I am copy from VM1->VM2 and VM2->VM1 is because I have a very basic failover set up. If VM1 goes down and files are added/removed from VM2, I want those changes duplicated on VM1; and of VM1 is up and files are added/removed, I want those changes duplicated on VM2.

Is this possible to do with rsync? Is my command wrong? Is there an alternative I should use instead?

Best Answer

Have a look at unison, it allows for bi directional sync: http://www.cis.upenn.edu/~bcpierce/unison/