PRE-ORDER

Dedicated Servers at Equinix London LD6

Inquire Today
How To Sync Multiple Linux Servers with Lsyncd

How To Sync Multiple Linux Servers with Lsyncd

NetShop ISP

NetShop ISP · Blog Author

Feb 22, 2022 · Tutorials

In an era when most businesses conduct online transactions, servers down time and network disruptions cause a major impact. The more visitors a website has, the more robust and bulletproof its underlying online infrastructure must be.

In the past few years we have witnessed large hosting and cloud providers going down due to cyber attacks and fire incidents. Business owners and IT managers need to ensure that the right disaster recovery and business continuity plans are in place to overcome potential threats that may impact their business.

Infrastructure horizontal scaling is a commonly used practice that increases the ability of a website to handle a significant amount of traffic and protect it against hardware failure of a standalone web or database server. Horizontal scaling is often implemented using cloud or dedicated servers in different hosting providers across multiple regions in order to eliminate as many single point of failures (SPoF) as possible.

Ensuring that all web servers have the same data is a challenging task as, otherwise, you will end up with missing or corrupted data when a backup server takes the primary role upon a disaster.

In this article we will demonstrate how to automate files and data synchronization using lsyncd; a popular and free open-source software.

What is Lsyncd

Lsyncd is a free, open-source utility that can be downloaded and configured with no charge for the software or use.
It’s setup is simple as you only need to install a single package. Comprised of reliable technology, rsync and ssh, lsyncd makes the perfect utility for synchronizing data across two or more servers.

Please note that lsyncd is not real-time. It pushes changes every 15 seconds. This value can be modified via the configuration file if needed.

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.

Lsyncd Configuration on Linux CentOS

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.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rCwcRH+3oop3kMhAehxmmNzi8vr0g/wOfwSowCsw user@server.localhost-2022-02-22-08:14:45+0000
The key's randomart image is:
+---[RSA 4096]----+
|. . . . |
| = o . . |
|o.= . . . . |
|oDo . . . . . |
|B. .. Y . |
|O+.. o . . |
|O++.o o . . . |
|=*. . ... . . o. |
|.o.=+.++. . . |
+----[SHA256]-----+
root@alt [~]#

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 root@slave-server.sampledomain.com

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 root@slave-server.sampledomain.com

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 ~]#

Congratulations! You have now configured lsyncd on your two Linux servers for automatic file directories synchronization.

Related Articles

Categories

Backup
6
Business Continuity
4
Cloud Hosting
9
Colocation
4
Company Updates
1
Cryptocurrencies
1
Data Centers
12
Dedicated Servers
4
Disaster Recovery
3
Forex
24
Gaming
1
iGaming
33
Industry News
31
Interviews
8
Joomla
9
Magento
5
Managed Services
1
Press Releases
58
Product News
6
Resellers
1
Security
24
Software & Apps
1
Streaming
1
Tech Startups
12
Tutorials
50
Uncategorized
389
Vps Hosting
11
Web Design & Development
3
Web Hosting
43
Webinars
1
Wordpress
14

Free VPS Trial

No Credit Card Required.

Recent Posts

NetShop ISP CEO Shortlisted at Fazzaco Hall of Fame 2022

NetShop ISP CEO Shortlisted at Fazzaco Hall of Fame 2022

21 November, 2022

How To Install MariaDB 10.5, Apache 2.4, PHP 7.4 (LAMP) on Debian 11 Server

How To Install MariaDB 10.5, Apache 2.4, PHP 7.4 (LAMP) on Debian 11 Server

09 November, 2022

NetShop ISP Expand in Two New Region Hubs

NetShop ISP Expand in Two New Region Hubs

08 November, 2022

How To Resolve Acronis Agent error related to “snumbd26” kernel module on Linux Plesk/cPanel Servers

How To Resolve Acronis Agent error related to “snumbd26” kernel module on Linux Plesk/cPanel Servers

07 November, 2022

How to Enable SSH on Debian 11 Bullseye Linux Server

How to Enable SSH on Debian 11 Bullseye Linux Server

02 November, 2022

Products

Bare Metal Dedicated Servers

Virtual Servers (VPS / VDS)

Rack Space & Colocation Hosting

Fast Web Hosting

Cloud Backup

Premium DNS Hosting

Resources

Blog

Videos

Events

APIConnect

WhitepapersNew

Looking Glass

Company

About Us

CareersNew

Awards & Honours

Become a Partner

Contact Us

#letushostyou

Award Winning Hosting Provider with a Customer-centric approach to Business and an Open Source Mindset regarding Solutions and Products Development.

Est. 2004, Larnaca – Cyprus


About

Legal Address: 2A, Marathonos, Livadhia, 7060, Cyprus

Operations Office: 120, Faneromenis Avenue, Imperial Tower, 2nd Floor, Larnaca, 6031, Cyprus

Company Reg: HE 217340
EU VAT Number: CY10217340J
ISO 9001:2015: No. QS.21.038

International Phone: +357 2425 0808