Friday, 23 October 2015

lsyncd to sync two directories

I have to sync two directories, from production to DR server. For that first choice was to use rsync command only, but rsync command has to be run manually or scheduled using cron. But there is a tool lsyncd that I have been using from a long time to get away from manual sync or scheduled sync. Lsyncd watches a local directory trees event monitor interface and perform certain actions when files change. lsyncd waits for few seconds and aggregates the change events and runs one or more processes to synchronize the changes.
Note: For this document, I am using Ubuntu 14.04
Installing lsyncd is very easy:
apt-get install lsyncd
For security requirements, I want to run the lsyncd as non-root user and sync the folders as a normal user (say admino). I am going to sync the folder /app/node/web from my production server (source) to my DR server (destination).
Login to production server as admino user and generate ssh keys without password (I am going to use passwordless ssh authentication)
$ ssh-keygen -t rsa
Copy the key to the DR server
$ ssh-copy-id
We are ready with passwordless ssh authentication, now I will configure lsyncd.
As I want to run the lsyncd daemon as the admino user (using which I will sync), I am going to change the /etc/init.d/lsyncd script
I changed the PID location
Next I specified that lsyncd daemon should be started as admino user, for that I have added the start-stop-daemon option --chuid
start-stop-daemon --chuid admino:admino --start --quiet --pidfile $PIDFILE --exec $DAEMON \
--test > /dev/null \
|| return 1
start-stop-daemon --chuid admino:admino --start --quiet --pidfile $PIDFILE \
--nicelevel $NICELEVEL --exec $DAEMON -- \
|| return 2
Create log directory for lsyncd
mkdir /var/log/lsyncd
Create the log files
touch /var/log/lsyncd/lsyncd.{log,status}
Now I will create the configuration file of lsyncd, create the config directory
mkdir /etc/lsyncd

Note: In Ubuntu, the default location for config file is /etc/lsyncd/lsyncd.conf.lua. It is specified in /etc/init.d/lsyncd script:
Create the config file /etc/lsyncd/lsyncd.conf.lua with contents like:
settings = {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
sync {
source = "/app/node/web",
host = "",
targetdir = "/app/node/web"
That’s it and we are done. For this example, I have shown the simplest lsyncd configuration, there are lots of options available with lsyncd, visit lsyncd home page for details.