CopyDisable

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 192.168.10.241 (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 192.168.10.241
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
PIDFILE=/home/admino/$NAME.pid
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 -- \
$DAEMON_ARGS \
|| 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:
CONFIG=/etc/lsyncd/lsyncd.conf.lua
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 {
default.rsyncssh,
source = "/app/node/web",
host = "192.168.10.241",
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 https://github.com/axkibe/lsyncd for details.


No comments:

Post a Comment