Automating Unison
In the previous part of our series of tutorials for creating a highly available solution using the VPS.net public cloud, we looked at setting up Unison to synchronize the files in the website’s directory between the servers. Unfortunately Unison has a small problem is that it only synchronizes the files when it is run. This time we’re going to look at automating it.
Obviously you don’t need to automate this if your website’s files are very rarely updated; you can manually run Unison every time you make a change. Having it as a manual task makes it very easy to forget when you are busy making updates, so some form of automation is definitely recommended.
There are a few options for automating the running of Unison to suit your needs:
The first method is to set up a cron task to run Unison at regular intervals. The configuration for this is relatively simple, though there are issues in that it’s possible for a file to change on one server, and that server to die prior to cron synchronizing that file to the other server.
This works better as a solution for sites where the files are changed infrequently or at regular times. One example is on a site where only the site manager makes changes, such as with e-commerce or a private blog. This way the files can be updated ready for the sync to take place or the server monitored for the sync to complete once the change is made.
To set up the cron task, we need to edit the cron table for the www-sync user that we created previously, so to start we’ll change to that user:
su www-sync
You’ll be prompted for the password you set for the www-sync user. Once you have changed users, we’ll go into editing the user’s cron table:
crontab -e
You will be prompted to choose a default editor to modify the cron table. After selecting the one you want, it will open a text editor with a blank cron table. Add the following line to the end of the file:
*/5 * * * * /usr/bin/unison -batch websync
This means that unison will be run to synchronize the files every 5 minutes. There are instructions in the blank cron table file that explain the format and can help you tweak the command to fit the frequency you want to use.
However, if your website has more frequent file changes, or can be randomly changed by users uploading files, you may want to run the synchronization each time a file changes. For that there is a utility called iwatch. Installation is as simple as:
sudo apt-get install iwatch
If you don’t already have a mail server installed on your server, it will prompt to install postfix which will allow iwatch to send mail notifications. If this comes up, select “internet site” from the options and ensure the hostname of your server shows up.
With that installed, we need to create a configuration file for iwatch:
nano ~/websync.xml
Then paste in the following
<config>
<watchlist>
<title>Website</title>
<contactpoint email=”root@localhost” name=”Administrator”/>
<path type=”recursive” alert=”off” exec=”/usr/bin/unison -batch websync”>/var/www</path>
</watchlist>
</config>
By default the email alerting is set to “off”, so the email address provided won’t be sent any information. If you feel that you’d like the notifications sent to you, you can set the email address you want to use in the email section of contactpoint, then set alert to “on”. As it stands this will monitor /var/www and it’s subdirectories for any changes, and if some are detected it will run unison with the websync configuration. Once it is configured how you’d like, you can save and exit the file.
To start iwatch, simply use the following command:
iwatch -d -f ~/websync.xml
If the server reboots, iwatch will need to be manually started again. To make this happen automatically we’ll add the command to the /etc/rc.local file:
sudo nano /etc/rc.local
Then add the following line before the “exit 0” line:
su www-sync -c ‘/usr/bin/iwatch -d -f /home/www-sync/websync.xml’ 2>&1
With that done, iwatch will be started automatically after a reboot.