如何在Linux中使用LSYNCD同步目录
在本文中,我将解释如何在Linux系统上安装Lsyncd(Live Syncing Mirror守护程序)以及如何在Linux中同步远程和本地目录。
LSYNCD是一个轻量级实时镜像解决方案,在不妨碍现有的本地文件系统性能的情况下进行比较易于安装。
跟踪任何数据修改并在经常使用新内容更新的目录之间同步这些数据是非常有用的。
默认情况下,它仅是rsync。
所有自定义配置文件都以Lua语言编写,可以获得强大,灵活和简单的配置。
LSYNCD 2.2.1需要在所有来源和目标机器上进行rsync 3.1.
在Rhel/CentOS上安装Lsyncd 7
要在CentOS 7.5系统上启用LSyncd,我们需要启用涡级资源库。
我们只需运行此命令即可安装它。
#yum install epel-release #yum install lsyncd
我们可以通过运行此命令确认已安装的版本:
# lsyncd -version Version: 2.2.2
lsyncd配置
LSYNCD配置文件在RHEL/CentOS 7.5系统上在/etc/lsyncd.conf中自动创建。
默认情况下,其内容如下所示:
# cat /etc/lsyncd.conf --- -- User configuration file for lsyncd. - -- Simple example for default rsync, but executing moves through on the target. - -- For more examples, see /usr/share/doc/lsyncd*/examples/
我们需要根据我们的目的修改此配置文件。
如本配置文件中所述,所有示例脚本都在以下位置可用:/usr/share/doc/lsyncd-2.2.2/examples /
# cd /usr/share/doc/lsyncd-2.2.2/examples/ [root@li1050-94 examples]# ll total 40 -rw-r--r--. 1 root root 715 Nov 16 2016 lalarm.lua -rw-r--r--. 1 root root 1055 Nov 16 2016 lbash.lua -rw-r--r--. 1 root root 534 Nov 16 2016 lecho.lua -rw-r--r--. 1 root root 3376 Nov 16 2016 lftp.lua -rw-r--r--. 1 root root 2278 Nov 16 2016 lgforce.lua -rw-r--r--. 1 root root 2737 Nov 16 2016 limagemagic.lua -rw-r--r--. 1 root root 2770 Nov 16 2016 lpostcmd.lua -rw-r--r--. 1 root root 211 Nov 16 2016 lrsync.lua -rw-r--r--. 1 root root 204 Nov 16 2016 lrsyncssh.lua -rw-r--r--. 1 root root 4047 Nov 16 2016 lsayirc.lua
所有这些文件都是LSYNCD示例配置文件。
在这些文件中,我们正在解释更多关于这些文件的使用即lrsync.lua和lrsyncssh.lua。
让我们看看下面的示例配置文件:
本地同步的示例配置:
# cat /usr/share/doc/lsyncd-2.2.2/examples/lrsync.lua --- -- User configuration file for lsyncd. - -- Simple example for default rsync. - settings { statusFile = "/tmp/lsyncd.stat", statusInterval = 1, } sync{ default.rsync, source="src", target="trg", }
远程SYNC的示例配置:
# cat /usr/share/doc/lsyncd-2.2.2/examples/lrsyncssh.lua --- -- User configuration file for lsyncd. - -- Simple example for default rsync, but executing moves through on the target. - sync{default.rsyncssh, source="src", host="localhost", targetdir="dst/"}
为了保留此同步过程而不是每x分钟运行Cron作业,Lsyncd使用Linux内核挂钩在已更改目录中的任何文件时获取通知。
默认情况下,它会在20秒内排队任何同步命令。
我们甚至可以根据" - -Delay"选项以及Sync命令来修改此时间间隔。
sync { default.rsyncssh, source = "SRC", target = "DEST", delete = "running", -- prevents deletion of files on startup (ie when a server comes back online, don't delete files that are new on the backup) delay = 5, -- run every 5 seconds instead of default 20 }
在使用"default.rsyncssh"sync命令"期间,始终建议使用" - delete""选项以防止从目标文件夹中丢失文件。
创建或者修改Lsyncd配置文件后,必须重新启动LSyncd进程。
由于Lsyncd使用rsync工具来复制,移动和删除从源到目标的文件。
我们可以利用rsync交换机来防止在目标上进行不必要的重复,并使此过程进行平滑。
以下解释了一些重要的rsync选项:
--delete:
此选项可确保删除源目录中不在源目录中的远程目录中的任何文件。- itmits:"如果我们将在又一次地运行此脚本,则此选项非常重要,因为它将在同步中保留两个文件之间的时间。 "--force:
此选项允许删除非空目录才能被空目录替换。--links:
此选项用于将Symlinks复制为Symlinks。--progress2:
它会导致整个传输的总体进展,而不仅仅是被复制的单个文件。- 运行:
此选项执行试运行,而无需实际执行任何删除或者传输,但也可以告诉我们它将做什么。
首次在编写后运行任何rsync命令时,我强烈建议使用此选项。--owner:
确保保留文件的所有者用户(不是所有者的权限级别)。--group:
确保保留文件的组用户(不是组的权限级别)。--perms:
preserve权限。- sparse:
确保稀疏镜像文件有效传输。
同步本地目录
现在让我们看看如何使用LSYNCD同步两个本地文件夹。
我们可以创建一个源文件夹即"SRC_DIR"和"目标"文件夹即"DEST_DIR"以更生动地解释此过程。
让我们创建文件夹并将一些文件添加到源目录中以执行同步。
# mkdir SRC_DIR *//Create source directory //* # mkdir DEST_DIR *//Create target directory //* # cd SRC_DIR/*//Move to the source folder and create some random files //* # touch file{1..10} ~/SRC_DIR# ll *//List out the Source folder contents //* total 8 drwxr-xr-x 2 root root 4096 Aug 2 07:45 ./ drwx------ 7 root root 4096 Aug 2 07:46 ../ -rw-r--r-- 1 root root 0 Aug 2 07:45 file1 -rw-r--r-- 1 root root 0 Aug 2 07:45 file10 -rw-r--r-- 1 root root 0 Aug 2 07:45 file2 -rw-r--r-- 1 root root 0 Aug 2 07:45 file3 -rw-r--r-- 1 root root 0 Aug 2 07:45 file4 -rw-r--r-- 1 root root 0 Aug 2 07:45 file5 -rw-r--r-- 1 root root 0 Aug 2 07:45 file6 -rw-r--r-- 1 root root 0 Aug 2 07:45 file7 -rw-r--r-- 1 root root 0 Aug 2 07:45 file8 -rw-r--r-- 1 root root 0 Aug 2 07:45 file9
接下来,我们可以创建LSYNCD日志文件和状态文件以跟踪该过程。
这些步骤是可选的。
但我建议为所有任务维护日志文件。
# mkdir /var/log/lsyncd # touch /var/log/lsyncd/lsyncd.{log,status}
现在我们需要修改LSyncd配置文件以执行此本地rsync。
正如我们之前讨论的那样,CentOS系统的默认LSYNCD配置文件是/etc/lsyncd.conf
。
我们需要使用源和目标目录和日志文件更新这些配置文件。
# cat /etc/lsyncd.conf --- -- User configuration file for lsyncd. - -- Simple example for default rsync, but executing moves through on the target. - -- For more examples, see /usr/share/doc/lsyncd*/examples/ - settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status" } sync { default.rsync, source = "/root/SRC_DIR", target = "/root/DEST_DIR", }
我们可以使用自己的值替换源和目标目录路径。
保存并关闭配置文件。
完成后,重新启动并启用LSYNCD服务。
# systemctl enable lsyncd lsyncd.service is not a native service, redirecting to systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable lsyncd # systemctl start lsyncd
现在比较源头和目标目录内容以确认其工作。
SRC_DIR]# ll total 0 -rw-r--r--. 1 root root 0 Aug 2 13:51 file1 -rw-r--r--. 1 root root 0 Aug 2 13:51 file10 -rw-r--r--. 1 root root 0 Aug 2 13:51 file2 -rw-r--r--. 1 root root 0 Aug 2 13:51 file3 -rw-r--r--. 1 root root 0 Aug 2 13:51 file4 -rw-r--r--. 1 root root 0 Aug 2 13:51 file5 -rw-r--r--. 1 root root 0 Aug 2 13:51 file6 -rw-r--r--. 1 root root 0 Aug 2 13:51 file7 -rw-r--r--. 1 root root 0 Aug 2 13:51 file8 -rw-r--r--. 1 root root 0 Aug 2 13:51 file9 DEST_DIR]# ll total 0 -rw-r--r--. 1 root root 0 Aug 2 13:51 file1 -rw-r--r--. 1 root root 0 Aug 2 13:51 file10 -rw-r--r--. 1 root root 0 Aug 2 13:51 file2 -rw-r--r--. 1 root root 0 Aug 2 13:51 file3 -rw-r--r--. 1 root root 0 Aug 2 13:51 file4 -rw-r--r--. 1 root root 0 Aug 2 13:51 file5 -rw-r--r--. 1 root root 0 Aug 2 13:51 file6 -rw-r--r--. 1 root root 0 Aug 2 13:51 file7 -rw-r--r--. 1 root root 0 Aug 2 13:51 file8 -rw-r--r--. 1 root root 0 Aug 2 13:51 file9
欢呼! SRC_DIR的源目录内容已成功同步到目标目录。
此外,我们可以查看日志和状态文件以验证复制状态,以确认它是否已完成。
# tail -10 /var/log/lsyncd/lsyncd.log Thu Aug 2 14:03:16 2016 Normal: --- Startup -- Thu Aug 2 14:03:16 2016 Normal: recursive startup rsync: /root/SRC_DIR/-> /root/DEST_DIR/ Thu Aug 2 14:03:16 2016 Normal: Startup of /root/SRC_DIR/-> /root/DEST_DIR/finished. # more /var/log/lsyncd/lsyncd.status Lsyncd status report at Thu Aug 2 14:03:27 2016 Sync1 source=/root/SRC_DIR/ There are 0 delays Excluding: nothing. Inotify watching 1 directories 1: /root/SRC_DIR/
同步多个本地文件夹
为了将多个文件夹同步到一个或者多个目标目录,我们需要使用更多的Sync命令语句与我们所需的源和目标目录更新配置文件。
sync{ default.rsync, source='source1', target='target1' } sync{ default.rsync, source=' 'source2', target='target2' }
例如,请参阅我的lsyncd配置文件/etc/lsyncd.conf以将两个文件夹中的两个同步到/root/src和/etc/nginx到下面的/备份的目标目录:
--- -- User configuration file for lsyncd. - -- Simple example for default rsync. - settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status" } sync{ default.rsync, source='/root/SRC', target='/backup/SRC' } sync{ default.rsync, source='/etc/nginx', target='/backup/Nginx_bkup' }
在使用所需的更改更新配置文件后,我们可以重新启动LSyncd进程以启动rsync进程。
完成后,我们可以验证日志文件以确认其状态。
# tail /var/log/lsyncd/lsyncd.log Mon Aug 6 08:36:16 2016 Normal: recursive startup rsync: /root/SRC/-> /backup/SRC/ Mon Aug 6 08:36:16 2016 Normal: recursive startup rsync: /etc/nginx/-> /backup/Nginx_bkup/ Mon Aug 6 08:36:16 2016 Normal: Startup of "/root/SRC/" finished. Mon Aug 6 08:36:16 2016 Normal: Startup of "/etc/nginx/" finished.
同样,我们可以多次使用此"default-rsync"sync命令语句,当我们想要将与多个目标或者多个源目录同一源目录同步到同一目标目录时多次。
与远程目录同步
要启动远程目录同步,我们需要设置无密码SSH登录。
这将有助于Lsyncd自动将本地目录的内容复制到远程目录,而无需用户干预。
由于我们正在跨两个服务器进行同步。
我们可以采用源服务器和目标服务器来更清楚地解释此过程。
请参阅下面的SRC和Dest Server IP:
SRC_ SERVER IP : 45.33.113.94 DEST_SERVER IP: 45.33.121.82
步骤1)在源服务器上创建SSH键以获取无密码登录
我们需要为源服务器创建一个SSH键,并将公钥复制到目标服务器,以在帐户同步期间增强服务器之间的SSH连接。
我们可以使用以下命令生成RSA键:
#ssh-keygen -t rsa 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:T+p4P/5WTw/JA7B/B+uonDNF6KvqDpSl2frl4i3Ma8Q root@li1050-94 The key's randomart image is: +---[RSA 2048]----+ | | | . | | . + | | * o o . | | =.. S..o o + | | . .E +. o O o| | o+ o .o = *.| | o**..=.o . o| | =O*=+*B. | +----[SHA256]-----+
步骤2)将公钥复制到目标服务器
现在将公钥复制到目标服务器以启用密码登录。
# ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '45.33.121.82 (45.33.121.82)' can't be established. ECDSA key fingerprint is SHA256:qI+CBEAw9MX+XfXQ1P0NmXVg0tBkWnmjeE0p1wWHzpM. ECDSA key fingerprint is MD5:62:d9:cc:a5:8b:7a:ef:fd:5e:b8:be:a2:75:3a:0c:20. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1
其中45.33.121.82是目标服务器IP。
现在我们将能够从源连接到远程目的地服务器,而无需任何密码。
# ssh [email protected]
步骤3)为远程同步创建目标目录
让我们在目标服务器上创建一个目标目录即nemate_dir。
一旦完成,我们可以注销目标服务器。
#mkdir Remote_Dir
步骤4)修改LSYNCD配置文件以启用Remote Sync
我们可以备份当前的LSYNCD配置文件,在/etc/lsyncd.conf中备份,并复制位于
/usr/share/doc/lsyncd-2.2.2/lifation/lrsyncssh.lua的远程同步到主LSyncd配置文件
/etc/lsyncd.conf`。
完成后,我们可以在配置文件中编辑源目录,主机和目标目录。
请参阅下面的Setup的远程同步的LSyncd配置文件:
复制示例配置以保留其正确的语法。
# cp /usr/share/doc/lsyncd-2.2.2/examples/lrsyncssh.lua /etc/lsyncd.conf
相应地编辑配置文件。
# cat /etc/lsyncd.conf --- -- User configuration file for lsyncd. - -- Simple example for default rsync, but executing moves through on the target. - settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status" } sync{default.rsyncssh, source="/root/SRC_DIR", host="45.33.121.82", targetdir="/root/Remote_Dir"}
步骤5)在源上重新启动LSyncd服务
配置文件正确编辑并保存后,我们可以重新启动LSyncd服务以使这些更改有效。
现在登录远程服务器(dest_server)并确认远程rsync目录上的内容即/root/remote_dir
。
#ssh [email protected] #cd /root/Remote_Dir # ll total 8 drwxr-xr-x 2 root root 4096 Aug 2 14:08 ./ drwx------ 9 root root 4096 Aug 5 06:52 ../ -rw-r--r-- 1 root root 0 Aug 2 13:51 file1 -rw-r--r-- 1 root root 0 Aug 2 13:51 file10 -rw-r--r-- 1 root root 0 Aug 2 13:51 file2 -rw-r--r-- 1 root root 0 Aug 2 13:51 file3 -rw-r--r-- 1 root root 0 Aug 2 13:51 file4 -rw-r--r-- 1 root root 0 Aug 2 13:51 file5 -rw-r--r-- 1 root root 0 Aug 2 13:51 file6 -rw-r--r-- 1 root root 0 Aug 2 13:51 file7 -rw-r--r-- 1 root root 0 Aug 2 13:51 file8 -rw-r--r-- 1 root root 0 Aug 2 13:51 file9
你好!我们可以从本地系统上的源目录中查看要在目标服务器中的目标目录上复制的所有文件。
我们甚至可以验证在源上查看LSYNCD日志文件的rsync进程的成功完成。
尾巴/var/log/lsyncd/lsyncd.log Sun 8月5日07:04:42 2016 2016正常:--startup - Sun 8月5 07:04:42 2016正常:递归启动rsync:/root/src_dir/- > 45.33 .121.82:/root/remote_dir/sun 8月5日07:04:43 2016正常:启动"/root/src_dir /"完成:0 sun 8月5 07:13:48 2016正常:rsyncing列表/Sun 8月5日07: 2016年13:49正常:完成(列表):0
在多个远程服务器上同步
以前,我们描述了如何在远程服务器上同步文件夹。
同样,我们可以多次使用default.rsyncssh命令语句来在多个远程目标文件夹上同步所需的源文件夹。
但是,我们需要确保启用密码少的SSH登录,以将本地目录的内容复制到多个远程目录,而无需用户干预。
sync{default.rsyncssh, source="source1", host="host1", targetdir="target1"} sync{default.rsyncssh, source="source2", host="host2", targetdir="target2}
我将用简单的例子将来自SRC服务器的"/etc/nginx"文件夹传输到远程服务器dest 1和dest 2的简单示例"。
此过程的第一步将在源服务器上生成RSA键,并将其公钥将其公共密钥复制到我的远程服务器dest 1和dest 2,如上所述。
对于两个远程服务器DEST 1和DEST 2,我们需要重复与步骤1到步骤3相同的步骤,以确保密码较少的SSH登录。
其次,我们需要使用所需的源,主机和目标文件夹修改具有多个default.rsyncssh
命令语句的Lsyncd配置文件。
请参阅下面此同步过程的LSyncd配置文件:
# cat /etc/lsyncd.conf --- -- User configuration file for lsyncd. - -- Simple example for default rsync, but executing moves through on the target. - settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status" } sync{default.rsyncssh, source="/etc/nginx", host="45.33.121.82", targetdir="/backup/nginx"} sync{default.rsyncssh, source="/etc/nginx", host="45.33.113.94", targetdir="/backup/nginx"}
进行以下配置更改后,我们可以重新启动LSyncd服务以启动此同步过程。
有一次,它完成了我们可以验证日志文件以确认其状态。
# tail /var/log/lsyncd/lsyncd.log Mon Aug 6 09:15:55 2016 Normal: recursive startup rsync: /etc/nginx/-> 45.33.121.182:/backup/nginx/ Mon Aug 6 09:15:55 2016 Normal: recursive startup rsync: /etc/nginx/-> 45.33.113.194:/backup/nginx/ Mon Aug 6 09:15:56 2016 Normal: Startup of "/etc/nginx/" finished: 0 Mon Aug 6 09:15:56 2016 Normal: Startup of "/etc/nginx/" finished: 0
在Debian/Ubuntu上安装Lsyncd 18.04
在Debian及其衍生品等ubuntu,linux Mint等,我们可以使用简单的apt命令安装lsyncd,如下所示:
#apt install lsyncd
我们可以使用此命令确认已安装的LSYNCD版本:
# lsyncd -version Version: 2.1.6
Debian/Ubuntu上的配置
在基于Ubuntu的系统上,它不会提供任何默认的LSYNCD配置文件。
建议根据我们的目的手动创建这些配置文件。
我们可以在该位置获取示例配置文件。/usr/share/doc/lsyncd/simem/ls
maplus示例配置文件为我们提供了一个/如何同步的基本思想。
:/usr/share/doc/lsyncd/examples# ll total 48 drwxr-xr-x 2 root root 4096 Aug 2 07:34 ./ drwxr-xr-x 3 root root 4096 Aug 2 07:34 ../ -rw-r--r-- 1 root root 715 Oct 15 2014 lalarm.lua -rw-r--r-- 1 root root 1057 Oct 15 2014 lbash.lua -rw-r--r-- 1 root root 534 Oct 15 2014 lecho.lua -rw-r--r-- 1 root root 3376 Oct 15 2014 lftp.lua -rw-r--r-- 1 root root 2278 Oct 15 2014 lgforce.lua -rw-r--r-- 1 root root 2737 Oct 15 2014 limagemagic.lua -rw-r--r-- 1 root root 2770 Oct 15 2014 lpostcmd.lua -rw-r--r-- 1 root root 213 Oct 15 2014 lrsync.lua -rw-r--r-- 1 root root 204 Oct 15 2014 lrsyncssh.lua -rw-r--r-- 1 root root 4047 Oct 15 2014 lsayirc.lua
所有这些配置文件都以LUA编程语言编写。
请参阅下面的简单本地rsync的Lsyncd示例配置:
:/usr/share/doc/lsyncd/examples# cat lrsync.lua --- -- User configuration file for lsyncd. - -- Simple example for default rsync. - settings = { statusFile = "/tmp/lsyncd.stat", statusInterval = 1, } sync{ default.rsync, source="src", target="trg", }
与上述CentOS系统相比,该过程与上述CentOS系统完全相同。
首先,我们需要在源服务器上设置密码SSH登录生成RSA键。
这将确保LSYNCD在没有用户干预的情况下自动将内容复制到远程目录。
在远程同步期间记住的关键点比较本地同步是我们需要将default.rsync更改为default.rsyncssh
以启用ssh的rsync,并且我们应该用“host”和“targeted”变量替换“target”变量。
此外,我们需要将配置文件位置维护为"ubuntu/debian系统"上的"/etc/lsyncd/lsyncd.conf.lua"。
所有Linux Sytems的LSYNCD配置过程的其余部分都是相同的。