如何在 AlmaLinux 上安装和配置 LsyncD

LsyncD(实时同步守护程序)用于在特定时间间隔后本地和远程同步或复制文件和目录。 LsyncD 在后端使用 rsync 和 ssh。

LsyncD 适用于主从架构。 它将监视主服务器上的目录。 如果主服务器上发生任何更改或修改,LsyncD 将在特定时间间隔后将它们复制到其从服务器上。 这些频繁的目录更新和同步操作确保主从目录保持相同。

在本文中,我们将讨论如何在 AlmaLinux 服务器上安装和配置 LsyncD。

要求

  • 主服务器和从服务器。
  • 操作系统和版本:AlmaLinux OS 8.5。
  • 处理器:1.1 GHz。
  • RAM:至少 1 GB,但建议使用 2 GB。
  • 磁盘空间:至少 20 GB,但建议 40 GB。
  • 架构:64 位。
  • 软件:LsyncD 和 SSHD。
  • 根访问您的 Linux 系统或通过 sudo 命令。

如何在 AlmaLinux 上安装和配置 LsyncD

要继续安装和配置,您需要两台安装了 AlmaLinux 的服务器 – 一台用于主服务器(IP:192.168.1.20) example) 和一台从属服务器(IP: 192.168.1.21 在此 example)。

第 1 步:在主服务器和从服务器之间启用基于密钥的身份验证。

从主服务器,使用 ssh-keygen 命令生成公钥和私钥。

这是输出。

[root@noufserver-master ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IU8Zc98zsgAVIlSCgchSavTv+kkUyC53LWo/LAOPV6c root@noufserver-master
The key's randomart image is:
+---[RSA 3072]----+
|.+...++.*.+.     |
|+o+ o  o B . .   |
|o. + .. + . o +  |
|. . . o+ . . o o |
| . o = .S   .    |
| .o =...         |
|  +oooo          |
| ..=+E.          |
|  ..++.          |
+----[SHA256]-----+
[root@noufserver-master ~]#

使用 ssh-copy-id 命令将公钥复制到从服务器。

[root@noufserver-master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] 
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

第 2 步:在 AlmaLinux 主服务器上安装 LsyncD 包

使用以下 yum 命令在 AlmaLinux 中安装 lsyncd 依赖包。

[root@noufserver-master ~]# yum install lua pkgconfig gcc asciidoc

这是输出。

[root@noufserver-master ~]# yum install lua pkgconfig gcc asciidoc
Last metadata expiration check: 0:02:29 ago on Sun 06 Mar 2022 04:37:19 AM UTC.
Dependencies resolved.
=========================================================================================================================================================================
 Package                                                Architecture             Version                                               Repository                   Size
=========================================================================================================================================================================
Installing:
 asciidoc                                               noarch                   8.6.10-0.5.20180627gitf7c2274.el8                     appstream                   216 k
 gcc                                                    x86_64                   8.5.0-4.el8_5.alma                                    appstream                    23 M
 lua                                                    x86_64                   5.3.4-12.el8                                          appstream                   191 k
 pkgconf-pkg-config                                     x86_64                   1.4.2-1.el8                                           baseos                       15 k

—----
—----

Installed:
  adobe-mappings-cmap-20171205-3.el8.noarch              adobe-mappings-cmap-deprecated-20171205-3.el8.noarch        adobe-mappings-pdf-20180407-1.el8.noarch           
  asciidoc-8.6.10-0.5.20180627gitf7c2274.el8.noarch      atk-2.28.1-1.el8.x86_64                                     avahi-libs-0.7-20.el8.x86_64                       
  binutils-2.30-108.el8_5.1.x86_64                       boost-regex-1.66.0-10.el8.x86_64                            cairo-1.15.12-3.el8.x86_64                         
  cpp-8.5.0-4.el8_5.alma.x86_64                          ctags-5.8-22.el8.x86_64                                     cups-libs-1:2.2.6-40.el8.x86_64                    
  docbook-dtds-1.0-69.el8.noarch                         docbook-style-xsl-1.79.2-9.el8.noarch                       emacs-filesystem-1:26.1-7.el8.noarch               
  fontconfig-2.13.1-4.el8.x86_64                         fontpackages-filesystem-1.44-22.el8.noarch                  fribidi-1.0.4-8.el8.x86_64                         
  gcc-8.5.0-4.el8_5.alma.x86_64                          gd-2.2.5-7.el8.x86_64                                       gdk-pixbuf2-2.36.12-5.el8.x86_64                   
  gdk-pixbuf2-modules-2.36.12-5.el8.x86_64               glibc-devel-2.28-164.el8.x86_64                             glibc-headers-2.28-164.el8.x86_64                  
  google-droid-sans-fonts-20120715-13.el8.noarch         graphite2-1.3.10-10.el8.x86_64                              graphviz-2.40.1-43.el8.x86_64                      
  gtk-update-icon-cache-3.22.30-8.el8.x86_64             gtk2-2.24.32-5.el8.x86_64                                   harfbuzz-1.7.5-3.el8.x86_64                        
  hicolor-icon-theme-0.17-2.el8.noarch                   isl-0.16.1-6.el8.x86_64                                     jasper-libs-2.0.14-5.el8.x86_64                    
  jbig2dec-libs-0.16-1.el8.x86_64                        jbigkit-libs-2.1-14.el8.x86_64                              kernel-headers-4.18.0-348.12.2.el8_5.x86_64        
  lcms2-2.9-2.el8.x86_64                                 libICE-1.0.9-15.el8.x86_64                                  libSM-1.2.3-1.el8.x86_64                           
  libX11-1.6.8-5.el8.x86_64                              libX11-common-1.6.8-5.el8.noarch                            libXau-1.0.9-3.el8.x86_64                          
  libXaw-1.0.13-10.el8.x86_64                            libXcomposite-0.4.4-14.el8.x86_64                           libXcursor-1.1.15-3.el8.x86_64                     
  libXdamage-1.1.4-14.el8.x86_64                         libXext-1.3.4-1.el8.x86_64                                  libXfixes-5.0.3-7.el8.x86_64                       
  libXft-2.3.3-1.el8.x86_64                              libXi-1.7.10-1.el8.x86_64                                   libXinerama-1.1.4-1.el8.x86_64                     
  libXmu-1.1.3-1.el8.x86_64                              libXpm-3.5.12-8.el8.x86_64                                  libXrandr-1.5.2-1.el8.x86_64                       
  libXrender-0.9.10-7.el8.x86_64                         libXt-1.1.5-12.el8.x86_64                                   libXxf86misc-1.0.4-1.el8.x86_64                    
  libXxf86vm-1.1.4-9.el8.x86_64                          libdatrie-0.2.9-7.el8.x86_64                                libfontenc-1.1.3-8.el8.x86_64                      
  libgs-9.27-1.el8.x86_64                                libicu-60.3-2.el8_1.x86_64                                  libidn-1.34-5.el8.x86_64                           
  libijs-0.35-5.el8.x86_64                               libjpeg-turbo-1.5.3-12.el8.x86_64                           libmcpp-2.7.2-20.el8.x86_64                        
  libmpc-1.1.0-9.1.el8.x86_64                            libpaper-1.1.24-22.el8.x86_64                               libpkgconf-1.4.2-1.el8.x86_64                      
  librsvg2-2.42.7-4.el8.x86_64                           libthai-0.1.27-2.el8.x86_64                                 libtiff-4.0.9-20.el8.x86_64                        
  libtool-ltdl-2.4.6-25.el8.x86_64                       libwebp-1.0.0-5.el8.x86_64                                  libxcb-1.13.1-1.el8.x86_64                         
  libxcrypt-devel-4.1.1-6.el8.x86_64                     libxslt-1.1.32-6.el8.x86_64                                 lua-5.3.4-12.el8.x86_64                            
  mcpp-2.7.2-20.el8.x86_64                               openjpeg2-2.4.0-4.el8.x86_64                                pango-1.42.4-8.el8.x86_64                          
  pixman-0.38.4-1.el8.x86_64                             pkgconf-1.4.2-1.el8.x86_64                                  pkgconf-m4-1.4.2-1.el8.noarch                      
  pkgconf-pkg-config-1.4.2-1.el8.x86_64                  sgml-common-0.6.3-50.el8.noarch                             source-highlight-3.1.8-17.el8.x86_64               
  urw-base35-bookman-fonts-20170801-10.el8.noarch        urw-base35-c059-fonts-20170801-10.el8.noarch                urw-base35-d050000l-fonts-20170801-10.el8.noarch   
  urw-base35-fonts-20170801-10.el8.noarch                urw-base35-fonts-common-20170801-10.el8.noarch              urw-base35-gothic-fonts-20170801-10.el8.noarch     
  urw-base35-nimbus-mono-ps-fonts-20170801-10.el8.noarch urw-base35-nimbus-roman-fonts-20170801-10.el8.noarch        urw-base35-nimbus-sans-fonts-20170801-10.el8.noarch
  urw-base35-p052-fonts-20170801-10.el8.noarch           urw-base35-standard-symbols-ps-fonts-20170801-10.el8.noarch urw-base35-z003-fonts-20170801-10.el8.noarch       
  vim-filesystem-2:8.0.1763-16.el8_5.4.noarch            xml-common-0.6.3-50.el8.noarch                              xorg-x11-font-utils-1:7.5-41.el8.x86_64            
  xorg-x11-fonts-ISO8859-1-100dpi-7.5-19.el8.noarch      xorg-x11-server-utils-7.7-27.el8.x86_64                    

Complete!

在使用 yum 命令安装 lsyncd 包之前,您需要先使用以下命令安装 EPEL 存储库配置包,因为默认存储库中没有 lsyncd 包。

[root@noufserver-master ~]# yum install -y epel-release

这是输出。

[root@noufserver-master ~]# yum install -y epel-release
Last metadata expiration check: 0:06:06 ago on Sun 06 Mar 2022 04:37:19 AM UTC.
Dependencies resolved.
=========================================================================================================================================================================
 Package                                     Architecture                          Version                                   Repository                             Size
=========================================================================================================================================================================
Installing:
 epel-release                                noarch                                8-10.el8                                  extras                                 22 k

Transaction Summary
=========================================================================================================================================================================
Install  1 Package

Total download size: 22 k
Installed size: 32 k
Downloading Packages:
epel-release-8-10.el8.noarch.rpm                                                                                                         269 kB/s |  22 kB     00:00    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                    163 kB/s |  22 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                 1/1 
  Installing       : epel-release-8-10.el8.noarch                                                                                                                    1/1 
  Running scriptlet: epel-release-8-10.el8.noarch                                                                                                                    1/1 
  Verifying        : epel-release-8-10.el8.noarch                                                                                                                    1/1 
Installed:
  epel-release-8-10.el8.noarch    
                                                                                                                                       
Complete!

要安装 lsyncd 包,请运行以下命令。

[root@noufserver-master ~]# yum install lsyncd

这是输出。

[root@noufserver-master ~]# yum install lsyncd
Extra Packages for Enterprise Linux 8 - x86_64                                                                                            16 MB/s |  11 MB     00:00    
Extra Packages for Enterprise Linux Modular 8 - x86_64                                                                                   5.7 MB/s | 979 kB     00:00    
Dependencies resolved.
=========================================================================================================================================================================
 Package                                Architecture                           Version                                        Repository                            Size
=========================================================================================================================================================================
Installing:
 lsyncd                                 x86_64                                 2.2.2-9.el8                                    epel                                  86 k

Transaction Summary
=========================================================================================================================================================================
Install  1 Package

Total download size: 86 k
Installed size: 205 k
Is this ok [y/N]: y
Downloading Packages:
lsyncd-2.2.2-9.el8.x86_64.rpm                                                                                                            1.4 MB/s |  86 kB     00:00    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                    691 kB/s |  86 kB     00:00     
Extra Packages for Enterprise Linux 8 - x86_64                                                                                           1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <[email protected]>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                 1/1 
  Installing       : lsyncd-2.2.2-9.el8.x86_64                                                                                                                       1/1 
  Running scriptlet: lsyncd-2.2.2-9.el8.x86_64                                                                                                                       1/1 
  Verifying        : lsyncd-2.2.2-9.el8.x86_64                                                                                                                       1/1 

Installed:
  lsyncd-2.2.2-9.el8.x86_64                                                                                                                                              

Complete!

第 3 步:使用 LsyncD 将本地目录与远程同步

使用此命令在主服务器上创建源目录。

[root@noufserver-master ~]# mkdir master-lsyncd

您可以使用以下命令将一些文件添加到源目录。

[root@noufserver-master ~]# sudo touch master-lsyncd/file{1..25}

这是输出。

[root@noufserver-master master-lsyncd]# ll
total 0
-rw-r--r--. 1 root root 0 Mar  6 05:07 file1
-rw-r--r--. 1 root root 0 Mar  6 05:07 file10
-rw-r--r--. 1 root root 0 Mar  6 05:07 file11
-rw-r--r--. 1 root root 0 Mar  6 05:07 file12
-rw-r--r--. 1 root root 0 Mar  6 05:07 file13
-rw-r--r--. 1 root root 0 Mar  6 05:07 file14
-rw-r--r--. 1 root root 0 Mar  6 05:07 file15
-rw-r--r--. 1 root root 0 Mar  6 05:07 file16
-rw-r--r--. 1 root root 0 Mar  6 05:07 file17
-rw-r--r--. 1 root root 0 Mar  6 05:07 file18
-rw-r--r--. 1 root root 0 Mar  6 05:07 file19
-rw-r--r--. 1 root root 0 Mar  6 05:07 file2
-rw-r--r--. 1 root root 0 Mar  6 05:07 file20
-rw-r--r--. 1 root root 0 Mar  6 05:07 file21
-rw-r--r--. 1 root root 0 Mar  6 05:07 file22
-rw-r--r--. 1 root root 0 Mar  6 05:07 file23
-rw-r--r--. 1 root root 0 Mar  6 05:07 file24
-rw-r--r--. 1 root root 0 Mar  6 05:07 file25
-rw-r--r--. 1 root root 0 Mar  6 05:07 file3
-rw-r--r--. 1 root root 0 Mar  6 05:07 file4
-rw-r--r--. 1 root root 0 Mar  6 05:07 file5
-rw-r--r--. 1 root root 0 Mar  6 05:07 file6
-rw-r--r--. 1 root root 0 Mar  6 05:07 file7
-rw-r--r--. 1 root root 0 Mar  6 05:07 file8
-rw-r--r--. 1 root root 0 Mar  6 05:07 file9

在从服务器上创建一个目标目录。

[root@noufserver-slave ~]# mkdir slave-lsyncd

要设置 LsyncD 配置文件,将示例配置复制到 /etc/lsyncd.conf。

[root@noufserver-master ~]# cp /usr/share/doc/lsyncd/examples/lrsync.lua /etc/lsyncd.conf

将以下行添加到配置文件 /etc/lsyncd.conf。

[root@noufserver-master master-lsyncd]# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings{
 logfile = "/var/log/lsyncd.log",
 statusFile = "/var/log/lsyncd.stat",
 statusInterval = 2,
}
sync{
 default.rsync,
 source="/root/master-lsyncd",
 target="192.168.1.21:/root/slave-lsyncd",
 rsync={rsh ="/usr/bin/ssh -l root -i /root/.ssh/id_rsa",}
}

第 4 步:启动 LsyncD 服务并在重启时启用

要启动 LsyncD 服务,您可以运行以下命令。

[root@noufserver-master ~]# systemctl start lsyncd

要在重新启动时启用 LsyncD,您可以运行以下命令。

[root@noufserver-master ~]# systemctl enable lsyncd

这是输出。

[root@noufserver-master ~]# systemctl start lsyncd
[root@noufserver-master ~]# systemctl enable lsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/lsyncd.service → /usr/lib/systemd/system/lsyncd.service.
[root@noufserver-master ~]# systemctl status lsyncd
● lsyncd.service - Live Syncing (Mirror) Daemon
   Loaded: loaded (/usr/lib/systemd/system/lsyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-03-06 04:58:17 UTC; 3s ago
 Main PID: 10950 (lsyncd)
    Tasks: 1 (limit: 5834)
   Memory: 1.8M
   CGroup: /system.slice/lsyncd.service
           └─10950 /usr/bin/lsyncd -nodaemon /etc/lsyncd.conf

Mar 06 04:58:17 noufserver-master systemd[1]: Started Live Syncing (Mirror) Daemon.

第 5 步:分析复制和日志报告

在本文中,我们使用 root 用户来同步数据。 您可以根据需要更改用户名。

使用 LsyncD 日志文件 /var/log/lsyncd.log 可以查看复制是否完成。

[root@noufserver-master ~]# tail -50 /var/log/lsyncd.log

这是输出。

[root@noufserver-master ~]# tail -50 /var/log/lsyncd.log
Sun Mar  6 04:58:17 2022 Normal: --- Startup ---
Sun Mar  6 04:58:17 2022 Normal: recursive startup rsync: /root/master-lsyncd/ -> 192.168.1.21:/root/slave-lsyncd/
Sun Mar  6 04:58:17 2022 Normal: Startup of /root/master-lsyncd/ -> 192.168.1.21:/root/slave-lsyncd/ finished.
Sun Mar  6 05:08:01 2022 Normal: Calling rsync with filter-list of new/modified files/dirs
/file1
/file2
/file3
/file4
/file5
/file6
/file7
/file8
/file9
/file10
/file11
/file12
/file13
/file14
/file15
/file16
/file17
/file18
/file19
/file20
/file21
/file22
/file23
/file24
/file25
Sun Mar  6 05:08:01 2022 Normal: Finished a list after exitcode: 23
Sun Mar  6 05:09:09 2022 Normal: --- TERM signal, fading ---
Sun Mar  6 05:09:09 2022 Normal: --- Startup ---
Sun Mar  6 05:09:09 2022 Normal: recursive startup rsync: /root/master-lsyncd/ -> 192.168.1.21:/root/slave-lsyncd/
Sun Mar  6 05:09:09 2022 Normal: Startup of /root/master-lsyncd/ -> 192.168.1.21:/root/slave-lsyncd/ finished.

您可以使用统计文件 /var/log/lsyncd.stat 查看 LsyncD 状态报告。

[root@noufserver-master lsyncd]# cat /var/log/lsyncd.stat

这是输出。

[root@noufserver-master ~]# cat /var/log/lsyncd.stat
Lsyncd status report at Sun Mar  6 05:09:11 2022

Sync1 source=/root/master-lsyncd/
There are 0 delays
Excluding:
  nothing.

Inotify watching 1 directories
  1: /root/master-lsyncd/

总结

如果您正在寻找在 Almalinux 上安装和配置 LsyncD 的服务器,或者您对执行上述任何步骤感到不舒服,Liquid Web 可以满足您的需求。 我们的支持团队由经验丰富的 Linux 技术人员和才华横溢的系统管理员组成,他们对多种网络托管技术有着深入的了解,尤其是本文中讨论的技术。

我们提供专用服务器和 VPS 托管选项。 此外,我们技术娴熟的团队提供 24/7/365 全天候支持和监控服务,让您可以专注于您的网站。 立即联系我们的团队以了解更多信息。