Lsyncd监视本地目录树事件监视器接口(inotify或fsevents)。它聚合并将事件组合在一起几秒钟,然后生成一个(或多个)进程来同步这些更改。默认情况下,由rsync实现同步。因此,Lsyncd是一种轻量级的实时镜像解决方案,相对容易安装,不需要新的文件系统或块设备,也不会妨碍本地文件系统的性能。

安装

yum install -y lsyncd

rsync+ssh模式

配置ssh免密登录

ssh-keygen -t rsa
ssh-copy-id [email protected]

配置lsyncd

vim /etc/lsyncd.conf

settings {
    logfile = "/var/log/lsyncd.log",          --日志路径
    statusFile = "/var/log/lsyncd.status",    --状态文件
    pidfile = "/var/run/lsyncd.pid",          --pid文件路径
    statusInterval = 1,                       --状态文件写入最短时间
    nodaemon = false,                         --daemon运行
    maxProcesses = 1,                         --最大进程
    maxDelays = 1,                            --最大延迟
}
sync {
    default.rsyncssh,      --默认rsync+ssh,rsync版本需要升级3以上版本
    source = "/tmp/srctest/",        --源目录
    delete = true,                            --保持完全同步        
    host = "[email protected]",         
    targetdir = "/tmp/dsttest/", --目标目录
    exclude={                 
             ".txt"            --需排除的文件
    },
rsync = {
    binary = "/usr/bin/rsync", --需先安装好rsync
    archive = true,            --归档
    compress = false,          --压缩
    owner = true,              --属主
    perms = true,              --权限
    whole_file = false
    },
ssh = {
    port = 22
    }
}

systemctl restart lsyncd

测试(前往/tmp/srctest对文件进行操作,观察192.168.1.12上的/tmp/dsttest/)

本地模式

lsyncd配置

sync {
    default.rsync,
    source = "/tmp/srctest",
    target = "/tmp/dsttest",
}

rsync-daemon模式

服务端搭建

vim /etc/lsyncd.conf #配置lsyncd

settings {
  logfile = "/var/log/lsyncd/lsyncd.log",
  statusFile = "/var/log/lsyncd/lsyncd.status",
  inotifyMode = "CloseWrite",
  maxProcesses = 1000,
  maxDelays = 200,
}
sync {
  default.rsync,
  source = "/tmp/srctest",
  target = "[email protected]::backup",
  delay = 100,
  rsync = {
    binary = "/usr/bin/rsync",
    password_file = "/etc/images.pas",
    archive = true,
    compress = false,
    verbose = true
  }
}

vim /etc/image.pas #创建密码文件

123456

chmod 600 /etc/images.pas

systemctl restart lsyncd

客户端操作

vim /etc/rsyncd.conf #配置rsync

uid = root
gid = root
use chroot = yes
max connections=0
log file=/var/log/rsyncd/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[backup]
path = /tmp/dsttest/
read only = no
list = yes
auth users = rsyncuser
secrets file = /etc/images.pas

vim /etc/images.pas

rsyncuser:123456

chmod 600 /etc/images.pas

rsync --port=873 --address=192.168.1.12 --daemon # 启动