How to auto sync modified file to another server using Lsyncd on Centos 7

How to auto sync modified file to another server using Lsyncd on Centos 7

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents).

It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes.

By default this is rsync.

Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or block devices and does not hamper local filesystem performance.

Rsync+ssh is an advanced action configuration that uses a SSH to act file and directory moves directly on the target instead of re-transmitting the move destination over the wire.

Fine-grained customization can be achieved through the config file.

Custom action configs can even be written from scratch in cascading layers ranging from shell scripts to code written in the Lua language.

This way simple, powerful and flexible configurations can be achieved.

Lsyncd is open source and you can view all feature and contribute on https://github.com/axkibe/lsyncd

How to Install Lsyncd on Centos 7

To install Lsyncd on Centos 7, first you need to enable EPEL Repository On CentOS 7 / RHEL 7

For CentOS/RHEL 7 Only
[root@serverdiary ~]# yum install epel-release

If command above does not work or CentOS 6 / RHEL 6 you can manually install with:

For CentOS/RHEL 7
[root@serverdiary ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

For CentOS/RHEL 6
[root@serverdiary ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

Then install Remi repository with command:

[root@serverdiary ~]# yum install lua lua-devel pkgconfig gcc asciidoc
[root@serverdiary ~]# yum install lsyncd

Configuration file is on /etc/lsyncd.conf

Example: We want to sync the folder “/var/www/html/serverdiary.com” from Master server to Slave server

Master Server's IP = 192.168.10.5
Slave Server’s IP = 192.168.10.6
Directory to be sync = /var/www/html/serverdiary.com

Enable Key based authentication between Master and Slave Server.

Login to Master server & generate the public and Private keys using ssh-keygen command.

[root@serverdiary ~]# ssh-keygen

Then the public key from Master to Slave using ssh-copy-id command

[root@linuxtechi ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.10.6

First you must login from Master server to Slave server before start lsyncd, or you will get error below on lsyncd

Error: Temporary or permanent failure on startup of /var/www/html/serverdiary.com/ -> 192.168.10.6:/var/www/html/serverdiary.com/. Terminating since "insist" is not set.

Example configuration of /etc/lsyncd.conf

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
-- 
settings{
	logfile = "/var/log/lsyncd/lsyncd.log",
	statusFile = "/var/log/lsyncd/lsyncd.stat",
	statusInterval = 2,
}
-- 192.268.10.6
sync{
	default.rsync,
	source="/var/www/html/serverdiary.com",
	target="192.168.10.6:/var/www/html/serverdiary.com",
	rsync={
		compress = true,
		verbose = true,
		update = true,
		perms = true,
		owner = true,
		group = true,
		rsh ="/usr/bin/ssh -l root -i /root/.ssh/id_rsa",
	}
}

Enable lsyncd service and start lsyncd service

[root@serverdiary ~]# systemctl enable lsyncd
[root@serverdiary ~]# systemctl start lsyncd

Check status on /var/log/lsyncd/lsyncd.log with command

[root@serverdiary ~]# tail -f /var/log/lsyncd/lsyncd.log

Example output

Sat May 30 19:22:51 2020 Normal: --- Startup ---
Sat May 30 19:22:51 2020 Normal: recursive startup rsync: /var/www/html/serverdiary.com/ -> 192.168.10.6:/var/www/html/serverdiary.com/
Sat May 30 19:22:51 2020 Normal: Startup of /var/www/html/serverdiary.com/ -> 192.168.10.6:/var/www/html/serverdiary.com/ finished.
Sat May 30 19:22:53 2020 Normal: Calling rsync with filter-list of new/modified files/dirs
/rss/latest.xml
/index.php
Sat May 30 19:22:56 2020 Normal: Finished a list after exitcode: 0

Leave a Reply

Your email address will not be published. Required fields are marked *