Lsyncd is a free, open-source utility that can be downloaded and configured with no charge for the software or use.
Modern sysadmins use lsyncd for several scenarios such as:
- Load balancing — this works best when the traffic levels are relatively low (or intermittent), or new and modified content is not frequently accessed.
- High availability — keeping in mind that there are multiple aspects of high availability. Using lsyncd to push data to another host that can take over in the event of a hardware failure is an excellent use-case.
- Real-time offsite backups — a great way to keep a running record of the files and folders that have changed will ensure we push the changes to a second host for backup purposes.
Install EPEL Repo
The first step is to add the EPEL repository which contains the lsyncd package.
root@server ~]# yum -y install epel-release
If everything goes well, you will see a “Complete!” message. Then you need to make sure the EPEL repo is enabled.
Open the epel.repo file as follows:
[root@server ~]# vi /etc/yum.repos.d/epel.repo
Change the “enabled=0” to “enabled=1” as follows:
[epel] name=Extra Packages for Enterprise Linux 7 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Install Lsyncd
Proceed to install the lsyncd package using the following command:
[root@server ~]# yum -y install lsyncd
Configure SSH on Master
At this point we need to configure SSH on Master server so that it can push files to the slave/backup server without requiring password authentication or user intervention. To do so, we will create SSH keys on the master server as follows:
[root@server ~] # ssh-keygen -t rsa
Upon execution of the command above you will be prompted with several questions. You can use the defaults. When prompted to enter passphrase, hit Enter to proceed with empty passphrase.
root@alt [~]# ssh-keygen -t rsa -C "email@domain.local"
Once the SSH keys are generated, transfer the public key (the file ending with .pub) to the slave server. In this way, master server will authenticate with the slave without the need for password.
Transfer the ssh key with the following command:
[root@server ~]# ssh-copy-id email@domain.local
NOTE: It is normal if when using the above command you are prompted to authenticate via password. This is because the SSH key is not yet in place.
Before proceeding to the next step, verify that the passwordless authentication works. From the master server, try to ssh to the slave server as follows:
[root@server ~]# ssh email@domain.local
Configure Lsyncd on Master
We are ready to configure the Lsyncd on Master server. The settings we will modify are the following:
- Log files location
- Frequency to write status file
- Synchronization method
- Source folder (in master) we wish to sync
- Destination folder (in slave)
Firstly, open the lsyncd.conf file to start editing it.
root@server [~]# vi /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd-status.log",
statusInterval = 10
}
## Slave server configuration ##
sync {
default.rsync,
source="/var/www/html/",
target="IP:/var/www/html/",
rsync = {
compress = true,
acls = true,
verbose = true,
owner = true,
group = true,
perms = true,
rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
}
}
Now that Lsyncd is installed and configured, along with the SSH keys for password-less authentication, execute the following commands to start and enable the lsyncd service.
[root@server lsyncd]# systemctl start lsyncd
[root@server lsyncd]# systemctl enable lsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/lsyncd.service to /usr/lib/systemd/system/lsyncd.service
Verify Lsyncd is Working
Check both your master and slave directories (/var/www/html/) are empty.
[root@server ~]# cd /var/www/html
[root@server html]# ls -luah
total 0
[root@server html]#
[root@slave-server ~]# cd /var/www/html
[root@slave-server html]# ls -luah
total 0
[root@slave-server html]#
Create an empty file on the master server named index.html. You can quickly do so by using the touch command as follows:
[root@server html]# touch index.html
After 15 seconds, lsyncd will notice the changes and push the new file to the slave server. We can monitor the lsyncd log on the master server to verify the transfer has occurred, and what files were transferred across.
[root@server ~]# cd /var/log/lsyncd
[root@server lsyncd]# cat lsyncd.log
Tue Feb 22 09:02:18 2022 Normal: Rsyncing list
/
/index.html
Tue Feb 22 09:02:20 2022 Normal: Finished (list): 0
[root@server lsyncd]#
Now, check the /var/www/html/ directory on the slave server to confirm the new index.html file has been pushed successfully.
[root@slave-server ~]# ls -luah /var/www/html
total 1
-rw-r--r-- 1 root root 10 Feb 22 09:04 index.html
[root@slave-server ~]#