Lsyncd

The following guide will show how to do a basic install and configuration of lsync for a master-slave configuration.

With this configuration lsync will sync the files every 20 milliseconds.

Quick Links:
Configuring ssh keys
Master server installations
Configuring /etc/init.d/lsyncd
Configuring sync directories and slave servers in lsyncd.lua

 

Slave server = x.x.x.x

Key Configuration

There are two parts to the key configuring, one part for Master and one step for the Slave.

Key config Master server (step 1)

From the master server you will need to run the following:

ssh-keygen -t rsa

This should then create two new files in ~/.ssh/ id_rsa and id_rsa.pub.

You should secure copy the id_rsa.pub to /root/.ssh/ directory on each of the slave servers. You can do this via scp with the following command:

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

Key config Slave server (step 2)

Once you have copied the files over to /.ssh/master.pub on the slave server you will now need to run the following command on the slave:

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

Once this has been performed you should test the keys by logging in.

From the MASTER server run the following command:

ssh [email protected]

Note: you should not be prompted for a password if the keys have been configured correctly

 

Master Installation

You should install the dependencies with:

yum -y install lua lua-devel pkgconfig gcc asciidoc

You should then make an lsync directory and get the tar file for lsync with the following command:

mkdir lsync_src; cd lsync_src; wget http://lsyncd.googlecode.com/files/lsyncd-2.1.4.tar.gz

Extract with:

tar zxf lsyncd-2.1.4.tar.gz; cd lsyncd-2.1.4

Then run the following to install:

export CFLAGS=”-march=native -O2″

./configure && make && make install

To run lsync you will need to create a log directory with the following command:

mkdir /var/log/lsyncd

 

Lsync init.d configuration

You will need to create the following location /etc/init.d/lsyncd and then enter the following information into the file

#!/bin/bash

#

# lsyncd: Starts the lsync Daemon

#

# chkconfig: 345 99 90

# description: Lsyncd uses rsync to synchronize local directories with a remote

# machine running rsyncd. Lsyncd watches multiple directories

# trees through inotify. The first step after adding the watches

# is to, rsync all directories with the remote host, and then sync

# single file buy collecting the inotify events.

# processname: lsyncd

. /etc/rc.d/init.d/functions

config="/etc/lsyncd.lua"

lsyncd="/usr/local/bin/lsyncd"

lockfile="/var/lock/subsys/lsyncd"

pidfile="/var/run/lsyncd.pid"

prog="lsyncd"

RETVAL=0

start() {

if [ -f $lockfile ]; then

echo -n $"$prog is already running: "

echo

else

echo -n $"Starting $prog: "

daemon $lsyncd -pidfile $pidfile $config

RETVAL=$?

echo

[ $RETVAL = 0 ] && touch $lockfile

return $RETVAL

fi

}

stop() {

echo -n $"Stopping $prog: "

killproc $lsyncd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f $lockfile

return $RETVAL

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

status)

status $lsyncd

;;

*)

echo "Usage: lsyncd {start|stop|restart|status}"

exit 1

esac

exit $?

You will then need to run the following commands to make sure it has the right permissions:

chmod 775 /etc/init.d/lsyncd

chown root:root /etc/init.d/lsyncd

 

Configuring sync directories

You should create the following location /etc/lsyncd.lua and paste the following:

settings {

logfile = "/var/log/lsyncd/lsyncd.log",

statusFile = "/var/log/lsyncd/lsyncd-status.log",

statusInterval = 20

}

--FIRST SLAVE

sync {

default.rsync,

source="/var/www/",

target="x.x.x.x:/var/www/",

rsync = {

compress = true,

acls = true,

verbose = true,

rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"

}

}

You should change ‘source‘ to the directory you wish to sync and ‘target‘ to the ip address of the slave server configured above and the sync target.

 

You should now start lsyncd with the following command and then installation should be complete:

service lsyncd start

Done! You can now test the configuration by creating a file on the master and then checking the location on the slave.