[linux] lsyncd同步工具

 环境说明:

192.168.56.101 同步源
192.168.56.102 同步目标
操作系统centos 7
lsyncd项目地址:https://github.com/axkibe/lsyncd

  1.源主机root用户运行同步程序,backup用户验证目标主机;如果不想配密钥验证,同样支持密码验证,在运行时(nodaemon)提示输入或者在配置文件定义password_file参数。

生成密钥对(略),目标主机添加公钥,注意.ssh和密钥的属主权限,以下xxx、yyy不要直接抄,填写你正确的密钥信息。
[root@192.168.56.102 ~]# echo "yyy" > /home/backup/.ssh/authorized_keys

源主机添加私钥
[root@192.168.56.101 ~]# echo "xxx" > /root/.ssh/backup_rsa
[root@192.168.56.101 ~]# chmod 600 /root/.ssh/backup_rsa
验证登录
[root@192.168.56.101 ~]# ssh -i /root/.ssh/backup_rsa backup@192.168.56.102

  2.源主机安装lsyncd,修改配置文件

安装lsyncd之前先安装epel-release
[root@192.168.56.101 ~]# yum install -y lsyncd
[root@192.168.56.101 ~]# rpm -qa | grep lsyncd
lsyncd-2.2.2-1.el7.x86_64
[root@192.168.56.101 ~]# 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       = "backup@192.168.56.102",
  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     = "backup@192.168.56.102:/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.源主机启动服务,看日志,目标主机检查文件同步

[root@192.168.56.101 ~]# systemctl start lsyncd

 

单源多目标需求可参考

https://www.stephenrlang.com/2015/12/how-to-install-and-configure-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

 

[root@HD1g-static-website ~]# 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 = {
  "backup@172.16.2.104",
  "backup@172.16.2.105",
}
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       = "backup@172.16.2.99",
  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

  

posted @ 2018-04-23 16:00  金枪语  阅读(869)  评论(0编辑  收藏  举报