一、Lsyncd 的简单介绍
- Lsyncd 是一个高效的文件同步工具,通过 Lua 语言封装的 "rsync + inotify" ,使用 Linux 内核里的 Inotify 触发机制通过 Rsync 去差异同步达到实时的效果。
- Lsyncd 是一种轻量级的实时镜像解决方案。Lsyncd 容易安装,不需要新的文件系统或设备,Lysncd 不会妨碍本地文件系统性能。
- Lsyncd 可以通过配置文件实现细粒度的自定义。自定义操作配置甚至可以从头开始编写,从 Shell 脚本到用 Lua 语言编写的代码。
二、Lsyncd 安装前的准备
2.1、设置单向 SSH 免密登录
[root@node120 ~]# cat /etc/hosts
192.168.1.120 node120
192.168.1.121 node121
192.168.1.122 node122
[root@node120 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
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:IRYgQHX339HQQlW4AM5Bx8Z0s591Kk5Ym4YORvE45E8 root@node120
The key's randomart image is:
+---[RSA 2048]----+
|ooo..o..o.+=+o=oo|
| .. .+.* o=o++ |
| o *.E..oooo|
| . o =.+.ooo+|
| S +.*....|
| . o + . |
| . . |
| |
| |
+----[SHA256]-----+
[root@node120 ~]# ssh-copy-id node121
[root@node120 ~]# ssh-copy-id node122
[root@node120 ~]# ssh node121
[root@node120 ~]# ssh node122
2.2、系统文件句柄数的修改
系统文件句柄涉及的两方面:
- File-max : 表示整个系统全局级别的能够打开的文件句柄的数量。是对整个系统的限制,并不是针对用户或进程的。
- Ulimit -n: 控制进程级别能够打开的文件句柄的数量。提供对用户及进程的可用文件句柄的控制,这是进程级别的。
# 1、两个参数大小的数值说明
file-max 的值数量大小取决于具体的 Linux 发行版和其默认配置
ulimit -n 的值,几乎所有 Linux 发行版都是默认为 1024
# 2、两个参数大小的数值查看
[root@localhost]# cat /proc/sys/fs/file-max
91658
[root@localhost]# ulimit -n
1024
# 3、两个参数大小的数值永久修改
[root@localhost]# echo "fs.file-max = 1000000" >> /etc/sysctl.conf
[root@localhost]# sysctl -p
fs.file-max = 1000000
[root@localhost]# cp /etc/security/limits.conf /etc/security/limits.conf.bak
[root@localhost]# vim /etc/security/limits.conf # 文本末尾添加,重启系统后生效
root soft nofile 655360
root hard nofile 655360
* soft nofile 655360
* hard nofile 655360
三、Lsyncd 的安装和使用
说明: 本次只演示单方向的数据同步,还可以实现双向的数据同步
3.1、Lsyncd 的安装和配置
1、使用 YUM 安装 lsyncd (epel 仓库自带安装包)
[root@node120 ~]# yum install epel-release -y
[root@node120 ~]# yum clean all
[root@node120 ~]# yum makecache
[root@node120 ~]# yum install lsyncd -y
2、编辑 lsyncd 的配置文件
[root@node120 ~]# > /etc/lsyncd.conf
[root@node120 ~]# vim /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
}
sync {
default.rsyncssh,
source = "/data/sourcedata/", # 同步的源数据目录
host = "192.168.1.121", # 同步的远程主机地址
targetdir = "/data/destdata", # 同步的远程主机数据目录
delay = 10, 同步的延迟时间(秒)
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = false,
verbose = true,
}
}
sync {
default.rsyncssh,
source = "/data/sourcedata/", # 同步的源数据目录
host = "192.168.1.122", # 同步的远程主机地址
targetdir = "/data/destdata", # 同步的远程主机数据目录
delay = 10,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = false,
verbose = true,
}
}
[root@node120 ~]# lsyncd -nodaemon /etc/lsyncd.conf
3、启动 lsyncd 服务
[root@node120 ~]# systemctl daemon-reload
[root@node120 ~]# systemctl start lsyncd
3.2、进行数据同步的验证
1、 新增文件和目录到 /data/sourcedata/ ,远程两主机 /data/destdata/ 查看同步成功
[root@node120 ~]# ll /root/test/
-rw-r--r--. 1 root root 0 Mar 17 05:40 abc.txt
-rw-r--r--. 1 root root 44 Mar 10 01:00 test.txt
[root@node120 ~]# cp -a /root/test /data/sourcedata/
[root@node120 ~]# cp -a /etc/fstab /data/sourcedata/
[root@node120 ~]# ll /data/sourcedata/
-rw-r--r--. 1 root root 547 Mar 17 00:34 fstab
drwxr-xr-x. 2 root root 37 Mar 17 05:40 test
[root@node121 ~]# ll /data/destdata/
-rw-r--r-- 1 root root 547 Mar 17 00:34 fstab
drwxr-xr-x 2 root root 37 Mar 17 05:40 test
[root@node122 ~]# ll /data/destdata/
-rw-r--r-- 1 root root 547 Mar 17 00:34 fstab
drwxr-xr-x 2 root root 37 Mar 17 05:40 test
2、查看日志信息验证
[root@node120 ~]# cat /var/log/lsyncd/lsyncd.log
Mon Mar 17 05:42:35 2025 Normal: Rsyncing list
/test/
/
/test/test.txt
/test/abc.txt
Mon Mar 17 05:42:35 2025 Normal: Rsyncing list
/test/
/
/test/test.txt
/test/abc.txt
Mon Mar 17 05:42:35 2025 Normal: Finished (list): 0
Mon Mar 17 05:42:35 2025 Normal: Finished (list): 0
Mon Mar 17 05:42:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:42:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:42:46 2025 Normal: Finished (list): 0
Mon Mar 17 05:42:46 2025 Normal: Finished (list): 0
3、删除文件 fstab ,查看远程主机会不也删除此文件
[root@node120 ~]# rm /data/sourcedata/fstab -fr
[root@node120 ~]# ll /data/sourcedata/
drwxr-xr-x. 2 root root 37 Mar 17 05:40 test
[root@node121 ~]# ll /data/destdata/
drwxr-xr-x 2 root root 37 Mar 17 05:40 test
[root@node122 ~]# ll /data/destdata/
drwxr-xr-x 2 root root 37 Mar 17 05:40 test
4、查看日志信息验证
[root@node120 ~]# cat /var/log/lsyncd/lsyncd.log
Mon Mar 17 05:42:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:42:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:42:46 2025 Normal: Finished (list): 0
Mon Mar 17 05:42:46 2025 Normal: Finished (list): 0
Mon Mar 17 05:47:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:47:45 2025 Normal: Rsyncing list
/fstab
/
Mon Mar 17 05:47:45 2025 Normal: Finished (list): 0
Mon Mar 17 05:47:45 2025 Normal: Finished (list): 0
四、Lsyncd 配置文件说明
4.1、配置文件的三个区域
-
settings: 全局设置相关,比如日志文件路径,同步进程数,是否后台运行等等
-
sync: 同步相关的设置,比如从哪同步到哪,是否有忽略文件,多久同步一次等等
-
rsync: 这部分是在sync区域里面的,它主要配置 rsync 本身的一些选项及参数
4.2、具体相关选项及参数
1、全局配置 "settings"
settings {
logfile ="/var/log/lsyncd/lsyncd.log", // 定义日志文件
tatusFile ="/var/log/lsyncd/lsyncd.status", // 定义状态文件
pidfile = "/var/log/lsyncd/lsyncd.pid", // 定义 pid 文件
inotifyMode = "CloseWrite", //指定 Inotify 监控的事件,默认是 CloseWrite
statusInterval = 10, // 状态刷新间隔(秒),默认10秒
maxProcesses = 10, // 数据同步时最大的进程个数
maxDelays = 1, // 激活一次同步的累计监控的事件个数
nodaemon =true, // 表示是否启用守护模式,默认是不启用,使用默认设置即可
}
2、sync 部分的配置
sync {
default.rsync, // 同步的三种模式 rsync、rsyncssh、direct,默认使用
// 本地目录间同步 (可用 direct 和 rsync 模式)
// 同步到远程主机目录 (可用 rsync 和 rsyncssh 模式)
source = "/xxxx", // 同步的源数据目录
host = "192.168.X.X", // 同步的远程主机地址
user = "username", // 同步的远程主机用户名,默认是 root 省略不写
targetdir = "/xxxxx", // 同步的远程主机数据目录
delay = 10, // 等待 rsync 同步延时时间,默认15秒,不建议设置太少
excludeFrom = " " // 排除不同步的选项列表,个人觉得完全是扯蛋,不同步放进来做什么!
delete = "running", // Lsyncd 默认会 "delete = true" 来允许同步删除。还有 "startup"、"running "值
// "startup" 表示将在启动时删除目标上的不同文件,但不会在正常操作时删除
// "running" 表示在启动时不会删除目标上的不同文件,但会删除正常操作期间删除的文件
ssh = {
port = 2022, // 默认端口为 22 时,此项设置省略
}
}
3、rsync 部分的配置
rsync = {
binary = "/usr/bin/rsync", // rsync可执行程序地址,默认/usr/bin/rsync
archive = true, // 默认 false,以递归方式传输文件,并保持所有文件属性
compress = true, // 压缩传输默认为 true。本地目录同步可以设为 false 参数
verbose = true, // 同步详细模式输出
perms = true, // 保留文件权限,默认为 true
_extra = {"--bwlimit=1000"}, // 限速速率,单位 kb/s
password_file = "/etc/lrsyncdpwd", // 密码文件路径(模式为 rsyncssh 时不需要设置此项)
}