Syncronize Files Between Two Ubuntu Servers Using Unison and Incron

Syncronize Files Between Two Ubuntu Servers Using Unison and Incron

Replicate your website syncronously with master-master servers

by Janeth Kent Date: 21-06-2013 syncronization unison ubuntu linux incron

File Synchronization Between Two Ubuntu Servers using Unison

Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other.

 

This tutorial shows how to set up file synchronization between two Ubuntu 12.04 servers with Unison that are on the same network but you can use the same approach on the servers that are not placed on the same network.

Before installing the unison,generate the ssh key pair on PrimarySrv and copy the public key to the SecondarySrv:

ssh-keygen
scp ~/.ssh/id_rsa.pub [email protected]:

Connect to the SecondarySrv, append the public key to authorized_keys:

cat id_rsa.pub >> ~/.ssh/authorized_keys

We got an error saying "~/.ssh/authorized_keys: No such file or directory" it means that there is no .ssh directory for this user (this user has never used ssh before). Simply create an empty .ssh directory with 700 permissions and try to append the public key to authorized_keys once again:

Now,try to connect to SecondarySrv from PrimarySrv, and this time, it will not ask for the password 

ssh [email protected]

Now, issue this command for the installation of unison on both server:

sudo apt-get install unison

Use this command to verify that the local unison client can start and connect to the remote server(In this case,remote server's ip address is 192.168.1.203):

unison -testServer /var/www/ ssh://192.168.1.203//var/www

Now, try to sync with the remote server with the -batch (batch mode) option ask no questions at all:

unison -batch /var/www/ ssh://192.168.1.203//var/www

Create a sample file inside the /var/www/ directory with name "server1" on PrimarySrv and run the unison again:

cd /var/www/
sudo touch server1
unison -batch /var/www/ ssh://192.168.1.203//var/www

Verify the /var/www/ directory on SecondarySrv:

For two-way synchronization verification, create a file on SecondarySrv with the name "server2" inside the /var/www/ directory:

sudo touch server2

Run unison again on PrimarySrv for sync:

unison -batch /var/www/ ssh://192.168.1.203//var/www

Verify the synchronization status on PrimarySrv:

Edit the crontab file to make the sychronization process automatically:

crontab -e

Make the unison to run every 30 min automatically:

*/30 * * * * unison -batch /var/www/ ssh://192.168.1.203//var/www

How To Call unison On Demand?

For this, we need to install the incron(inotify cron daemon) which monitors filesystem events (e.g. add a new file, delete a file etc) and executes the command or scripts.It's usage is generally similar to cron.

Type the following command for incron installation:

sudo apt-get install incron

Configure the /etc/incron.allow file in order to allow user to use incron:

sudo nano /etc/incron.allow

In this case the user is "arbab"(you can change it with your desired user):

Now edit the incrontab:

incrontab -e

Run unison command automatically when file created,deleted or modified from /var/www directory:

/var/www IN_CREATE,IN_DELETE,IN_CLOSE_WRITE unison -batch /var/www/ ssh://192.168.1.203//var/www

Create a sample test file inside the /var/www/ directory with name "incron" on PrimarySrv:

cd /var/www/

sudo touch incron

Verify the synchronization status on SecondarySrv:

Hope this will help you.

 
by Janeth Kent Date: 21-06-2013 syncronization unison ubuntu linux incron hits : 8351  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 

Related Posts

How To Use Varnish As A Highly Available Load Balancer On Ubuntu 20.04 With SSL

Load balancing with high availability can be tough to set up. Fortunately, Varnish HTTP Cache server provides a dead simple highly available load balancer that will also work as a…

How to install a Linux partition on a Windows 10 PC

In spite of a past we could say almost confronted, the approach between Windows and Linux is accelerating more and more, drawing a story closer to love than to hate.…

WSL2 is released to run Linux distributions on Windows

If you are reading about this for the first time, the Windows Subsystem for Linux is a kind of virtual machine that allows you to run the Linux terminal on…

Linux For Dummies: Permissions

In the previous articles I made a short introduction to the Unix world and in the following article I have dealt with the basic commands for the file system management. Today we are…

Linux for Dummies: Ubuntu Terminal

I introduced in the previous article, available here, the basic concepts concerning the Linux world. Today we are going to have a look to some basic operations that we can perform…

Linux for Dummies: Introduction

If you have thought about migrating from Windows to a Unix operating system, or Linux specifically there are things you should know. The goal is to give essential information (and…

The Best RSS Readers for Ubuntu

Even if most of the tech experts actively claim that RSS (Rich Site Summary) is dead especially after Google Reader was discontinued 5 years ago but it isn’t yet as…

80 Linux Network Monitor Software & Tools for Managing & Monitoring Unix/Linux Systems

It’s hard work monitoring and debugging Linux performance problems, but it’s easier with the right tools at the right time. Finding a Linux Network Monitor tool or Software package for…

How to install Letsencrypt Certificates with Certbot in Ubuntu

In this article we will explain how to install, manage and configure the SSL Security certificate, Let's Encypt in NGINX server used as proxy. This certificate is free but does…

How to Set up a Fully Functional Mail Server on Ubuntu 16.04 with iRedMail

Setting up your own mail server from scratch on Linux is complex and tedious, until you meet iRedMail. This tutorial is going to show you how you can easily and…

GIMP 2.10 released: Features 32-bit support, new UI and A Ton Of Improvements

It's been over a half-decade since the GIMP 2.8 stable debut and today marks the long-awaited release of GIMP 2.10, its first major update in six years. And among other…

Setting Up SFTP on Ubuntu 16.04

I recently had a request to setup SFTP for a customer so they could manage a set of files in their environment through an FTP GUI. Being an avid user…