Lsyncd 是一种轻量级的文件实时镜像解决方案,默认使用 rsync 实现
介绍
Lsyncd 通过文件系统事件接口(inotify 或 fsevents)
来监视本地文件和目录的更改。
安装
源服务器
- Lsyncd 2.2.1 requires rsync >= 3.1
# CentOS/Redhat
yum install lsyncd rsync
# Ubuntu
apt install lsyncd rsync
源服务器
apt install rsync
使用示例
采用 rsyncssh
模式
# 源服务器
ssh-keygen -t ed25519 -b 4096
ssh-copy-id root@<target-host-ip>
ssh root@<target-host-ip>
$ cat << EOF > /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log",
insist = true,
inotifyMode = "Modify",
statusInterval = 20
}
sync {
default.rsyncssh,
source="/data/",
host="172.17.0.2",
excludeFrom="/etc/lsyncd.exclude",
targetdir="/data",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
whole_file = false
},
ssh = {
port = 22
}
}
EOF
- 创建排除的文件配置
/etc/lsyncd.exclude
$ cat << EOF > /etc/lsyncd.exclude
*.log
EOF
lsyncd -nodaemon /etc/lsyncd.conf
systemctl start lsyncd.service
lsyncd /etc/lsyncd.conf
-
查看状态并测试
- 日志:
/var/log/lsyncd.log
- 测试:
/var/log/lsyncd-status.log
-
配置 /etc/rsyncd.conf
uid = root # 表示同步的文件所属用户
gid = root # 表示同步的文件所属用户组
use chroot = yes # 安全相关,表示在同步时,是否将同步目录转为根目录
max connections=0 # 允许客户端的最大连接数,0表示不限制
log file=/var/log/rsyncd/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[backup]
path = /var/tmp/
read only = no
list = yes
# auth users = rsyncuser
# secrets file = /etc/rsync.pas
# 密码文件
$ cat /etc/images.pas
rsyncuser:123456
# 修改权限
chmod 600 /etc/images.pas
systemctl start rsync
配置示例
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log",
insist = true,
inotifyMode = "Modify",
statusInterval = 20
}
# 本地目录同步rsync模式
sync {
default.rsync,
source = "/tmp/src",
target = "/tmp/dest1",
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
}
}
# 远程目录同步rsync模式 + rsyncd daemon
sync {
default.rsync,
source = "/tmp/src",
target = "syncuser@172.17.0.2::module1",
delete="running",
exclude = { ".*", ".tmp" },
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsyncd.d/rsync.pwd",
_extra = {"--bwlimit=200"}
}
}
# 远程目录同步rsync模式 + ssh shell
sync {
default.rsync,
source = "/tmp/src",
target = "172.17.0.2:/tmp/dest",
# target = "root@172.17.0.2:/remote/dest",
# 上面target注意如果是普通用户必须拥有写权限
maxDelays = 5,
delay = 30,
# init = true,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
# rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
# 如果要指定其它端口请用上面的rsh
}
}
# 远程目录同步rsync模式 + rsyncssh效果与上面相同
sync {
default.rsyncssh,
source = "/tmp/src2",
host = "172.17.0.2",
targetdir = "/remote/dir",
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
# maxDelays = 5,
delay = 0,
# init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
_extra = {"--bwlimit=2000"},
},
ssh = {
port = 1234
}
}