0
点赞
收藏
分享

微信扫一扫

lsyncd 配合 rsync 实时差异同步节点文件

攻城狮Chova 2022-03-30 阅读 51

文章目录

说明书

  • rsync
    • rsync 是一个开源的实用程序,可提供快速增量文件传输
    • rsync 官网
    • 和 sync 命令是完全两个玩意,sync 是将内存 buff 中的资料强制写入磁盘,rsync 是增量文件传输
  • lsyncd
    • Lsyncd监视本地目录树事件监视器接口( inotify 或 fsevents )
    • 它聚合并组合事件几秒钟,然后生成一个(或多个)进程来同步更改,(默认情况下是 rsync )
    • lsyncd 2.2.1 要求所有源计算机和目标计算机上的 rsync >= 3.1
    • lsyncd github
IPSERVICE/ROLEOS_VERSION
192.168.16.100lsyncd & rsync/serverCentOS-7.6.1810
192.168.16.107rsync/clientCentOS-7.6.1810
192.168.16.108rsync/clientCentOS-7.6.1810

部署客户端

安装 rsync 服务

yum install -y rsync

编写配置文件

mv /etc/rsyncd.conf{,.bak}
vim /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
timeout = 300
[data]
path = /data/
read only = false
list = false
hosts allow = 192.168.16.100
hosts deny = 0.0.0.0/32
auth users = rsync
secrets file = /etc/rsyncd.secrets

创建 rsync 用户

useradd rsync -s /sbin/nologin

创建目录并赋权

mkdir /data
chown -R rsync.rsync /data

创建认证用户和密码文件

echo 'rsync:rsync' > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

启动 rsync

rsync --daemon --config=/etc/rsyncd.conf

查看 rsync 是否启动成功

cat /var/log/rsyncd.log
2022/03/28 15:27:50 [21654] rsyncd version 3.1.2 starting, listening on port 873

部署服务端

安装 rsync 服务

yum install -y rsync

创建 rsync 用户

useradd rsync -s /sbin/nologin
创建目录并赋权
mkdir /data
for i in $(seq 1 10);do mkdir /data/test_$i;echo "this is no.$i" > /data/test_$i/test.log;done
chown -R rsync.rsync /data
创建认证用户和密码文件
echo 'rsync' > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
测试 rsync 文件同步
rsync -avz /data/ rsync@192.168.16.107::data --password-file=/etc/rsyncd.secrets
sending incremental file list
./
test_1/
test_1/test.log
test_10/
test_10/test.log
test_2/
test_2/test.log
test_3/
test_3/test.log
test_4/
test_4/test.log
test_5/
test_5/test.log
test_6/
test_6/test.log
test_7/
test_7/test.log
test_8/
test_8/test.log
test_9/
test_9/test.log

sent 1,022 bytes  received 261 bytes  2,566.00 bytes/sec
total size is 131  speedup is 0.10
客户端查看文件
for i in $(seq 1 10);do cat /data/test_$i/test.log;done
this is no.1
this is no.2
this is no.3
this is no.4
this is no.5
this is no.6
this is no.7
this is no.8
this is no.9
this is no.10

安装 lsyncd 服务

yum install -y epel* && yum install -y lsyncd
编写配置文件
mv /etc/lsyncd.conf{,.bak}
vim /etc/lsyncd.conf
-- 全局配置
settings {
  -- 定义日志文件路径和名称
  logfile = "/var/log/lsyncd/lsyncd.log",
  -- 定义状态文件路径和名称
  statusFile = "/var/log/lsyncd/lsyncd.status",
  -- 指定inotify监控的事件
     -- 默认是"CloseWrite",还可以是"Modify"
  inotifyMode = "CloseWrite",
  -- 最大进程数
  maxProcesses = 8,
  -- 累计到多少所监控的事件激活一次同步,即使后面的sync配置的delay延迟时间还未到
  maxDelays = 1,
  -- true 表示不启用守护进程模式(默认是true)
  nodaemon = false,
}

-- 定义同步的配置
sync {
  -- 使用 rsync 进行目录同步
  default.rsync,
  -- 源目录
  source = "/data/",
  -- 虚拟用户和远程主机ip以及模块名称
     -- 如果是 default.direct ,target 直接写同步到哪个目录即可,不需要写虚拟用户和主机ip以及模块名称
  target = "rsync@192.168.16.107::data",
  -- 排除选项
     -- excludeFrom = "/etc/lsyncd.exclude" 指定列表文件
     -- exclude = { LIST } 指定规则
  exclude = {
  '.**',
  '.git/**',
  '*.bak',
  '*.tmp',
  'runtime/**',
  'cache/**'
  },
  -- 累计事件,默认15秒
     -- 15s内两次修改了同一文件,最后只同步最新的文件
  delay = 15,
  -- rsync 配置
  rsync = {
    -- rsync 二进制文件绝对路径 [使用'whereis rsync'命令可以查看 rsync 二进制文件的绝对路径]
    binary = "/usr/bin/rsync",
    -- 指定密码文件
    password_file = "/etc/rsyncd.secrets",
    -- 是否归档
    archive = true,
    -- 是否压缩传输
       -- 默认是 true ,根据文件大小等因素决定是否开启压缩
    compress = false,
    verbose = false,
    -- 其他参数
       -- bwlimit 限速,单位kb/s
    _extra = {"--bwlimit=200", "--omit-link-times"}
  }
}

sync {
  default.rsync,
  source = "/data/",
  target = "rsync@192.168.16.108::data",
  exclude = {
  '.**',
  '.git/**',
  '*.bak',
  '*.tmp',
  'runtime/**',
  'cache/**'
  },

  delay = 15,
  rsync = {
    binary = "/usr/bin/rsync",
    password_file = "/etc/rsyncd.secrets",
    archive = true,
    compress = false,
    verbose = false,
    _extra = {"--bwlimit=200", "--omit-link-times"}
  }
}
启动 lsyncd 服务
systemctl enable lsyncd
systemctl start lsyncd
测试 lsyncd 功能
ssh 192.168.16.100 "ls /data/"
test_1
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
for i in 107 108;do ssh 192.168.16.$i "ls /data/";done
test_1
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
test_1
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
ssh 192.168.16.100 "rm -rf /data/test_1"
for i in 100 107 108;do ssh 192.168.16.$i "ls /data/";done
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9

68.16.108`)是否也会删除这个目录

ssh 192.168.16.100 "rm -rf /data/test_1"
for i in 100 107 108;do ssh 192.168.16.$i "ls /data/";done
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
test_10
test_2
test_3
test_4
test_5
test_6
test_7
test_8
test_9
举报

相关推荐

0 条评论