The sys admin's daily grind: Unison

Syncing in Unison


Charly may be a traditionalist with sync-unfriendly duplicate home directories, but a little utility called Unison helps keep his data consistent . This month you'll learn why Rsync sometimes isn't enough.

By Charly Kühnast

I'm an old-fashioned kind of guy. You can tell by the way I still use a desktop computer, instead of entrusting my data to a laptop. The downside of this traditionalism is that I sometimes need to synchronize my home directories between my desktop and my laptop.

Rsync is not a solution to this problem. The Rsync tool is a one-way street that updates a slave copy to reflect the master copy status. If I used Rsync consistently with the --delete parameter set, it would flatten any changes I had made on the slave machine in the meantime. The antidote to this problem is a tool called Unison [1].

Unison relies on SSH to synchronize between computers. Under the hood, Unison still uses the Rsync algorithm to keep the volume of data to a reasonable size.

At its simplest, a call to the Unison program just specifies the two directories to synchronize:

unison -ui text /home/charly
ssh://192.168.1.16//home/charly

The double slash that follows the IP address is not a typo, but a required feature. The -ui text parameter prevents Unison's GUI from interrupting. The tool creates a hidden .unison directory in which you can store a file with defaults. To create a homedir.prf file with the following content, I used:

root = /home/charly
root = ssh://192.168.1.16//home/charly
ignore = Path .unison
ignore = Path Desktop

After doing so, I can simply enter unison homedir to pass the name of the configuration file, without the .prf extension, to the program.

Silent Running

Unison can get on my nerves with its continuous prompts, but I can keep the tool quiet with:

unison homedir -batch

to launch Unison batch mode, which is fine for handling simple cases without any intervention on my part. In case of conflict, the tool leaves the files untouched. I can then take care of the remaining files by launching a manual synchronization session.

Conflicts can occur, for example, if you edit a file on both systems after the last sync. In this case, Unison does not simply assume that the most recent edit is the version you want to keep. Instead, it lets you choose how to deal with the conflict (Figure 1).

Figure 1: Unison in interactive mode resolves conflicts that occur when you change a file in the home directory on your desktop and laptop after a sync.

Take care if you use Unison for cross-platform applications. (The program is available for other flavors of Unix and Windows.) In cross-platform settings, different handling of upper- and lowercase letters and - even worse - Unicode or composed/decomposed UTF 8 can cause headaches, even for cutting-edge users.

INFO
[1] Unison:http://www.cis.upenn.edu/~bcpierce/unison
THE AUTHOR

Charly Kühnast is a Unix System Manager at the data center in Moers, near Germany's famous River Rhine. His tasks include ensuring firewall security and availability and taking care of the DMZ (demilitarized zone).