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:
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:
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.
Related Article : Email Marketing 101: Tips And Tricks For Success
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:
Then paste in the following
<contactpoint email=”root@localhost” name=”Administrator”/>
<path type=”recursive” alert=”off” exec=”/usr/bin/unison -batch websync”>/var/www</path>
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.