配置rsync Inotify进行文件实时同步

一、简介

rsync用于网络间数据备份 具备高安全性,能实现增量备份,监控的文件必须扫描 文件量大时扫描花费大量时间 所以使用inotify的异步文件系统监控

调用内核监控 检测到文件的修改 同时rsync同步文件

rpmfind.NET搜索 rsync 找到官网地址为 http://rsync.samba.org/ 可以下载源码包进行安装

二、安装过程

1、模拟环境

  • 源服务器192.168.58.142 目标服务器(可以有多台) 192.168.58.143
  • 目标服务器需要监听服务 等待源服务器(客户端)推送文件

2、演示rsync安装过程

目标服务器rsync安装和配置

yum install rsync xinetd

xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,可以用于管理其他的网络服务

  • 关闭selinux(不关闭有可能抛出rsync: mkstemp “/.a.txt.SqAQVm” (in test) failed: Permission denied (13))
  • 临时关闭 setenforce 0 或者编辑 etc/selinux/config 修改 SELINUX=disabled 永久关闭

编辑/etc/xinted.d/rsync

service rsync  
{  
        disable = no  #将禁用改成no  
        flags           = IPv6  
        socket_type     = stream  
        wait            = no  
        user            = root  
        server          = /usr/bin/rsync  
        server_args     = --daemon  
        log_on_failure  += USERID  
}   

通过官方文档 查看rsync的配置文件格式(http://everythinglinux.org/rsync/)

[root@ha etc]# vi rsyncd.conf  
log file = /var/log/rsyncd.log  #日志文件  
pid file = /var/run/rsyncd.pid  #进程文件  
lock file = /var/run/rsync.lock #同步锁文件  

[simple_path_name]   
   path = /data   #表示要同步的目录  
   comment = My Very Own Rsync Server #表示注释  
   uid = root   #同步有权限的用户名  
   gid = root   #同步有权限的用户组  
   port = 873   #对外监听的端口  
   read only = no  #是否文件目录是只读的  
   list = yes   # 是否显示服务端文件列表  
   auth users = mysync  需要同步的文件需要用到的内部账号 可以在下面参数文件中定义  
   secrets file = /etc/rsyncd.scrt 同步的内部通信的账号和密码文件  

创建 用户密码文件 /etc/rsyncd.scrt 格式为:用户名:密码 可以定义多个用户

mysync:123456  

给该两个文件设置600权限

chmod 600 rsyncd.conf

chmod 600 /etc/rsyncd.scrt 

启动rsync

service xinetd start |  stop |restart  

查看运行状态

[root@ha etc]# service xinetd status  
xinetd (pid  7283) is running...  

查看默认 873端口是否开放

[root@ha etc]# netstat -aon | grep 873  
    tcp        0      0 :::873                      :::*                        LISTEN      off (0.00/0/0)  

尝试在任意机器 使用 telnet 192.168.58.142 873 可以查看配置的日志文件

[root@ha run]# more /var/log/rsyncd.log  
    2017/08/03 12:13:30 [7406] connect from ha1 (192.168.58.143)  
    2017/08/03 12:14:11 [7505] connect from ha1 (192.168.58.143)  

源服务器(192.168.58.143)rsync安装和配置

yum install rsync xinetd

xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,可以用于管理其他的网络服务

编辑/etc/xinted.d/rsync

service rsync  
{  
        disable = no  #将禁用改成no  
        flags           = IPv6  
        socket_type     = stream  
        wait            = no  
        user            = root  
        server          = /usr/bin/rsync  
        server_args     = --daemon  
        log_on_failure  += USERID  
}  
  • 关闭selinux(不关闭有可能抛出rsync: mkstemp “/.a.txt.SqAQVm” (in test) failed: Permission denied (13))
  • 临时关闭 setenforce 0 或者编辑 etc/selinux/config 修改 SELINUX=disabled 永久关闭

编辑/etc/xinted.d/rsync

使用命令测试同步文件-v表示增量备份 -a表示传输文件 /data/ 表示当前机器需要同步到目标服务器的文件目录 :test 表示目标服务器 rsyncd.conf定义的[模块名称]

rsync -avH --port=873 --progress  /data/   mysync@192.168.58.142::test  要求你输入目标服务器用户名密码配置文件中的密码 输入123456即可

多次执行只有一次同步 修改文件后再次尝试

3、演示inotify安装过程

  • rpmfind.net搜索 inotify-tool 搜索到官网 https://github.com/rvoicilas/inotify-tools/wiki
  • 下载 源代码 wget https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

  • inotify只需要在源服务器监听文件的修改状态 然后调用rsync即可 或者使用scp都行

  • 解压后 ./configure –prefix=/usr/local/inotify & make & make install 安装 (安装过rsync 一般gcc都已经安装 如果没有 yum -y install gcc)

  • 安装完成后 /usr/local/inotify/bin下的可执行文件 必须设置到环境变量中

  • vi ~/.bash_profile 添加 或者添加到/etc/rc.local文件中

PATH=$PATH:/usr/local/inotify/bin  
export PATH  

source .bash_profile 执行

查看inotify的三个内核参数是否成功配置

[root@ha1 bin]# ll /proc/sys/fs/inotify    
total 0  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_queued_events  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_user_instances  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_user_watches  

该三个参数的作用为

max_user_instances:用户创建inotify实例最大值  
max_queued_events:inotify产生的事件队列最大长度,如果值太小,会出现错误,导致监控文件不准确  
max_user_watches:监控同步的文件包含的最大目录数,  
可以用:find /data -type d|wc -l 统计,必须保证参数值大于统计结果(/home/rain为同步文件目录)。  

查看查看完整的参数名称

[root@ha1 bin]# sysctl -a | grep max_user_instances  
fs.inotify.max_user_instances = 128  #可以看出参数名 带了前缀 fs.inotify.  

可以通过sysctl -w 参数名=参数值修改 或者编辑 /etc/sysctl.conf 添加参数名=参数值键值对
查看官方教程 https://github.com/rvoicilas/inotify-tools/wiki#info 通过查看/usr/local/inotify/bin目录 发现就两个命令

inotifywait 用于等待一个事件被触发(增删改移动等)  
inotifywatch 监听某个目录后者文件的所有事件 一有事件就触发  

4、inotifywatch 演示

(-e表示监听事件(create创建 access表示读取 delete删除 modify修改) 可以通过inotifywatch –help查看 -t表示监听事件 -r 表示监听目录)

inotifywatch -v -e access -e modify -t 60 -r /data 

60s内尝试读取或者修改文件 过了60s后统计记录就会出现在一个列表中

[root@ha1 bin]# inotifywatch -v -e access -e modify -t 60 -r /data  
Establishing watches...  
Setting up watch(es) on /data  
OK, /data is now being watched.  
Total of 1 watches.  
Finished establishing watches, now collecting statistics.  
Will listen for events for 60 seconds.  
total  access  modify  filename  
4      1       3       /data/  

5、inotifywait演示

该命令 等待一个事件触发 触发后返回状态 此时根据状态调用rsync同步

测试该命令 创建一个脚本文件

vi ~/test.sh
#!/bin/sh  

EVENT=$(inotifywait --format '%e' ~/data)  
[ $? != 0 ] && exit  # 返回值0表示成功 不为0表示出现错误 就退出  
[ "$EVENT" = "MODIFY" ] && echo 'file modified!' 根据变量EVENT判断是哪个事件被触发了   
[ "$EVENT" = "DELETE" ] && echo 'file deleted!'  
chmod +x ~/test.sh
~/test.sh 

发现出现了阻塞 等待一个事件

[root@ha1 bin]# ./test.sh  
Setting up watches.  
Watches established.  

另外一个客户端上执行 删除/data下一个文件

[root@ha1 bin]# ./test.sh  
Setting up watches.  
Watches established.  
file delete  #输出了脚本中 输出的内容  

当然可以通过设置输出输出文件名等信息

  • –format表示时间输出格式 %T表示时间 时间格式–timefmt表示 %d表示天 %m月 %y年 %H 24小时制小时 %M分钟
  • -e表示事件 可以多个 %w表示目录 %f表示文件

[root@ha1 bin]# inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create /data  
Setting up watches.  Beware: since -r was given, this may take a while!  
Watches established.  
03/08/17 15:16 /data/ a.txt  
03/08/17 15:17 /data/ b.txt  

也可以通过while循环 使用管道命令获取到对应的文件目录

inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create /data |  
while read date time dir file  
do  
 echo $file  
done  

6、inotifywait整合rsync备份文件从源服务

有了上面 inotifywait的基础后 只需要在循环中获取到file 调用同步即可

使用 以下脚本测试 所有的时间 (通过inotifywait –help查看)

inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,modify,delete,move /data |  
while read date time dir file event  
do  
 echo "$dir $file $event"  
done  

尝试去修改文件创建目录等操作 控制台输出如下 代码中根据不同的类型进行不同的处理

/data/ cc CREATE,ISDIR  创建目录  
/data/cc/ a.txt CREATE  创建文件  
/data/cc/ a.txt MOVED_FROM 从哪里移动  
/data/cc/ b.txt MOVED_TO   移动到这里  
/data/cc/ b.txt DELETE  删除文件  
/data/ cc DELETE,ISDIR 删除目录  
/data/ bb MOVED_FROM,ISDIR 从目录移动  
/data/ cc MOVED_TO,ISDIR   移动到新目录  

完整代码如下(如果有一个文件被修改就同步该文件就行了 但是实现起来较为复杂 这里直接更新整个目录)还是rsync的环境

srcDir=/data  
echo 123456 > /pass.wd  
chmod 600 /pass.wd  
inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,modify,delete,move $srcDir |  
while read date time dir file event  
do  
 echo "$dir $file $event begin syncing"  
 rsync -avH --port=873 --progress --password-file=/pass.wd  $srcDir mysync@192.168.58.142::test   
done  

目标服务器 192.168.58.143 新建/my.sh 填写上边内容

nohup /my.sh & 运行 测试在/data目录修改数据 是否能同步到 58.142对应的/data目录。

标签:Rsync 发布于:2019-11-15 00:07:22