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:
- official website: http://www.seas.upenn.edu/~bcpierce/unison/ or http://www.cis.upenn.edu/~bcpierce/unison/
- In Linux Pro Magazine: Syncing in Unison (by Charly Kühnast 09/2007):
english: http://www.linux-magazine.com/w3/issue/82/Charlys_Column.pdf
german: http://www.linux-magazin.de/layout/set/print/content/view/full/14351 - Unison and Conduit in Linux Magazin (02/2008):
english: http://www.linux-magazine.com/w3/issue/87/Conduit_And_Unison.pdf
german: http://www.linux-magazin.de/Online-Artikel/Synchronisation-mit-Unison-und-Conduit - excellent wiki article (in German): http://wiki.ubuntuusers.de/Unison
Thanks for this post Philipp, very good. I'm using Unison with many of the same options as you. Do you find a problem with the timestamp on directories when using times=true? I find that it keeps the original time of files but directory times are always timestamped at their moment of (Unison) creation?
Mark P markshop at pengelly dot info
This timestamp issue has been bugging me forever. Thanks for posting the solution. If anyone else is in my situation, I got everything synced first using unison, then added
times=true
prefer=older
to my profile and re-ran unison. I then removed the 'prefer' line above (or else one would be very, very sorry next time you synced).