Recently I have implemented two-way files synchronization between two Ubuntu Cloud servers using unison and inotifywait. [/source]
Thought I would share it here as a blog for future references.
1. Setup: Two Rackspace Cloud server running Ubuntu or your own VPS server.
Unison is a file-synchronization tool for Unix and Windows. It allows to replicate a collection of files and directories between two servers (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other.
inotifywait is an Linux utility to notify file changes.
Now, lets use both of them
Install unison on both servers
$ apt-get install unison openssh-server ssh
Unison requires password less ssh-key authentication between servers. Lets set them.
Generate public/private key on server1.
$ ssh-keygen -t rsa
Copy the ssh keys and paste it into server2 authorized_keys which would be typically located at ~/.ssh/authorized_keys. (if not create it)
To make sure SSH working with password try login to server2 from server 1, it should connect without asking password.
$ ssh root@server2_ip_address
Install inotifywait on both servers
$ sudo apt-get install inotify-tools
Now unison and inotifywait are ready to use. Run the unison to listen for folder between two servers.
$ unison /var/www/proj1/images ssh://192.168.0.5//var/www/proje1/images/
Open server1 terminal and try to create some file inside /var/www/proj1/folder on server1. Unison will detect the file and prompt to user to enter.
To skip prompting use -batch option.
$ unison -batch /var/www/proj1/images ssh://192.168.0.101//var/www/proje1/images/
Once file synchronized with server2 unison will stop. To make unison to listen always for file changes create small shell script that you can run in background.
$ nano sync_file.sh
inotifywait -r $LOCATION
unison -batch $LOCATION ssh://
Run the shell script
$ sh sync_file.sh
Run the shell script in background(add & symbol at the end).
$ nohup sh sync_file.sh &
It should work fine if not please post your issue as a comment.