Looking to sync directories on two Linux servers in your data center and want to do it on the cheap? Unison might be just the tool you need.
You probably have a few Linux servers in your data center. You also might need to keep certain directories in those servers in sync. For instance, say you have a need to keep a /data directory in sync between those servers, so a single application has access to up-to-date information. Or maybe you simply need to keep a synchronized backup of one server to another.
Whatever your need, Unison can help you out.
Unison is a tool similar to rsync–the only difference being that it can track and synchronize both directories, not just one. The one caveat to Unison is that it requires you create a passwordless SSH authentication key (otherwise mirroring won’t work), so if this is a deal breaker for you, you’ll want to pass on this tool. However, if you feel confident in your network security, (such that a server can use SSH key authentication without a password) continue on.
SEE: 10 things companies are keeping in their own data centers (TechRepublic download)
What you’ll need
I’m going to demonstrate using two Ubuntu Servers (both 18.04). You can install and use Unison on different distributions, you only have to modify the installation command, as Unison can be installed from the standard repositories.
You’ll also need a user with sudo privileges.
I’ll be using the following:
- server1 – 192.168.1.6
- server2 – 192.168.1.19
How to install Unison
The first thing to take care of is the installation of Unison. This must be done on both servers. Log in to both servers and issue the command:
sudo apt-get install unison unison-all -y
Once Unison is installed, you’re ready to continue.
How to generate and copy an SSH key
Now we generate an SSH key on server1 only. For this issue the command:
ssh-keygen -t rsa
When prompted for a password, simply hit Enter on your keyboard.
Once the key is generated, copy it to server2 with the command:
With your SSH key copied, you’re ready to start using Unison.
How to use Unison
For testing purposes, let’s create directories on both servers. On server1 issue the command:
sudo mkdir -p /data1
On server2 issue the command:
sudo mkdir -p /data2
On both servers, you’ll want to change the ownership of the new directory–otherwise Unison won’t be able to write to it. Change the ownership to the username who’ll be running the Unison command like so:
sudo chown -P $USER.$USER /data2
Do the same on server1 with the command:
sudo chown -P $USER.$USER /data1
Place a couple of test files in /data1 with the command:
Now let’s sync the two directories with the command (run on server1):
unison /data1 ssh://192.168.0.19//data2
Because this is the first time you’ve attempted to sync these root folders, you’ll be warned that the synchronization can take some time (it won’t, because we’ve only added three empty files to the directory). If this were a production backup, the first run could take some time.
Hit Enter on your keyboard to launch the process. Once you’ve done this, you should be prompted to okay the sync of each file. When this is complete, type y to continue.
As soon as you type y, the sync will occur. Because we’re only syncing the three new files, it will happen very fast and return you to the bash prompt. To make sure the files are synced, go to server2 and issue the command:
You should see test1, test2, and test3 listed (Figure A).
How to run Unison without interaction
You don’t want to have to answer questions every time you run Unison, especially when you’re syncing directories with a lot of files. To prevent the interaction, issue the command (on server1):
In that configuration file, add the lines:
Save and close the file.
Now, when you run the Unison sync command, you won’t be prompted for any input.
How to create a cron job for Unison
You certainly don’t want to have to run those sync jobs manually, otherwise you might wind up forgetting and having out of sync directories in your data center servers. To set this up automatically, you’ll have to create a cron job on server1.
First, let’s create a script to run the sync. Issue the command:
sudo nano /usr/local/bin/unisonsync
In that file, add the following:
#!/bin/bash/ unison /data1 ssh://192.168.1.19//data2
Give that new script executable permissions with the command:
sudo chmod ugo+x /usr/local/bin/unisonsync
Create a cronjob with the command:
In the resulting file, add the following line (to run the script every 5 minutes):
*/5 * * * * /usr/local/bin/unisonsync &> /dev/null
Now you have a file sync system running that will keep directories on two of your Linux data center servers in sync every five minutes. Do make sure to test this by adding files to both data directories on both servers. Once you’re certain the sync is working, you can then set it up on a production machine.
Source of Article