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/uptimemadeeasy.com/wp-content/uploads ssh://email@example.com//website/uptimemadeeasy.com/wp-content/uploads -batch
The above example does a great job of taking the files from both sides and syncing the directories.
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] 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/uptimemadeeasy.com/wp-content/uploads root = ssh://firstname.lastname@example.org//website/uptimemadeeasy.com/wp-content/uploads
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.