Real-time file synchronization using Lsyncd under CentOS7

Real-time file synchronization using Lsyncd under CentOS7

A brief introduction to Lsyncd

Lsyncd uses the file system event interface (inotify or fsevents) to monitor local files and directories for changes.

Lsyncd collates these file events within a few seconds and then spawns one or more processes to synchronize the changes to the remote filesystem. The default synchronization method used is rsync. Therefore, Lsyncd is a lightweight real-time mirroring solution.

Lsyncd is relatively easy to install and requires no new filesystems or block devices. Lysncd does not affect the performance of the local file system. As an alternative to rsync, Lsyncd can also push changes via rsync+ssh. rsync+ssh allows for more efficient synchronization when files or directories are renamed or moved to new locations in the local tree. (In contrast, plain rsync performs moves by deleting old files and then retransferring the entire file.)

Lsyncd 2.2.1 requires rsync >= 3.1.

Official documentation:

https://lsyncd.github.io/lsyncd/

The following introduces the use of Lsyncd to realize real-time file synchronization under CentOS7

Source server A: 192.168.31.230
Target server B: 192.168.31.60
Requirement: Synchronize the files under the /var/log/nginx/ directory on server A to server B in real time, and exclude error.log*

1. Install lsyncd on server A

The premise is that the EPEL source is configured

yum install lsyncd

insert image description here

2. Configure the password-free login from server A to server B

ssh-keygen -t rsa -b 4096
ssh-copy-id -p 8122 [email protected]
ssh -p 8122 [email protected]

insert image description here

(Click on the picture to enlarge it)

3. Configure /etc/lsyncd.conf

mv /etc/lsyncd.conf /etc/lsyncd.conf_default
touch /etc/lsyncd_exclude.lst
vi /etc/lsyncd.conf

Add the following content

settings {
    
    
   logfile    = "/var/log/lsyncd/lsyncd.log",
   statusFile = "/tmp/lsyncd.status",
   insist = true,
   inotifyMode = "Modify",
   statusInterval = 1
}
sync {
    
    
   default.rsyncssh,
   source="/var/log/nginx/",
   host="192.168.31.60",
   targetdir="/data/logs_backup",
   excludeFrom="/etc/lsyncd_exclude.lst",
   maxDelays = 5,
   delay = 0,
   rsync = {
    
    
     archive = true,
     compress = false,
     -- verbose   = true
   },
   ssh = {
    
    
     port = 8122
   }
}

To exclude error.log*

vi /etc/lsyncd_exclude.lst 
error.log*

4. Configuration file check test

Run lsyncd -nodaemon /etc/lsyncd.conf to see if there is an error, if there is an error, check the configuration file according to the error

lsyncd -nodaemon /etc/lsyncd.conf

As shown in the figure below, when curl http://192.168.31.230 triggers access.log changes, synchronization will be triggered
insert image description here

(Click on the picture to enlarge it)

If there is no error report and exit directly, you can use systemctl start lsyncd to start lsyncd

5. Set it to run in the background of the service

systemctl enable lsyncd.service
systemctl start lsyncd.service
tail -f /var/log/lsyncd/lsyncd.log

(Click on the picture to enlarge it)

The above is the process of using Lsyncd to realize automatic file synchronization

Reposted from: https://cloud.tencent.com/developer/article/2040991

Guess you like

Origin blog.csdn.net/Chen118222/article/details/128241089