Setting Up Unison File Synchronization Between Two Servers On Ubuntu 11.10
Author: Falko Timme
Follow me on Twitter
Last edited 12/09/2011
This tutorial shows how to set up file synchronization between two Ubuntu 11.10 servers with Unison. Unison is a file-synchronization tool similar to rsync, but the big difference is that it tracks/synchronizes changes in both directions, i.e., files changed on server1 will be replicated to server2 and vice versa.
I do not issue any guarantee that this will work for you!
1 Preliminary Note
In this tutorial I use the following two Ubuntu 11.10 servers:
- server1.example.com with the IP address 192.168.0.100
- server2.example.com with the IP address 192.168.0.101
I want to synchronize the directory /var/www between the two servers. I will run Unison as the root user in this tutorial so that Unison has sufficient permissions to synchronize user and group permissions.
I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:
2 Installing Unison
Unison has to be installed on server1 and server2; since we connect from server1 to server2 using SSH, we also need the SSH packages. This can be achieved as follows:
apt-get install unison openssh-server ssh
3 Creating A Private/Public Key Pair On server1
Now we create a private/public key pair on server1.example.com:
ssh-keygen -t dsa
root@server1:~# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): <-- ENTER
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <-- ENTER
Enter same passphrase again: <-- ENTER
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
32:0f:f5:49:f0:32:f8:d0:63:8d:44:88:a5:12:f9:73 [email protected]
The key's randomart image is:
+--[ DSA 1024]----+
| .. o.o+ |
| .....+ = |
| ... o O + |
| .o E= * . |
| o+ S o |
| = |
| . |
It is important that you do not enter a passphrase otherwise the mirroring will not work without human interaction so simply hit ENTER!
Next, we copy our public key to server2.example.com (please note that the root account must be enabled on server2.example.com, and that root logins must be allowed. To enable the root login on an Ubuntu system, run
sudo passwd root
To check if root logins are allowed check the directive PermitRootLogin in /etc/ssh/sshd_config - you might have to restart the SSH daemon afterwards.):
ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
root@server1:~# ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.
ECDSA key fingerprint is a2:38:f3:df:7a:6c:b6:3c:d6:c3:9c:88:93:e2:f0:63.
Are you sure you want to continue connecting (yes/no)? <-- yes (you will see this only if this is the first time you connect to server2)
Warning: Permanently added '192.168.0.101' (ECDSA) to the list of known hosts.
[email protected]'s password: <-- server2 root password
Now try logging into the machine, with "ssh '[email protected]'", and check in:
to make sure we haven't added extra keys that you weren't expecting.
Now check on server2 if server1's public key has correctly been transferred:
ssh-dss AAAAB3NzaC1kc3MAAACBAJgYGqpkEPBIV/Uiygz2FbOwCE9ggjXQK8anG/VxaTqH0KhZR+oL1i+Av2aZmK/D3msUQEkqClO51jarQXbm9i7ie7Y6OwQ4G8DBZXzy8A8nQ6V1bEikqif8xmmSayqVKPvHXMv3yINteUdCNYBHAuci1NB5iAf3Gmt/bo5SRpU1AAAAFQDJnh+a6lODf+zSF2eX1L4HvQgtxwAAAIAIOdjsJkzBDseOdTMD5oVZqrrp6cy0e1846DZ4C0PKQSeT9RQqKmbOICa8t94FF8MRLdV0p4x+R8I8VHRloErQ5v1CIVvrtzOGIGAR//Bo5wPI+8lsfbsOj8ZyZI6+bF6wHKjzNFLbgFpZjnIf/CisP87GtPp8MXq3bH7SI/7i6gAAAIAfByiUkqQZ7fOM18BHTqQhopxMjhJhvrATt0PJ/k2JzQp0lQm/Z31azpvccK5GF61AwWbfQSFAGy4AOKtVXZ6ztz80sKZeDwECNFJpT6rubjsbaSX2PvacrV8sAuJI1XZPAgQTl06mJLL9dC7rNrY+2QgXoJqFtgjiFGWhCC+UjQ== [email protected]
4 Running Unison
We can now run Unison for the first time to synchronize the /var/www directory on both servers. On server1 run:
unison /var/www ssh://192.168.0.101//var/www
Output will be similar to this one - you might have to answer a few questions as this is the first time Unison is being run:
root@server1:~# unison /var/www ssh://192.168.0.101//var/www
Connected [//server1.example.com//var/www -> //server2.example.com//var/www]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
Update detection may take a while on this run if the replicas are
Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.
If you see this message repeatedly, it may be because one of your machines
is getting its address from DHCP, which is causing its host name to change
between synchronizations. See the documentation for the UNISONLOCALHOSTNAME
environment variable for advice on how to correct this.
Donations to the Unison project are gratefully accepted:
Waiting for changes from server| webalizer
dir ----> apps [f] <-- ENTER
file ----> index.html [f] <-- ENTER
link ----> ispconfig [f] <-- ENTER
dir ----> php-fcgi-scripts/apps [f] <-- ENTER
dir ----> webalizer [f] <-- ENTER
link ----> webmail [f] <-- ENTER
Proceed with propagating updates?  <-- y
UNISON 2.32.52 started propagating changes at 14:25:02 on 09 Dec 2011
[BGN] Copying apps from /var/www to //server2.example.com//var/www
[BGN] Copying index.html from /var/www to //server2.example.com//var/www
[BGN] Copying ispconfig from /var/www to //server2.example.com//var/www
[BGN] Copying php-fcgi-scripts/apps from /var/www to //server2.example.com//var/www
[BGN] Copying webalizer from /var/www to //server2.example.com//var/www
[BGN] Copying webmail from /var/www to //server2.example.com//var/www
[END] Copying ispconfig
[END] Copying webmail
[END] Copying apps
[END] Copying webalizer
[END] Copying index.html
[END] Copying php-fcgi-scripts/apps
UNISON 2.32.52 finished propagating changes at 14:25:03 on 09 Dec 2011
Saving synchronizer state
Synchronization complete at 14:25:03 (6 items transferred, 0 skipped, 0 failed)
Check the /var/www directory on server1 and server2 now, and you should find that they are in sync now.
Of course, we don't want to run Unison interactively, therefore we can create a preferences file (/root/.unison/default.prf) that contains all settings that we otherwise would have to specify on the command line:
# Unison preferences file # Roots of the synchronization root = /var/www root = ssh://192.168.0.101//var/www # Paths to synchronize #path = current #path = common #path = .netscape/bookmarks.html # Some regexps specifying names and paths to ignore #ignore = Path stats ## ignores /var/www/stats #ignore = Path stats/* ## ignores /var/www/stats/* #ignore = Path */stats ## ignores /var/www/somedir/stats, but not /var/www/a/b/c/stats #ignore = Name *stats ## ignores all files/directories that end with "stats" #ignore = Name stats* ## ignores all files/directories that begin with "stats" #ignore = Name *.tmp ## ignores all files with the 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.) auto=true # 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. batch=true # !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, synchronization 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. confirmbigdel=true # 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. fastcheck=true # When this flag is set to true, the group attributes of the # files are synchronized. Whether the group names or the group # identifiers are synchronizeddepends on the preference numerids. group=true # When this flag is set to true, the owner attributes of the # files are synchronized. Whether the owner names or the owner # identifiers are synchronizeddepends on the preference # extttnumerids. owner=true # 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! prefer=newer # 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. silent=true # When this flag is set to true, file modification times (but not # directory modtimes) are propagated. times=true
The comments should make the file self-explaining, except for the path directives. If you specify no path directives, then the directories in the root directives will be synchronized. If you specify path directives, then the paths are relative to the root path (e.g. root = /var/www and path = current translates to /var/www/current), and only these subdirectories will be synchronized, not the whole directory specified in the root directive.
You can find out more about the available options by taking a look at Unison's man page:
Now that we have put all settings in a preferences file (especially the root (and optionally the path) directives), we can run Unison without any arguments:
5 Creating A Cron Job
We want to automate synchronization, that is why we create a cron job for it on server1.example.com:
*/5 * * * * /usr/bin/unison &> /dev/null
This would run Unison every 5 minutes; adjust it to your needs (see
man 5 crontab
). I use the full path to unison here (/usr/bin/unison) just to go sure that cron knows where to find unison. Your unison location might differ. Run
to find out where yours is.