安装 yum install -y lsyncd
# lsyncd --version
Version: 2.2.3
# 统计目标目录文件总数(仅统计需要同步的目录,排除历史) 23330
find /hhny/storage/templates/ /hhny/storage/uploadPath/2025/ -type f | wc -l
# 查看当前 inotify 限制(需保证文件数 < max_user_watches) 262144
cat /proc/sys/fs/inotify/max_user_watches
# 永久生效(重启后仍有效)
echo "fs.inotify.max_user_watches=2000000" >> /etc/sysctl.conf
mkdir -p /var/log/lsyncd
chmod 755 /var/log/lsyncd
编辑 /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 2,
maxDelays = 500,
insist = true
}
sync {
default.rsync,
source = "/home/hhny/storage/",
target = "as2:/home/hhny/storage/",
delete = true,
delay = 5,
rsync = {
archive = true,
compress = true,
checksum = false,
verbose = false,
timeout = 60,
_extra = {
"--exclude=*.sw[po]", -- 匹配 .swp, .swo, .swx ...
"--exclude=*~", -- 匹配 file.txt~
"--exclude=.*.sw[po]", -- 隐藏的 swap 文件(如 .file.txt.swp)
"--inplace"
}
}
}
# 临时生效(立即调高到200万句柄)
sysctl -w fs.inotify.max_user_watches=2000000
# 永久生效(重启服务器后仍有效)
echo "fs.inotify.max_user_watches=2000000" >> /etc/sysctl.conf
sysctl -p # 加载配置
创建 /etc/logrotate.d/lsyncd 文件:
/var/log/lsyncd/*.log {
daily # 每天轮转一次
rotate 7 # 保留7天日志
compress # 压缩旧日志
missingok # 日志不存在也不报错
notifempty # 空日志不轮转
create 644 root root # 新建日志的权限
postrotate
systemctl reload lsyncd > /dev/null 2>&1 || true # 轮转后重载日志
endscript
}
# 启动服务
systemctl start lsyncd
# 开机自启(避免服务器重启后服务中断)
systemctl enable lsyncd
# 查看服务状态(关键!确认无报错)
systemctl status lsyncd
正常状态示例:
lsyncd.service - Lsyncd daemon
Loaded: loaded (/usr/lib/systemd/system/lsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2025-12-11 17:00:00 CST; 10s ago
Main PID: 12345 (lsyncd)
Tasks: 3
CGroup: /system.slice/lsyncd.service
└─12345 /usr/bin/lsyncd -nodaemon /etc/lsyncd.conf
# 1. 创建 10MB 测试附件(模拟Java上传)
dd if=/dev/zero of=/hhny/storage/uploadPath/2025/12/test_big_attach.file bs=10M count=1
# 2. 创建静态文件(模拟Java生成)
echo "java generate static file" > /hhny/storage/templates/test_java_static.html
tail -f /var/log/lsyncd/lsyncd.log
cat /var/log/lsyncd/lsyncd.status
# 查看错误日志
grep -i "error\|warning" /var/log/lsyncd/lsyncd.log
# 重启服务(仅异常时用)
systemctl restart lsyncd
# 查看当前句柄使用数(远低于200万即正常)
ls -l /proc/$(pgrep lsyncd)/fd | grep inotify | wc -l
核心逻辑:
/hhny/storage 根目录,自动匹配所有年月的附件目录;