Syncing 2+ Ubuntu Servers With Unison

January 23, 2014    ubuntu linux networking sync servers server

When running a load-balanced site with 2 or more data servers, it’s important to keep them in sync as I have found out the hard way. Initially, I used rsync (the default sync tool in Ubuntu) but it turned out to be complicated and not the right fit. It was okay to have one of the servers as the master which would sync with the the slave but things got wonky when data was being written to the slave server and changes would not be reflected in the master. Even after setting up rsync on the slave server, things did not quite work out the way I would have liked.

In come Unison. It’s a popular sync software for multiple plateforms including Linux, OSX and Windows. It’s fully open-source and actually uses the rsync algorithm to make synchronising faster. I will be showing you how to install and set up Unison on two Ubuntu data servers to synchronise data between them.

First off, run the following commands to make sure all the package lists are up-to-date.
sudo apt-get update & apt-get upgrade

Install Unison on both servers by issuing the following command:
sudo apt-get install unison

If you don’t have SSH installed, install it along with Unison since we will be communicating between the 2 servers using SSH. sudo apt-get install unison openssh-server ssh

Next is to generate the SSH keys that the 2 machines will use to log in. sudo ssh-keygen -t rsa
Keeping hitting ENTER without typing anything when the key is being generated. Open the public key file in Server 1 and add the text contained in it to the authorized_keys file in Server 2. Repeat the step for Server 2 public key. Restart the SSH server to makes the changes take effect. sudo service ssh restart
Next run the command to test if the SSH connection is working as it should.
sudo ssh://
If all is well, you should be prompted with the login screen for Server 2.

Now that we have Unison installed, it’s time to tell Unision which folders to synchronise and without the need for human interaction.
sudo nano /root/.unison/default.prf

Copy the following text in to the file and save it by pressing Ctrl+X. The comments explain what each line is doing. You can modify it to your needs.

# Unison preferences file
# Root folders
root = /var/www
root = ssh://

# Ignore some files during sync
ignore = Name *.tmp    ## ignores files with extension .tmp

# When set to true, this flag causes the user interface to skip
# asking for confirmations on non-conflicting changes. (More
# precisely, when the user interface is done setting the
# propagation direction for one entry and is about to move to the
# next, it will skip over all non-conflicting entries and go
# directly to the next conflict.)

# When this is set to true, the user interface will ask no
# questions at all. Non-conflicting changes will be propagated;
# conflicts will be skipped.

# !When this is set to true, Unison will request an extra
# confirmation if it appears that the entire replica has been
# deleted, before propagating the change. If the batch flag is
# also set, synchronisation will be aborted. When the path
# preference is used, the same confirmation will be requested for
# top-level paths. (At the moment, this flag only affects the
# text user interface.) See also the mountpoint preference.

# When this preference is set to true, Unison will use the
# modification time and length of a file as a `pseudo inode
# number' when scanning replicas for updates, instead of reading
# the full contents of every file. Under Windows, this may cause
# Unison to miss propagating an update if the modification time
# and length of the file are both unchanged by the update.
# However, Unison will never overwrite such an update with a
# change from the other replica, since it always does a safe
# check for updates just before propagating a change. Thus, it is
# reasonable to use this switch under Windows most of the time
# and occasionally run Unison once with fastcheck set to false,
# if you are worried that Unison may have overlooked an update.
# The default value of the preference is auto, which causes
# Unison to use fast checking on Unix replicas (where it is safe)
# and slow checking on Windows replicas. For backward
# compatibility, yes, no, and default can be used in place of
# true, false, and auto. See the section "Fast Checking" for more
# information.

# When this flag is set to true, the group attributes of the
# files are synchronised. Whether the group names or the group
# identifiers are synchronised depends on the preference numerids.

# When this flag is set to true, the owner attributes of the
# files are synchronised. Whether the owner names or the owner
# identifiers are synchronised depends on the preference
# extttnumerids.

# Including the preference -prefer root causes Unison always to
# resolve conflicts in favor of root, rather than asking for
# guidance from the user. (The syntax of root is the same as for
# the root preference, plus the special values newer and older.)
# This preference is overridden by the preferpartial preference.
# This preference should be used only if you are sure you know
# what you are doing!

# When this preference is set to true, the textual user interface
# will print nothing at all, except in the case of errors.
# Setting silent to true automatically sets the batch preference
# to true.

#  When this flag is set to true, file modification times (but not
# directory modtimes) are propagated.

We will need to set up a cron task to run Unison automatically every 1 minute. Run this command to open the cron task file and paste the line that follows in to the file and save it.
sudo crontab -e

*/1 * * * * /usr/bin/unison &> /dev/null

Check the /var/www folders on both servers. You should now see that all files and folders are synchronised between them.

comments powered by Disqus