Install and configure Lsyncd

When configured, lsyncd is a replication service that will run on the “master” server to ensure the same data exists on all “slave” servers. The lsyncd process monitors the web content directory and replicates changes every few seconds to clone nodes.

Lsyncd is especially useful for synchronizing data from a secure area to a less secure area. To accomplish this, Lsyncd uses Rsync to automate the replication of files from one master server to one or more subordinate servers. Lsyncd transfers only files that have changed.

Complete the following steps to install and configure Lsyncd.


  • Two or more Linux servers
  • Ability to run the installation and configuration commands while logged in as the root user or from a super-user account
  • Public and private IP addresses for the master server
  • Public and private IP addresses for the subordinate servers

Install Lsyncd

  1. On the master server, run the following command:
    ssh-keygen -t rsa

    Two new files are created in ~/.ssh/ id_rsa and directories.

  2. Use SFTP to access the master server, copy the file to the /root/.ssh/ folder of each of your subordinate servers, and change the name of each copied file to
  3. On your subordinate servers, run the following command:
    cat ~/.ssh/ >> ~/.ssh/authorized_keys
  4. From the master server, SSH to log in to each subordinate server and choose Yes to accept the generated key.

    Now you can log in to the subordinate servers without a password.

Install Package Dependencies on the Master Server

Run the following command for your OS to install all needed dependencies:

For CentOS:

yum -y install lua lua-devel pkgconfig gcc asciidoc

For Ubuntu:

apt-get update && apt-get install -y lua5.1 liblua5.1-dev pkg-config rsync asciidoc

Install Lsyncd

Important: Be sure to manually check the destination directory on the subordinate server. If you have NFS or CloudFuse mount your Cloud Files, these could be deleted by the Lsyncd process.

Note: Lsyncd version 2.0.4 has a known issue with permissions. Ensure that you install either 2.0.7 (stable) or 2.1.5+ (latest). This procedure installs 2.1.5.

To install the version of Lsyncd required for this procedure, you need to compile the code from source. If you are unfamiliar with this process, you can copy and paste the following commands:

cd /var/tmp
tar xzvf lsyncd-2.1.5.tar.gz
cd lsyncd-2.1.5
./configure && make && make install

When the make install command finishes running, the Lsyncd service is installed on the master server. The compressed Lsyncd package does not come with startup scripts, so you must install your own.

Set Up the startup scripts

Perform the following steps for your OS.

For Ubuntu

  1. Create the following file at /etc/init/lsyncd.conf:
    description "lsyncd file synchronizer"
    			 start on (starting network-interface
       			 or starting network-manager
    			 or starting networking)
    			 stop on runlevel [!2345]
    			 expect fork
    			 respawn limit 10 5
    			 exec /usr/local/bin/lsyncd /etc/lsyncd.lua
  2. Create a symbolic link to preserve functionality when called as a deprecated init script.
    ln -s /lib/init/upstart-job /etc/init.d/lsyncd
  3. If you want to disable Lsyncd from starting automatically at startup (for example, if the customer is scaled down to one server), you can create the following file at /etc/init/lsyncd.override:

For Centos

  1. Create the following file at /etc/init.d/lsyncd. (Skip this step if you ran the yum install lsyncd command earlier.)
        	# 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
        	start() {
            	if [ -f $lockfile ]; then
                	echo -n $"$prog is already running: "
                	echo -n $"Starting $prog: "
                	daemon $lsyncd -pidfile $pidfile $config
                	[ $RETVAL = 0 ] && touch $lockfile
                	return $RETVAL
        	stop() {
            	echo -n $"Stopping $prog: "
            	killproc $lsyncd
            	[ $RETVAL = 0 ] && rm -f $lockfile
            	return $RETVAL
        	case "$1" in
                status $lsyncd
                echo "Usage: lsyncd {start|stop|restart|status}"
                exit 1
            exit $?
  2. Configure Lsyncd to start at boot:
    chkconfig lsyncd on


Configure log rotation


After Lsyncd has an assigned log area, it will create a log event if an error occurs during replication. Log files can become large and unruly when not properly monitored, causing a hard disk to fill up. To ensure this does not happen, add Lsyncd to logrotate.

Start by creating a directory to store the log files.

mkdir /var/log/lsyncd

Create a file at /etc/logrotate.d/lsyncd with the following shell script:

cat << EOF > /etc/logrotate.d/lsyncd
   	 	 /var/log/lsyncd/*log {
        	 if [ -f /var/lock/lsyncd ]; then
            	/sbin/service lsyncd restart > /dev/null 2>/dev/null || true

With this script enabled, the logs will be rotated instead of accumulating until they are manually deleted.


Create the configuration file


  1. On the master server, create a file at /etc/lsyncd.lua and insert the following data with a text editor.

    Note: Replace the target parameter with the private IP address of the subordinate server. You can find the IP address in the Rackspace Cloud Control Panel.

    settings {
    			    logfile = "/var/log/lsyncd/lsyncd.log",
    				statusFile = "/var/log/lsyncd/lsyncd-status.log",
    				statusInterval = 20
    			sync {
    				rsync = {
            			compress = true,
            			acls = true,
            			verbose = true,
            			rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no" }

    Take special note of the block of code beginning with sync. If you require more web servers, this block of code will must be replicated with the IP address of each additional subordinate server.

  2. With configuration complete, start the lsyncd service.
    service lsyncd start


Test the configuration


Before continuing, test the configuration.

  1. Create a file in the /var/www/ directory on the master server. You can do this with the touch command.
    touch /var/www/dummy_file

    If everything is working correctly, the file is replicated to the /var/www/ directory on the subordinate server.

  2. To verify that the file was replicated, use SSH to log in to the subordinate server and run the ls command.
    ls /var/www


Was this content helpful?

© 2015 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

See license specifics and DISCLAIMER