搬瓦工 VPS 部署 Lsyncd 实时文件同步工具教程
Lsyncd(Live Syncing Daemon)是一款基于 inotify 和 rsync 的实时文件同步守护进程。它监控本地目录的文件变化事件,并在检测到变更后立即通过 rsync 将变化同步到远程服务器。Lsyncd 特别适合需要实时单向同步的场景,如网站文件分发、配置同步和灾备复制。本教程将介绍如何在搬瓦工 VPS 上安装和配置 Lsyncd。
一、Lsyncd 工作原理
- inotify 监控:利用 Linux 内核的 inotify 机制监控文件系统事件(创建、修改、删除、移动)。
- 事件聚合:短时间内的多个事件会被聚合,避免频繁触发同步操作。
- rsync 传输:使用 rsync 的增量传输算法,只传输变化的数据块。
- Lua 配置:配置文件使用 Lua 语言编写,支持灵活的自定义逻辑。
二、安装 Lsyncd
# Ubuntu/Debian
apt update
apt install lsyncd -y
# CentOS
yum install epel-release -y
yum install lsyncd -y
# 验证安装
lsyncd --version
2.1 调整 inotify 参数
# 增加 inotify 监控数量限制
echo "fs.inotify.max_user_watches = 524288" >> /etc/sysctl.conf
sysctl -p
三、基本配置
3.1 本地目录同步
mkdir -p /etc/lsyncd
cat > /etc/lsyncd/lsyncd.conf.lua <<'EOF'
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
maxProcesses = 4,
nodaemon = false,
}
sync {
default.rsync,
source = "/var/www/html",
target = "/backup/www-mirror",
delay = 5,
rsync = {
archive = true,
compress = true,
verbose = true,
},
}
EOF
mkdir -p /var/log/lsyncd
3.2 远程服务器同步
cat > /etc/lsyncd/lsyncd.conf.lua <<'EOF'
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
maxProcesses = 4,
}
sync {
default.rsyncssh,
source = "/var/www/html",
host = "backup-server",
targetdir = "/backup/vps-mirror",
delay = 5,
exclude = {
"*.log",
"*.tmp",
".git",
"node_modules",
"__pycache__",
},
rsync = {
archive = true,
compress = true,
whole_file = false,
_extra = {
"--bwlimit=5000",
},
},
ssh = {
port = 22,
identityFile = "/root/.ssh/id_ed25519",
options = {
StrictHostKeyChecking = "accept-new",
},
},
}
EOF
3.3 多目标同步
cat >> /etc/lsyncd/lsyncd.conf.lua <<'EOF'
-- 同步到第二台服务器
sync {
default.rsyncssh,
source = "/var/www/html",
host = "backup-server-2",
targetdir = "/backup/vps-mirror",
delay = 10,
rsync = {
archive = true,
compress = true,
},
ssh = {
port = 22,
identityFile = "/root/.ssh/id_ed25519",
},
}
-- 同步配置文件
sync {
default.rsyncssh,
source = "/etc/nginx",
host = "backup-server",
targetdir = "/backup/nginx-config",
delay = 15,
rsync = {
archive = true,
},
ssh = {
port = 22,
identityFile = "/root/.ssh/id_ed25519",
},
}
EOF
四、启动服务
# 测试配置文件语法
lsyncd -nodaemon /etc/lsyncd/lsyncd.conf.lua
# 启动服务
systemctl start lsyncd
systemctl enable lsyncd
# 查看状态
systemctl status lsyncd
五、监控与日志
# 查看同步状态
cat /var/log/lsyncd/lsyncd.status
# 查看实时日志
tail -f /var/log/lsyncd/lsyncd.log
# 检查 inotify 使用量
cat /proc/sys/fs/inotify/max_user_watches
find /proc/*/fdinfo -name '*.fdinfo' 2>/dev/null | xargs grep -c inotify 2>/dev/null | head
六、高级配置
6.1 自定义过滤规则
-- 使用 rsync 过滤规则文件
rsync = {
archive = true,
filter_from = "/etc/lsyncd/filter.rules",
}
# /etc/lsyncd/filter.rules
- *.log
- *.tmp
- .git/
- node_modules/
+ /var/www/html/***
- *
6.2 同步后执行脚本
-- 在 Lua 配置中添加钩子
sync {
default.rsyncssh,
source = "/var/www/html",
host = "backup-server",
targetdir = "/var/www/html",
delay = 5,
onComplete = function(event)
os.execute("ssh backup-server 'systemctl reload nginx'")
end,
}
七、常见问题
同步延迟过大
减小 delay 参数值(单位为秒),但设置过低会增加系统负载。对于大量小文件的目录,建议保持 5-15 秒。
inotify 监控数量不足
# 查看当前限制
cat /proc/sys/fs/inotify/max_user_watches
# 临时调整
sysctl fs.inotify.max_user_watches=1048576
SSH 连接断开
Lsyncd 会自动重试失败的同步操作。可以在 SSH 配置中添加 Keep-Alive 设置防止连接超时。
总结
Lsyncd 是实现实时文件同步的轻量方案,特别适合网站文件分发和实时备份场景。如果需要双向同步,可以参考 Syncthing;需要定时批量同步,可以参考 Rsync 教程。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,购买链接:bwh81.net。