lsyncd 使用文件系统事件接口(inotify或fsevents)来监视对本地文件和目录的更改。
参考文章
lsyncd实时同步搭建指南——取代rsync+inotify
rsync安装与使用——实战
环境
- system: centos8
- version: rsync-3.1.3, lsyncd-2.2.2
节点架构
源主机:数据来源的主机
目标主机:备份数据的主机
源主机 | 目标主机 | |
---|---|---|
节点名 | node001 | node002 |
节点地址 | 192.168.1.2 | 192.168.1.3 |
组件安装 | lsyncd(服务端)rsync(客户端,服务端) | rsync(客户端,服务端) |
自定义脚本参数
变量名 | 变量默认值 | 说明 | |
---|---|---|---|
rsync | backup_user | rsync_backup | 虚拟用户名称,备份专用 |
backup_passwd | 123456 | 虚拟用户密码,备份专用 | |
backup_module | backup | 备份模块名,包含具体备份路径,见/etc/rsyncd.conf配置 | |
backup_path | /root/rsyncd_backup | 备份路径 | |
lsyncd | local_src | 本地需要同步的文件路径 | |
local_dst | 本地备份的目标路径 | ||
local_rsync_passwd_file | /root/rsyncd_backup | rsync密码文件,来自rsync客户端虚拟用户 | |
remote_host | /root/rsyncd_backup | 目标主机地址 | |
remote_rsyncd_user | /root/rsyncd_backup | 虚拟用户名称 | |
remote_rsyncd_module | backup | 备份模块名,同rsync的backup_module |
安装
# yum安装
yum install lsyncd
# 安装验证
lsyncd --version
配置
备份配置文件
mv /etc/lsyncd.conf /etc/lsyncd.conf.bkb
修改配置文件 /etc/lsyncd.conf
- 全局配置参数 settings
cat > /etc/lsyncd.conf << EOF
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
pidfile = "/var/run/lsyncd.pid",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 5,
inotifyMode = "CloseWrite",
maxProcesses = 7,
maxDelays = 1,
nodaemon =true
}
- direct模式(未测试)
local_src="/root/local_workspace/lsyncd_workspace/test/src"
local_dst="/root/local_workspace/lsyncd_workspace/test/dst"
cat >> /etc/lsyncd.conf << EOF
sync {
default.direct,
source = $local_src,
target = $local_dst,
delay = 1,
maxProcesses = 2
}
EOF
- rsync模式(已测试)
local_src="/root/local_workspace/lsyncd_workspace/test/src"
local_dst="/root/local_workspace/lsyncd_workspace/test/dst"
cat >> /etc/lsyncd.conf << EOF
sync {
default.rsync,
source = $local_src,
target = $local_dst,
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
}
}
EOF
- rsync + rsyncd模式(已测试)
local_src="/root/local_workspace/lsyncd_workspace/test/src"
remote_host="192.168.1.3"
remote_rsyncd_user="rsync_backup"
remote_rsyncd_module="backup"
backup_path="/root/rsync_backup"
# 远程备份主机的 rsyncd.conf
cat > /etc/rsyncd.conf <<EOF
[$remote_rsyncd_module]
path = $backup_path
comment = dst test data
EOF
# 本地源主机的 rsyncd.conf
cat >> /etc/lsyncd.conf << EOF
sync {
default.rsync,
source = $local_src,
target = "$backup_user@$remote_host::$remote_rsyncd_module",
delete = "running",
exclude = { ".*", ".tmp" },
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.$remote_rsyncd_user.passwd",
_extra = {"--bwlimit=200"}
}
}
EOF
- rsync + ssh模式(未测试)
local_src="/root/local_workspace/lsyncd_workspace/test/src"
remote_host="192.168.1.3"
remote_user="root"
remote_dst="/root/test_lsyncd_dst"
cat >> /etc/lsyncd.conf << EOF
sync {
default.rsync,
source = $local_src,
target = "$remote_user@$remote_host:$remote_dst",
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no",
}
}
EOF
- rsyncssh模式(未测试)
local_src="/root/local_workspace/lsyncd_workspace/test/src"
remote_host="192.168.1.2"
remote_user="root"
remote_dst="/root/test_lsyncd_dst"
cat >> /etc/lsyncd.conf << EOF
sync {
default.rsyncssh,
source = $local_src,
host = "$remote_host",
targetdir = "$remote_dst",
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
_extra = {"--bwlimit = 2000"}
}
ssh = {
port = 22
}
}
EOF