Move Over Rsync, Unison is Better at Bi-Directional Syncing

I installed a new redundant WordPress instance on two different VPS servers the other day.  One of my biggest worries with the new system was how would media images and files uploaded by the content editors on one of the systems would be kept in sync with media images and files uploaded to other server.  In the past, I had done a bit of tricky rsync work, but this time, with a little research, I found the unison command.

Install Unison on CentOS

Unison is installed using yum on CentOS.  You likely won’t have it installed by default.

# yum install unison

Bidirectional Rsync Using Unison

While this works great for keeping two WordPress uploads directory in sync, the unison command works great for keeping any two folders synced up.  Let’s give a few examples to show you how it works.

Example – Rsync two WordPress uploads directories

$ /usr/bin/unison /website/ ssh:// -batch

The above example does a great job of taking the files from both sides and syncing the directories.

Unison Options

There are so many options to the unison command that it has become beyond the scope of this article to list them all here.  You can get a listing of all of the unison options using the –help syntax.

$ unison --help
Usage: unison [options]
 or unison root1 root2 [options]
 or unison profilename [options]

 -addprefsto xxx file to add new prefs to
 -addversionno add version number to name of unison executable on server
 -auto automatically accept default actions
 -backup xxx add a pattern to the backup list
 -ui xxx select user interface ('text' or 'graphic'); command-line only
 -version print version and exit
 -xferbycopying optimize transfers using local copies, if possible
unison: unknown option `--help'.

Be Cautious With File Deletion and Unison

I want to make it clear that the Unison command is not a replacement for a backup.  This became obvious when I deleted a file on one of the servers and found that unison correctly deleted it on the other side the next time it was run.  How does unison know whether the file was added last or deleted last?

Unison’s .unison Directory

The unison command keeps track of its last run, in fact, all of its runs in its archive file.  You will find the unison archive file in the $HOME/.unison directory.  The filename is a long nonsensical string, as shown below.

Example:  Showing the $HOME/.unison directory’s contents including the archive and the default.prf files.

$ ls -l $HOME/.unison
total 1032
-rw------- 1 deploy deploy 1052204 Aug 6 19:56 ar4be4fbcea60c4d32dc92eb471ba26fcf
-rw------- 1 deploy deploy 26 Aug 6 10:32 default.prf

Unison prf Profile or Preferences Files

If you are going to be synchronizing 2 machines’ directories frequently, then you may want to do it using a profile file.  Profile files can be named however you like and have a default .prf extension.  Unison profile files are also stored in the $HOME/.unison directory.  The most important part of the profile file is the listing of both server’s directories using the “root” option.

Additional options for your .prf files are

Option Description / Example
ignore Files to not synchronize.  Example:  directory1/somefile* or Name *.jpg
path paths of files / directories to include.  Example:  path = mary/photos
include include instructions from another .prf file.  Example:  include common

Example:  A unison profile file is defined below

$ cat default.prf
# Unison preferences file
root = /website/
root = ssh://

Now, when we want to sync our directories, we just type “unison” and the unison command will find the default.prf file and follow the instructions inside.  If you have multiple unison tasks that you may want to perform, you can build multiple .prf files each with instructions for a unison task.  You would then call the specific task by typing unison + unison profile file name – .prf.

Example:  Run the unison command defined in the uptimemadeeasy.prf file:

$ unison uptimemadeeasy

The above command will find the uptimemadeeasy.prf file in the $HOME/.unison directory and run the unison command using the options defined inside.

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

2 comments for “Move Over Rsync, Unison is Better at Bi-Directional Syncing

  1. Ivan Vasquez
    October 28, 2014 at 8:38 pm

    Hi! why exactly did you prefer unison over rsync? I’ve used both in the past (also for a reduntant WordPress instance), and distinctly remember that unison took a long time in it’s first run.

  2. October 29, 2014 at 12:38 pm

    The situations where I use unison is when either server may be the one that is updated. In these cases, unison is better because it brings in the changes from both sides. Unison is best when both sides are the source and both are the destination for the changes.

    Rsync works find when one side is the source and the other is the destination.

Leave a Reply

Your email address will not be published. Required fields are marked *