Task:
Keep multiple web server’s htdocs files in sync.
One way to do it is to write a script as cronjob which synchronizes the main server’s configuration directories to all other servers.
But there are more elegant ways to do it:
GlusterFS and lsyncd. This article relates to the later: lsyncd
This deamon process uses the special kernel’s feature called ‘inotify’ to find out if any files within a certain directory have changed, been removed or created and synchronizes then only those files to the mirror directory in other web servers.
Procedure:
You can always use the Debian’s standard package but it is out of date(ver.1.34-1). The latest lsyncd is version 2.0.7 and offer many advantages compared with the Debian one.
Compile it:
apt-get install lua5.1 liblua5.1-0-dev rsync gcc pkg-config
cd /tmp
wget http://lsyncd.googlecode.com/files/lsyncd-2.0.7.tar.gz
tar fvxz lsyncd-2.0.7.tar.gz
cd lsyncd-2.0.7
./configure
make && make install
The files will then be located at:
Program: /usr/local/bin/lsyncd
Examples and docs: /usr/local/share/doc/lsyncd/*
To get the man page: man lsyncd
The full manual can be found at : https://github.com/axkibe/lsyncd/wiki/Manual-to-Lsyncd-2.0.x
Configure it:
Although for simple tasts the command line arguments can be used to start the daemon, it is recommended to use the configuration file for more elaborate tasks which you need to provide on the command line. eg.
lsyncd /etc/lsyncd.conf
Here is an example of configuration file:
-- general settings
settings = {
logfile = "/tmp/lsyncd.log",
statusFile = "/tmp/lsyncd.status",
nodaemon = false,
maxDelays = 900,
maxProcesses = 6,
}
-- synchronization settings 1
sync{
default.rsyncssh,
source="/path/on/source/",
host="hostnam.target.server.tld",
targetdir="/path/on/target/",
rsyncOpts = {"-vazc", "--numeric-ids", "--bwlimit=10000"}
}
-- synchronization settings 2
sync{
default.rsyncssh,
source="/path/on/source2/",
host="hostnam.target.server.tld",
targetdir="/path/on/target2/",
rsyncOpts = {"-vazc", "--numeric-ids", "--bwlimit=10000"}
exclude="SomeExcludedFileName"
}
Note: The ‘rsyncOpts‘ example above will be given to rsync with spaces between them but must be written here in this special fashion.
Starting lsyncd
lsyncd can be started in multiple ways here are some suggestions:
Starting the daemon manually:
/usr/local/bin/lsyncd /etc/lsyncd.conf
Starting at boot time using cron job:
@reboot /usr/local/bin/lsyncd /etc/lsyncd.conf
Starting at boot time using an init script:
Content of init script /etc/init.d/lsyncd
(copied form this site. Thanks you author:)
http://www.thisisnotsupported.com/lsyncd/
#!/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.conf"
lsyncd="/usr/local/bin/lsyncd"
lockfile="/var/lock/lsyncd"
prog="lsyncd" RETVAL=0
RETVAL=0
start() {
if [ -f $lockfile ]; then
echo -n $"$prog is already running: "
echo
else
echo -n $"Starting $prog: "
$lsyncd $config
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $lockfile
return $RETVAL
fi
}
stop() {
echo -n $"Stopping $prog: "
killall $lsyncd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $lockfile
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: lsyncd {start|stop|restart}"
exit 1
esac
exit $?
Make the script runnable:
chmod 755 /etc/init.d/lsyncd
Make sure it start at boot time(Debian):
ln -s /etc/init.d/lsyncd /etc/rc2.d/S20lsyncd
Troubleshooting
If lsyncd doesn’t want to start, then check in the log file. In our case the log file is /var/log/lsyncd.log
If you encounter a last line like:
Tue Oct 30 18:59:40 2012 Error: Terminating since out of inotify watches.
Tue Oct 30 18:59:40 2012 Error: Consider increasing /proc/sys/fs/inotify/max_user_watches
Then do one of the followings:
To change immediately the limit, run:
# echo 32768 > /proc/sys/fs/inotify/max_user_watches
To make the change permanent, edit the file /etc/sysctl.conf and add this line to the end of the file:
fs.inotify.max_user_watches=32768
After a crash of the start you should delete the lock file as follows:
rm /var/lock/lsyncd
To check if it runs, run the command:
root@ado:~# ps ax | grep lsyncd
12635 ? Ss 0:00 /usr/local/bin/lsyncd /etc/lsyncd.conf
12647 pts/2 S+ 0:00 grep lsyncd