Unison: setting up and automating two-way synchronization of directories on two servers


Getty Images / iStockphoto

The problem of synchronizing directories on two servers with Linux family of operating systems on board can be solved easier if you use specialized tools. Let’s see how this can be done with Unison.

Perhaps you have several servers, and you are faced with the task of synchronizing certain directories on these servers. For example, you need to synchronize a directory / data on each of these servers so that a certain application has access to relevant information. Or maybe you just need to periodically back up data from one server to another in a synchronized fashion.

Unison is like a synchronization utility rsync, but unlike it, it supports two-way synchronization. That is, it allows you to synchronize two copies of files, updating each copy depending on the changes made.

When using Unison on servers, it is desirable to have installed packages such as openssh-server and ssh since for security reasons, synchronization is best done using the SSH protocol. However, if you are confident in the security of your network (for example, the server can use SSH authentication without a password), you can not bother.

What else is needed to use Unison

I will show the whole process using two Ubuntu servers as an example (both 18.04). You can install and use Unison with other distributions, but then you will need to change the installation command, otherwise Unison can be installed from standard repositories (of course, your user must have sudo privileges).

I will use these servers:

  • server1 – 192.168.1.6
  • server2 – 192.168.1.19

How to install Unison

The first thing to take care of is installing Unison. This should be done on both servers. Go to both servers and enter the command:

sudo apt-get install unison unison-all -y

Wait for the Unison installation to complete and continue.

How to generate and copy an SSH key

First, we generate an SSH key only for server1. To do this, use the command:

ssh-keygen -t rsa

When you are asked to enter a password, just press the ENTER key.
When the key is generated copy it to server2 using the command:

ssh-copy-id 192.168.1.19

Once the key is copied, you can proceed directly to business.

How to use Unison

Let’s create one test directory for each server for test purposes. Team for server1:

sudo mkdir -p /data1

For server2:

sudo mkdir -p /data2

Next, on both servers, you must change the name of the owner of the created directory, otherwise Unison will not be able to write anything there. As the owner, specify the user who will run the Unison command:

sudo chown -R $USER.$USER /data2

Do the same for server1:

sudo chown -R $USER.$USER /data1

Put some test files in / data1:

touch /data1/{test1,test2,test3}

We synchronize our directories with the following command (by running it on server1):

unison /data1 ssh://192.168.1.19//data2

Since you are trying to synchronize these root directories for the first time, you will receive a warning that synchronization may take some time (but in our case this will not happen, since we added only three test files of the minimum volume to the directory). If it were a backup on production, the first launch could really take some time.

So drink tea next time. In the meantime, press Enter to start the process. After you do this, you will be asked to confirm the synchronization of each file. When finished, enter ‘y’ to continue.

Since we only synchronize three test files, this will happen very quickly and will return you to the bash shell. To make sure the files are synchronized, go to server2 and enter the command:

ls /data2

Next, the system will report that in the directory / data2 our test1, test2, and test3 files are:

We did it: Unison synchronized both directories.

How to start Unison without the need for user interaction

I think you do not want to constantly confirm the synchronization of each directory file every time you start Unison. This is especially annoying when you synchronize directories with a large number of files. Therefore, automation is needed. Go to server1 and enter the command:

nano ~/.unison/default.prf

When the configuration file opens for editing, add two lines to it:

auto=true
batch=true

Then save and close the file.
Now Unison will not bother you with trifles. But that’s not all.

How to create a task for Cron Scheduler

I think you would hardly want to start synchronization manually, because you can make a mistake somewhere or, for example, not take into account the change in the directory structure on the servers. To set up automation, you need to create a cron job for server1.
Let’s create a script to start synchronization:

sudo nano /usr/local/bin/unisonsync

The following should be added to this file:

#!/bin/bash/
unison /data1 ssh://192.168.1.19//data2

Next, you need to give the script the right to execute:

sudo chmod ugo+x /usr/local/bin/unisonsync

Create a job for Cron with the command:

crontab -e

After that type:

*/5 * * * * /usr/local/bin/unisonsync &> /dev/null

Now the synchronization will automatically start every 5 minutes.

I have shown that Unison makes it easy to synchronize directories on two Linux servers. You can verify this yourself by adding files to the corresponding test directories / data1 and / data2 on both servers. After making sure that the synchronization works, you can use it in combat mode on production servers.


As an advertisement

Vdsina I am always glad to offer reliable servers for any tasks. You have a huge choice of operating systems for automatic installation, it is possible to install any OS from your own imageas well as use high speed local network between servers within the same location.

Similar Posts

Leave a Reply