Configuring Unison

by alex

Unison is a file syncronization software, using the rsync algorithm.
Unlike rsync, Unison makes two-way file synchronization very simple.
Unison also allows you to syncronize files across different platforms and filesystems.
There are official Unison releases for several platforms, including Linux and Windows, although in my Windows machine I use Linux’s version through Cygwin.
You can actually also install a GUI for Unison (which probably come by default with some pre-compiled binaries, such as the Windows version).

It allows you to perform local syncronizations, for example between your main hard drive and an external hard drive, or with a remote host by using ssh.

If you want to sync two roots regularly, you should create a prefences file (.prf) file. Here’s an example of the file I use for mirroring my working directory in my SD card (FAT32) with my external hard drive (NTSF) in Windows (using Cygwin). In Linux this file should go in your .unison directory located in your home folder.


# Unison preferences file

# roots
root=/cygdrive/f/Documents
root=/cygdrive/g/DocumentsBackup

# for mirroring
force=/cygdrive/f/Documents

# to sync fat32
perms=0
fastcheck=true

# prevent unison from prompting the user
batch=true

Here I am mirroring my /cygdrive/f/Documents directory.
Note how I name the directories in the two roots in a different way. In this case, if by any chance the drive name changes, I am not mirroring things in the opposite direction! (an alternative is to completely disable mirroring)
Note that since I store my data in an SD card using the FAT32 filesystem which does not support the same set of permissions as NTFS or EXT , I set “perms=0” (otherwise Unison will complain about permissions).

And here’s the .prf file I use for synching my working directory with my server (via ssh):

# Unison preferences file

# roots
root=/cygdrive/f/Documents
root=ssh://alex@myserver/backupDirectory

# for mirroring
force=/cygdrive/f/Documents

# to sync fat32
perms=0
fastcheck=true

# prevent unison from prompting the user
batch=true

# to prevent the ssh connection from hanging
sshargs = -o ServerAliveInterval=120

One more thing that is wort mentioning, is that, even with ServerAliveInterval set to a large value, the synchronization may fail whenever there are many files or these a very large due to connection reset by peer.
In this case, what works for me is to synchronize smaller subsets of files before synchronizing the full roots.
This can be achieved via the path variable. Imagine you have a folder /cygdrive/f/Documents/foo/bar that you want to synchronize, using the roots above.
Then, by setting,


path = foo/bar

Unison will synchronize only that folder.

Please note that this aims to be a small introduction to Unison and not a comprehensive one. In case you need more information, you should always consult the user manual.

You can obviously schedule automatic synchronizations in Linux using crontab or identical tools in Windows, and using public key authentication but that is material for another post…

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.