lsyncd文件实时同步

说明

lsyncd是一个支持实时、双向、多机器的多模式文件同步工具。使用 Lua 语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过 rsync 去差异同步,达到实时的效果。完美解决了 inotify + rsync海量文件同步带来的文件频繁发送文件列表的问题 —— 通过时间延迟或累计触发事件次数实现。

配置

#防火墙配置
firewall-cmd --permanent --zone=public --add-port=873/tcp
firewall-cmd --reload

 
#源端安装,如果没有则安装epel
yum -y install epel-release
yum -y install lsyncd

#目标端安装
yum -y install rsync

#源端配置
mkdir /opt/lsyncd


#配置日志时,一定注意日志需要定期归档,切分。

vi /etc/lsyncd.conf

settings {
    logfile      ="/opt/lsyncd/lsyncd.log",
    statusFile   ="/opt/lsyncd/lsyncd.status",
    inotifyMode  = "CloseWrite",
    maxProcesses = 8,
    -- nodaemon = false,
}
 
#远程目录同步,rsync模式 + ssh shell
sync {
    default.rsync,
    -- 同步源目录(本机某个目录,必须保证该文件或目录在源服务器上存在,不然启动会报错)
    source    = "/var/opt/gitlab/backups",
    -- 同步目标地址和目标目录,注意target如果是普通需要有写权限
    target    = "[email protected]:/opt/gitlab/backups",
    -- excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    maxDelays = 1,
    delay = 15,
    rsync     = {
        binary    = "/usr/bin/rsync",
        archive   = true,
        compress  = true,
        verbose   = true
        }
}

#免密配置
ssh-keygen -t rsa
ssh-copy-id [email protected]
ssh [email protected]

#服务配置

cat /etc/sysconfig/lsyncd
LSYNCD_OPTIONS="/etc/lsyncd.conf"

cat /usr/lib/systemd/system/lsyncd.service
[Unit]
Description=Live Syncing (Mirror) Daemon
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/lsyncd
ExecStart=/usr/bin/lsyncd -nodaemon $LSYNCD_OPTIONS

[Install]
WantedBy=multi-user.target


#启动服务
systemctl daemon-reload
systemctl start lsyncd
systemctl status lsyncd
systemctl enable lsyncd

常见问题

日志里显示:

Error: Terminating since out of inotify watches.
Consider increasing /proc/sys/fs/inotify/max_user_watches

df -i

cat /proc/sys/fs/inotify/max_user_watches

#修改系统配置
echo "99999999" > /proc/sys/fs/inotify/max_user_watches

vi /etc/sysctl.conf 
fs.inotify.max_user_watches=99999999

sysctl -p

你可能感兴趣的:(运维,junit,lsyncd文件实时同步,linux)