Configuring Unison File Synchronization
Welcome back to our series on using the VPS.net public cloud to build your own highly available website solution. So far we’ve looked at how to get the servers set up, and configuring the MySQL server on each server in a Master-Master replication setup. With our database now being replicated between the servers, the next step is to ensure that the website’s files will also be replicated between them. For this we’ll be using the Unison file synchronization tool.
Using Unison to Synchronize Files Between Two Directories
Unison is a nice and effective tool for synchronizing the files between two directories. This can be done using two directories on the same server or across multiple servers. It can also be used to synchronize directories across different systems, such as Windows to Mac or Linux to Windows. It comes with the standard repositories on Debian-based Linux distributions which helps make getting set up simple. The downside to Unison is that it needs to be run every time you want to synchronise the changes; it doesn’t run as a daemon. Next time we’ll be looking at a solution to automate that. All the steps will need to be performed on each server, so we’ll start with setting everything up on the first one, then the process can be repeated for the second.
Setting Up Unison On Your Servers
So we’ll start with installing both unison:
sudo apt-get install unison
With that installed, we’ll need to create a user that can synchronize the files. We could use the root user but that may pose a potential security risk. We’ll call this user www-sync to make its purpose clear.
sudo adduser www-sync
You’ll be prompted with a few questions for the user, all of which you can skip through except the password request, for which you should assign a secure password. After this we need to add the www-sync user to the www-data group:
sudo usermod -G www-data www-sync
The reason for this is that on Debian-based systems, the www-data user is the one that the web server runs as. As such it should have read and write access to all the files in the server’s website directory on which we will rely for synchronizing the files. If the www-data user and group don’t have ownership of your website files, you can set this with:
sudo chown -R www-data:www-data /var/www
With that done, we can now change user to the www-sync user and look at creating the configuration required for unison to synchronize the directories:
sudo su www-sync
nano ~/.unison/websync.prf
Now paste in the following configuration:
# Prevent Unison asking any questions
auto=true
batch=true
fastcheck=true
silent=true
#keep the attributes of the file
group=true
owner=true
times=true
#prefer the newest file if there is a conflict
prefer=newer
#configure the directories to use
label=WebDirectory
root=/var/www
root=ssh://www-sync@192.168.0.3/var/www
sshargs=-C
Remember that when pasting this in, you’ll need to change the IP 192.168.0.3 for the IP for your setup. When pasting onto the first server, use the IP of the second server, and when pasting onto the second server use the IP of the first server. Once this is done, save and exit the file.
Next we’ll need to create some SSH keys so that the two servers can synchronise without needing to prompt you for a password each time:
ssh-keygen -t rsa
You don’t want to set a passphrase for this so hit enter when prompted. At this point it’s a good idea to move over to the second server and repeat these steps so that that one is configured.
With the other server done, we now need to copy the public keys over to the other servers. This can be done with ssh-copy-id:
ssh-copy-id www-sync@192.168.0.3
You’ll need to do this on both servers, again making sure that on the first server you use the IP of the second and vice versa for the second server.
With that done you can now test that unison can copy the files from one server to the other. Create or edit a file within the /var/www directory or a subdirectory and then run:
unison -batch websync
This should have synchronised the change over to the other server, and means we now have unison working to synchronise our data between servers.
Next time we’ll be looking at how we can automate the process of copying those files over.