Lsyncd is awesome tool to make backup of home files or other directory we define to another server. It duplicates files with rsync. When there are changes or new files in home directory of master server, lsyncd will automatically in real time duplicates files to slave or subordinate server.
Here I share how to install and configure Lsyncd with 2 servers:
Ingredients
- Two servers with two IP Addreses (Server A and Server B)
- Linux OS: Centos 6 or 7.
- Software SSH tool (Putty or Bitvise SSH).
Paswordless SSH
On master server (Server A), input this command:
ssh-keygen -t rsa
It will creates file with extension .pub in directeory /root/.ssh/. Usually named with id_rsa.pub.
Copy id_rsa.pub from /root/.ssh with SFTP and paste it to Slave Server (Server B) into directory /root/.ssh.
After paste the .pub file, run this command in the Slave Server:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
From Master Server do login to Slave server with SSH command:
ssh [email protected] (change the IP with slave server IP).
Choose Yes to generate keys, and now Server A can do SSH login to Server B without password.
Install Lsyncd
Still in Master Server, do install dependencies:
yum -y install lua lua-devel pkgconfig gcc asciidoc
Install lsyncd:
cd /var/tmp
wget http://lsyncd.googlecode.com/files/lsyncd-2.1.5.tar.gz
tar xzvf lsyncd-2.1.5.tar.gz
cd lsyncd-2.1.5
./configure && make && make install
Configurate Lsyncd
We need to make new file to configurate Lsyncd:
nano /etc/init.d/lsyncd
Paste this code:
#!/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 $?
Give executable permission:
chmod 775 /etc/init.d/lsyncd
Create log rotation directory:
mkdir /var/log/lsyncd
Configure log rotation file:
nano /etc/logrotate.d/lsyncd
Paste this code:
cat << EOF > /etc/logrotate.d/lsyncd
/var/log/lsyncd/*log {
missingok
notifempty
sharedscripts
postrotate
if [ -f /var/lock/lsyncd ]; then
/sbin/service lsyncd restart > /dev/null 2>/dev/null || true
fi
endscript
}
EOF
Then, make main configuration to sync files:
nano /etc/lsyncd.lua
Paste this code:
settings {
statusFile = "/tmp/lsyncd.stat",
statusInterval = 1,
pidfile = "/var/run/lsyncd.pid",
logfile = "/var/log/lsyncd/lsyncd.log",
}
sync {
default.rsync,
source="/var/www",
target="ip-10-10-10-10:/var/www",
rsync = {
compress = true,
acls = true,
verbose = true,
owner = true,
perms = true,
group = true,
rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
}
}
Note: Do not forget to change source and destination directory to your home directory and IP Address of your Slave Server.
Start Lsyncd
/etc/rc.d/init.d/lsyncd start
Use this command to lsyncd run on startup:
chkconfig lsyncd on
Now, do test with creating new files in Master Server:
nano /var/www/test.txt
And input some text. And finish, Server A will automatically duplicate test.txt to Server B. You can check it via SFTP.**
Comments