Data synchronisation using Unison

 · Systeemkabouter

Some time ago I found a small utility called unison. I used it for some time to sync my homedir on a Linux box to my Windows laptop, but later decided to go with the Winows built-in 'Offline files' thingie (bad idea, don't do it).

What is Unison?

Unison resembles rsync in some ways, it looks at to files trees and synchronises them by only exchanging the parts of files that actually changed. The big difference is that rsync is great for syncing one-way, but unison actually synchronises both ways

More on unison on its homepage: http://www.cis.upenn.edu/~bcpierce/unison/.

What do I use it for?

I've been using Unison for a couple of weeks now to keep exact copies of some of my websites on two servers. These two servers are both capable of serving requests and Unison makes sure that dynamic content (form based uploads for instance) gets distributed to both servers.

This, combined with my Multi-Master MySQL setup leaves me with a redundant setup for some of my websites.

As this is a work in progress, I haven't done any proper scripting yet, but this small script does the job quite nicely for now:

more /opt/stuff/bin/synchronize_data.sh
#!/bin/bash
#
#
# When rsync is not good enough, try unison
#
# SyncAddict, Feb 05, 2006
# Delft, The Netherlands

UNISON=/usr/bin/unison
DSTHOST=cypher.vpn
DSTDIR=/home/websites


for i in syncaddict web2 web3 website4;
do
$UNISON /home/websites/$i ssh://$DSTHOST/$DSTDIR/$i -batch -owner -silent
scp /etc/apache2/sites-available/$i $DSTHOST:/etc/apache2/sites-available/
done

Currently, I run this script every 15 minutes, but after I add a check to see if the script is already running, I will probably schedule it to run every 5 minutes.

Next thing on the list is to synchronise my mailboxes in a simular way and install imap daemons on both hosts. Maybe it will work, or maybe I will never so your mail (or any other mail ;)