Linux: Using lsyncd – Live Syncing (Mirror) Daemon

I have a situation where there is one critical website under our company server is critically need to be sync to our backup server. Sync means whatever changes happen in master server, it will replicated to slave server. Yes, you can rsync. But, I do not want to schedule the task as cron to sync. There is one tool which is better than that and very suitable to be applied in this situation called lsyncd.

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or blockdevices and does not hamper local filesystem performance.

OS: RHEL 5.3 (Tikanga) 64bit
Source IP: 192.168.50.10
Source directory: /home/webmedia/public_html
Destination IP: 102.168.50.20
Destination directory: /backup/webmedia/public_html

Destination server

In destination server, we just need to make sure that the destination directory is exist. If not, create the destination folder:

$ mkdir -p /backup/webmedia/public_html

Source server

1. It is good to have embedded host value that point to the IP address. Open /etc/hosts and add destination host value:

192.168.50.20   backup-server.local

2. We need to allow passwordless authentication to the destination server. We will using root user to execute the sync process. Run following command as root:

$ ssh-keygen -t dsa

Please enter for all prompts. Once done, run following command to copy the public key to destination server:

$ cat ~/.ssh/id_dsa.pub | ssh root@backup-server.local “cat >> ~/.ssh/authorized_keys”

Or we can use another command which do the same thing called ssh-copy-id:

$ ssh-copy-id -i ~/.ssh/id_dsa root@backup-server.local

Once done, try to access the destination server with following command. Make sure you can access the server without any password prompted:

$ ssh root@backup-server.local

3. Lsyncd required LUA to be installed. For latest RedHat distribution (RHEL 6) you can use yum to install:

$ yum install -y lua*

In my case, I am using the RHEL 5, and the packages are not available at yum. So I will need to find another place for RPM which is here: http://www6.atomicorp.com/channels/atomic/centos/5/x86_64/RPMS/

Download these 3 packages and install:

  1. lua-5.1.4-1.el5.art.x86_64.rpm
  2. lua-static-5.1.4-1.el5.art.x86_64.rpm
  3. lua-devel-5.1.4-1.el5.art.x86_64.rpm
$ mkdir -p /usr/local/src/lua
$ cd /usr/local/src/lua
$ wget http://www6.atomicorp.com/channels/atomic/centos/5/x86_64/RPMS/lua-5.1.4-1.el5.art.x86_64.rpm
$ wget http://www6.atomicorp.com/channels/atomic/centos/5/x86_64/RPMS/lua-devel-5.1.4-1.el5.art.x86_64.rpm
$ wget http://www6.atomicorp.com/channels/atomic/centos/5/x86_64/RPMS/lua-static-5.1.4-1.el5.art.x86_64.rpm
$ rpm -Uhv *.rpm

4. Download and install lsyncd from this website: http://code.google.com/p/lsyncd/downloads/list:

$ cd /usr/local/src
$ wget http://lsyncd.googlecode.com/files/lsyncd-2.0.6.tar.gz
$ tar -xzf  lsyncd-2.0.6.tar.gz
$ cd lsyncd-*
$ ./configure
$ make 
$ make install

5. Once done, start the daemon in the source server by using this command:

$ lsyncd -rsyncssh /home/webmedia/public_html root@backup-server.local /backup/webmedia/public_html

Done. The file should be in sync now. You can check the process using ‘ps‘ command and monitor the progress by constantly viewing the destination directory. In some cases, the sync terminate immediately with following error at /var/log/message:

lsyncd: Error, Consider increasing /proc/sys/fs/inotify/max_user_watches

Depending on the directory that you are watching, you might need to increase the max_user_watches to higher value using following command:

$ echo 65500 > /proc/sys/fs/inotify/max_user_watches

Once the synchronization complete, you can make some test by add more files in the source server, after 5 seconds you will see that the files has been synced to the destination directory. To make this daemon run automatically after boot, you can put the lsyncd command into /etc/rc.local.

5 thoughts on “Linux: Using lsyncd – Live Syncing (Mirror) Daemon

  1. “cat -” ? Why the added ‘-‘ mark? It seems redundant, given that cat(1) called with no options will open stdin.

    Reply

    1. Good question. I do not know that this one was already available in RPMforge during this writing.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *