NEW VPS PLANS

Experience Ultra-high Performance of NVMe Storage on New UK VPS Plans.

Deploy Instantly
  • +357 2425 0808
  • Login
  • English

Home

Blog

How To Sync Multiple Linux Servers with ...

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 · Technical 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.

Press Releases
72

Free VPS Trial

No Credit Card Required.

Recent Posts

How To Migrate from MongoDB Atlas to self-hosted Ubuntu 22.04 Server

How To Migrate from MongoDB Atlas to self-hosted Ubuntu 22.04 Server

15 April, 2024

How NetShop ISP Improves Trading Infrastructure Resilience through Equinix LD7 Data Center Hosting

How NetShop ISP Improves Trading Infrastructure Resilience through Equinix LD7 Data Center Hosting

21 March, 2024

Introducing New Cutting-Edge VPS Plans: OKTAPLUS, HYPER, and TITAN Enhanced with NVMe Technology

Introducing New Cutting-Edge VPS Plans: OKTAPLUS, HYPER, and TITAN Enhanced with NVMe Technology

12 March, 2024

How To Install Let’s Encrypt SSL on Ubuntu Server 22.04 for Apache or Nginx

How To Install Let’s Encrypt SSL on Ubuntu Server 22.04 for Apache or Nginx

04 March, 2024

Navigating Forex Server Hosting: Key Distinctions from Conventional Providers

Navigating Forex Server Hosting: Key Distinctions from Conventional Providers

23 February, 2024

#letushostyou

Award Winning Hosting Provider established in 2004.

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

Products

Bare Metal Servers

Customized Servers

Virtual / Cloud Servers

Forex VPS

Storage VPS

cPanel Web Hosting

Reseller Web Hosting

Colocation

Addons

Premium DNS

Email Hosting

Cloud Backup

DDoS Protection

Licenses

SSL Certificates

Domain Names

Premium SLAs

About Us

Data Center Locations

Looking Glass

Our Company

Contact Us

Careers in Cyprus

Become a Partner

Awards

Certifications

© 2024 S.S. NetShop Internet Services Ltd. All rights reserved.  Terms & Conditions  |  Privacy Policy
CY Reg. Number: HE 217340 | EU VAT Number: CY10217340J

Visa
Mastercard
PayPal
Bitcoin
Tether
Ethereum
Litecoin
Wise
Revolut
Wire Transfer