Skip to main content

How to Synchronize Multiple Linux Server with lsyncd

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

Comments

Popular posts from this blog

Cara Disable Antimalware Service Executable di Windows 10

Disadari atau tidak, Windows 10 (dan juga windows-windows lainnya) hadir dengan banyak sekali aplikasi bloatware (aplikasi yang tidak perlu-perlu amat dimiliki oleh end user). Contohnya, adalah aplikasi yang seharusnya sudah tergantikan fungsinya oleh antivirus, seperti Antimalware Service Executable . Aplikasi ini dicurigai membuat Windows 10 mengalami inefisiensi memori/RAM, memakan resource yang tinggi, dengan Load yang tinggi (tanpa limit terkadang). Nah, berikut adalah cara men-disable nya: Tekan tombol Windows + I untuk membuka apliaksi Windows Setting. Pilih icon menu Update and Security Pilih lagi menu disamping kiri Windows Security Pada jendela baru yang muncul, ada pilihan Virus & Threat protection Klik ini Lalu matikan proses Real-time protection tersebut. Dengan Regedit. Buka dialog regedit, Windows + R dan ketik ‘regedit’ Cari Folder regedit ini HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender Buat sebuah DWORD baru dengan klik kanan

Setup Debian 11 Official Repository In sources.list (/etc/apt/sources.list)

When you install Debian 11 using a DVD, the OS installer sets the DVD as the source for getting packages for your system if you didn't choose to scan for network mirrors. Due to this reason, the system would ask you to insert a DVD if the disc is not present in the DVD drive when you try to install any software.  Also, the packages on DVD may not be the latest release.  So, we need to get the packages from the Internet. Media change: please insert the disc labeled 'Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04' in the drive '/media/cdrom/' and press [Enter] The /etc/apt/sources.list file with DVD as a source would look something like below. # deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb cdrom:[Debian GNU/Linux 11.0.0 _Bullseye_ - Official amd64 DVD Binary-1 20210814-10:04]/ bullseye contrib main deb http://security.debian.org/debian-security bullseye-security

How to Install Traefik in Debian (Without Docker)

 Download the suitable version of Traefik for your system from here: https://github.com/traefik/traefik/releases as an example, we'll be download the linux 64 bit version: wget https://github.com/traefik/traefik/releases/download/v2.8.7/traefik_v2.8.7_linux_amd64.tar.gz extract those package: tar -xzvf traefik_v2.8.7_linux_amd64.tar.gz set the traefik as executable and move the traefik binary to linux bin folder