环境说明:
192.168.56.101 同步源 192.168.56.102 同步目标 操作系统centos 7 lsyncd项目地址:https://github.com/axkibe/lsyncd
1.源主机root用户运行同步程序,backup用户验证目标主机;如果不想配密钥验证,同样支持密码验证,在运行时(nodaemon)提示输入或者在配置文件定义password_file参数。
生成密钥对(略),目标主机添加公钥,注意.ssh和密钥的属主权限 [[email protected] ~]# echo "yyy" > /home/backup/.ssh/authorized_keys 源主机添加私钥 [[email protected] ~]# echo "xxx" > /root/.ssh/backup_rsa [[email protected] ~]# chmod 600 /root/.ssh/backup_rsa 验证登录 [[email protected] ~]# ssh -i /root/.ssh/backup_rsa [email protected]
2.源主机安装lsyncd,修改配置文件
安装lsyncd之前先安装epel-release [[email protected] ~]# yum install -y lsyncd [[email protected] ~]# rpm -qa | grep lsyncd lsyncd-2.2.2-1.el7.x86_64 [[email protected] ~]# cat /etc/lsyncd.conf ---- -- User configuration file for lsyncd. -- -- Simple example for default rsync, but executing moves through on the target. -- -- For more examples, see /usr/share/doc/lsyncd*/examples/ -- settings { logfile = "/var/log/lsyncd.log", statusFile = "/var/log/lsyncd.status", } sync { default.rsyncssh, source = "/data/web/web1", host = "[email protected]", targetdir = "/data/backup/web/web1", -- 10秒同步一次,默认15秒 delay = 10, rsync = { archive = true, compress = true, verbose = true, rsh = "ssh -o StrictHostKeyChecking=no -i /root/.ssh/backup_rsa", } } sync { default.rsync, source = "/data/web/web2", target = "[email protected]:/data/backup/web/web2", rsync = { archive = true, compress = true, verbose = true, rsh = "ssh -o StrictHostKeyChecking=no -i /root/.ssh/backup_rsa", --_extra = {"-e","ssh -o StrictHostKeyChecking=no","-e ssh -i /root/.ssh/backup_rsa"} } }
3.源主机启动服务,看日志,目标主机检查文件同步
[[email protected] ~]# systemctl start lsyncd
单源多目标需求可参考
1.单目录同步到多机器:
源主机目录/data/web/sh.your.com同步到多机器 172.16.2.104:/data/web/sh.your.com 172.16.2.105:/data/web/sh.your.com
2.多目录同步到相同机器:
源主机多目录 /data/web/resource.your.com /data/web/www.your.com 同步到相同机器 172.16.2.99:/data/backup/web/resource.your.com 172.16.2.99:/data/backup/web/www.your.com
[[email protected] ~]# cat /etc/lsyncd.conf ---- -- User configuration file for lsyncd. -- -- Simple example for default rsync, but executing moves through on the target. -- -- For more examples, see /usr/share/doc/lsyncd*/examples/ -- settings { logfile = "/var/log/lsyncd.log", statusFile = "/var/log/lsyncd.status", } servers = { "[email protected]", "[email protected]", } for _, server in ipairs(servers) do sync { default.rsyncssh, source = "/data/web/sh.your.com", host = server, targetdir = "/data/web/sh.your.com", delay = 5, rsync = { archive = true, compress = true, verbose = true, rsh = "ssh -o StrictHostKeyChecking=no -i /root/.ssh/backup_rsa", } } end sites = { "resource.your.com", "www.your.com", } for _, site in ipairs(sites) do sync { default.rsyncssh, source = "/data/web/" .. site, host = "[email protected]", targetdir = "/data/backup/web/" .. site, delay = 300, rsync = { archive = true, compress = true, verbose = true, rsh = "ssh -o StrictHostKeyChecking=no -i /root/.ssh/backup_rsa", } } end
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/2412.html