File Synchronization Between Two Ubuntu Servers using Unison

Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (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.

This tutorial shows how to set up file synchronization between two Ubuntu 12.04 servers with Unison that are on the same network but you can use the same approach on the servers that are not placed on the same network.

Before installing the unison,generate the ssh key pair on PrimarySrv and copy the public key to the SecondarySrv:

scp ~/.ssh/ arbab@

Connect to the SecondarySrv, append the public key to authorized_keys:

cat >> ~/.ssh/authorized_keys

We got an error saying “~/.ssh/authorized_keys: No such file or directory” it means that there is no .ssh directory for this user (this user has never used ssh before). Simply create an empty .ssh directory with 700 permissions and try to append the public key to authorized_keys once again:

Now,try to connect to SecondarySrv from PrimarySrv, and this time, it will not ask for the password :-)

ssh arbab@

Now, issue this command for the installation of unison on both server:

sudo apt-get install unison

Use this command to verify that the local unison client can start and connect to the remote server(In this case,remote server’s ip address is

unison -testServer /var/www/ ssh://

Now, try to sync with the remote server with the -batch (batch mode) option ask no questions at all:

unison -batch /var/www/ ssh://

Create a sample file inside the /var/www/ directory with name “server1” on PrimarySrv and run the unison again:

cd /var/www/
sudo touch server1
unison -batch /var/www/ ssh://

Verify the /var/www/ directory on SecondarySrv:

For two-way synchronization verification, create a file on SecondarySrv with the name “server2” inside the /var/www/ directory:

sudo touch server2

Run unison again on PrimarySrv for sync:

unison -batch /var/www/ ssh://

Verify the synchronization status on PrimarySrv:

Edit the crontab file to make the sychronization process automatically:

crontab -e

Make the unison to run every 30 min automatically:

*/30 * * * * unison -batch /var/www/ ssh://

How To Call unison On Demand?

For this, we need to install the incron(inotify cron daemon) which monitors filesystem events (e.g. add a new file, delete a file etc) and executes the command or scripts.It’s usage is generally similar to cron.

Type the following command for incron installation:

sudo apt-get install incron

Configure the /etc/incron.allow file in order to allow user to use incron:

sudo nano /etc/incron.allow

In this case the user is “arbab“(you can change it with your desired user):

Now edit the incrontab:

incrontab -e

Run unison command automatically when file created,deleted or modified from /var/www directory:

/var/www IN_CREATE,IN_DELETE,IN_CLOSE_WRITE unison -batch /var/www/ ssh://

Create a sample test file inside the /var/www/ directory with name “incron” on PrimarySrv:

cd /var/www/

sudo touch incron

Verify the synchronization status on SecondarySrv:

Hope this will help you!

Please Remember me in your prayers!

Enjoy 🙂

4 responses to “File Synchronization Between Two Ubuntu Servers using Unison

  1. Pingback: Simple failover cluster using UCARP on Ubuntu « Lazy Geek -:)

  2. Pingback: Network Sync | Jan Söhlke

  3. Pingback: Simple failover cluster using UCARP on Ubuntu

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: