Philipp's Computing Blog

Success is about speed and efficiency

Synchronize Directories using Unison

Unison is a great tool to keep directories and files synchronized. It is extremely simple to use once you figure out how to do the setup and it can be done automatically (via a cronjob).

I show you here how to do it:

If you are not sure if used Unison before, make a backup of the default configuration file:

mv ~/.unison/default.prf ~/.unison/default.old.prf

Then start your first synchronization run manually:

unison ~/university/ ssh://philipp@lion//home/philipp/university

Where lion may be a hostname or IP in your local network or a domain name or IP on the Internet (your server or your dyndns host at home).
After the synchronization finished, you have a new file ~/.unison/default.prf containing the settings of the last run:

root = /home/philipp/university/
root = ssh://philipp@lion//home/philipp/university

So to move this file to a named profile do:

mv ~/.unison/default.prf ~/.unison/localnet.prf

Now you can run this profile using:

unison localnet

The configuration file (~/.unison/localnet.prf) gives you the possibility to adjust advanced settings:
For the synchronization operation to run without user interaction, add the option batch=true. To force Unison to resolve conflicts by keeping the newest files is equally simple: prefer=newer. If you want Unison to preserve the files’ modification times, add the times=true to the .prf config file.
So the configuration file could then look like this:

root = /home/philipp/university/
root = ssh://philipp@lion//home/philipp/university
batch=true
prefer=newer
times=true
group = true
owner = true
logfile = /var/log/unison.log

Resources: