×


Sync Two CentOS 8 Servers Using File Replication

Need to Sync Two CentOS 8 Servers Using File Replication?

This tutorial is for you.


Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to do syncing of Centos servers.

In this context, we shall look into the steps to perform this CentOS task.


Basic Configuration required for Syncing Two CentOS 8 Servers Using File Replication

You can Synchronize data between two CentOS 8 servers running Apache is done using Lsyncd.

To do this, follow the steps below;


1. Before setting up Lsyncd we have to add the software repository that contains the Lsyncd package.

The following command can be used to add EPEL Repo:

root@alt ~]# yum -y install epel-release

The installation will take a moment, and we will see a decent amount of output. Once it says “Complete!” we can move on.

[root@alt ~]# yum -y install epel-release
Loaded plugins: fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
1 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================
Package Arch Version Repository Size
=====================================================================
Installing:
epel-release noarch 7-11 system-extras 15 k
Transaction Summary
=====================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!


To check whether the repository we set up is enabled or not we can use the following command:

[root@alt ~]# vim /etc/yum.repos.d/epel.repo

After this ensure that the repo is set to enabled=1.

[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


2. Then Install the Lsyncd Software

Now we can install the Lsyncd software using the following command.

root@alt ~]# yum -y install lsyncd

This process will take a few moments to complete, and then we can continue the set up once we see “Complete!”.

[root@alt ~]# yum -y install lsyncd
Loaded plugins: fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 16 kB 00:00:00
* epel: mirrors.liquidweb.com
epel | 5.4 kB 00:00:00
(1/3): epel/x86_64/group_gz | 90 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/3): epel/x86_64/primary_db | 6.9 MB 00:00:00
166 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package lsyncd.x86_64 0:2.2.2-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================
Package Arch Version Repository Size
======================================================================
Installing:
lsyncd x86_64 2.2.2-1.el7 epel 83 k
Transaction Summary
======================================================================
Install 1 Package
Total download size: 83 k
Installed size: 227 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/lsyncd-2.2.2-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY] 0.0 B/s | 0 B --:--:-- ETA
Public key for lsyncd-2.2.2-1.el7.x86_64.rpm is not installed
lsyncd-2.2.2-1.el7.x86_64.rpm | 83 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-11.noarch (@system-extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lsyncd-2.2.2-1.el7.x86_64 1/1
Verifying : lsyncd-2.2.2-1.el7.x86_64 1/1
Installed:
lsyncd.x86_64 0:2.2.2-1.el7
Complete!


3. After that configure SSH on Master

To ensure the master host can push files to the slave hosts without requiring user intervention we will configure SSH on Master

We can create the SSH key using the following command:

root@host # ssh-keygen -t rsa (or)
root@host # ssh-keygen -t rsa -b 4096 -C "$(whoami)@$(hostname)-$(date -u +%Y-%m-%d-%H:%M:%S%z)"

We can use the second command to generate a stronger key.


Here, we will use the defaults and no added password.
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+3vxiIsrkrwikDaE1UlTi8vr0g/wOfwSowCsw user@domain.com-2019-08-21-13:14:45+0000
The key's randomart image is:
+---[RSA 4096]----+
|. . . . |
| = o . . |
|o.= . . . . |
|oDo . . . . . |
|B. .. Y . |
|O+.. o . . |
|O++.o o . . . |
|=*. . ... . . o. |
|.o.=+.++. . . |
+----[SHA256]-----+

After the SSH keys are generated, we will transfer the public key over to our slave host so that it will allow us to authenticate and access that host without needing to enter a password.


We can transfer the key using the following command:

root@alt [~]# ssh-copy-id root@opt.thisisnotadomain.com


To ensure that lsyncd can utilize the SSH keys we created, we need to modify the SSH config file on the master to add a few snippets of information.

root@alt [~]# vim ~/.ssh/config

Edit the config file using vim, and add the following into it.

Host dest_host
Hostname 172.16.144.32
User root
IdentityFile ~/.ssh/id_rsa

How to perform Synchronizing using Lsyncd ?

To Sync Two CentOS 8 Servers Using File Replication with Lsyncd we need to follow the below steps:


Configure Lsyncd on Master

Next, we need to modify the Lsyncd configuration file. We will need to specify the following settings:

Edit the config file using the vim command.

root@alt [~]# vim /etc/lsyncd.conf

Then we need to modify the configurations

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/",
target="IP:/var/www/",
rsync = {
compress = true,
acls = true,
verbose = true,
owner = true,
group = true,
perms = true,
rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
}
}

After making the edits, run the following commands to start and enable the service.

[root@alt lsyncd]# systemctl start lsyncd
[root@alt lsyncd]# systemctl enable lsyncd

Created symlink from /etc/systemd/system/multi-user.target.wants/lsyncd.service to /usr/lib/systemd/system/lsyncd.service.


We can see that Lsyncd is running,  now we need to verify that it is monitoring the folder for any changes in the status log.

root@alt [~]# cd /var/log/lsyncd
[root@alt lsyncd]# tail lsyncd.status
Lsyncd status report at Thu Feb 6 10:07:37 2020
Sync1 source=/var/www/html/
There are 0 delays
Excluding:
Nothing.
Inotify watching 1 directories
1: /var/www/html/

From the logs we can see that it is being synced from the master to the slave server.

Since we have the Lsyncd service running, it will start when the system is rebooted and continuously monitor any changes and push the changes across to the slave node.


[Need urgent assistance to perform CentOS related tasks? We can help you!]


Conclusion

The #sync command forces an immediate write of all cached data to disk. Run sync if you anticipate the system to be unstable, or the storage device to become suddenly unavailable, and you want to ensure all data is written to disk. Individual files may be synced, or the entire filesystem containing the specified files.

This article will guide you on the right steps to sync two #CentOS 8 #servers using file replication.