rsync+inotify实时同步案例

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

1.1 inotify介绍
2.1 rsync+inotify同步逻辑图
3.1 环境部署
4.1 inotify-slave部署
4.1.1检查是否安装rsync
4.1.2 新建rsync用户及模块目录并更改其用户组
4.1.3 编写rsync daemon配置文件/etc/rsyncd.conf
4.1.4 配置虚拟用户的密码文件
4.1.5 启动rsync 服务
4.1.6 通过inotify-master测试推送
5.1 inotify-master部署
5.1.1 查看当前系统是否支持inotify
5.1.2 inotify安装
5.1.3 inotify之inotifywait命令常用参数详解
5.1.4 编写监控脚本并加载到后台执行
5.1.5 实时同步测试

1.1 inotify介绍
inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

1240

2.1基本架构

3.1 基本环境部署(这里为了避免不兼容的情况出现,采用统一版本的系统)
主机名IP地址系统版本内核版本
inotify master192.168.42.116centos 6.82.6.32-642.el6.x86_64
inotify slave192.168.42.112centos 6.82.6.32-642.el6.x86_64
4.1 inotify-slave部署
这里就是部署rsync服务,rsync daemon工作模式。
4.1.1检查是否安装rsync
 rpm -qa  rsync

1240

结果

4.1.2 新建rsync用户及模块目录并更改其用户组
useradd rsync -s /sbin/nologin  -M
mkdir /backup   
#创建rsync daemon工作模式的模块目录chown rsync.rsync /backup/   
#更改模块目录的用户组ll -d /backup/
4.1.3 编写rsync daemon配置文件/etc/rsyncd.conf
##rsyncd.conf start##
#工作中指定用户(需要指定用户)
uid = rsync
gid = rsync
#相当于黑洞.出错定位
use chroot = no
#有多少个客户端同时传文件
max connections = 200
#超时时间
timeout = 300
#进程号文件
pid file = /var/run/rsyncd.pid
#锁文件
lock file = /var/run/rsync.lock
#日志文件
log file = /var/log/rsyncd.log
#模块开始
#这个模块对应的是推送目录#模块名称随便起[backup]#需要同步的目录path = /backup/
#表示出现错误忽略错误
ignore errors
#表示网络权限可写(本地控制真正可写)read only = false
#这里设置IP或让不让同步list = false
#指定允许的网段
hosts allow = 192.168.42.0/24
#拒绝链接的地址,一下表示没有拒绝的链接。
hosts deny = 0.0.0.0/32#不要动的东西(默认情况)
#虚拟用户
auth users = root
#虚拟用户的密码文件secrets file = /etc/rsync.password
#配置文件的结尾#rsync_config_______________end
4.1.4 配置客户端及虚拟inotify-backup用户的密码文件
  echo "root:123456" >/etc/rsync.password  
  #注:这里是客户端,密码文件得写全,root为rsync同步使用的用户,123456为这个虚拟用户的密码
  chmod 600 /etc/rsync.password 
  #为密码文件提权,增加安全性
4.1.5 启动rsync 服务
rsync --daemon   #启动rsync服务ps -ef |grep rsync
root       2389      1  0 10:15 ?        00:00:00 rsync --daemon
root       2392   2305  0 10:15 pts/0    00:00:00 grep rsync
ss    -tunl
tcp   LISTEN     0      5                                                :::873                                             :::*     
tcp   LISTEN     0      5                                                 *:873                                              *:*
4.1.6 通过inotify-master测试推送
             inotify-master配置密码文件,测试推送
echo "123456" >/etc/rsync.password
#注意:服务器端只要写密码即可,切记。
chmod 600 /etc/rsync.password
echo "hello sjf">test.txt
rsync -avz test.txt root@192.168.42.116::backup --password-file=/etc/rsync.password
sending incremental file list
test.txt

sent 79 bytes  received 27 bytes  42.40 bytes/sec
total size is 10  speedup is 0.09inotify-slave检查:
ll /backup/
total 4-rw-r--r-- 1 rsync rsync 10 Oct 29 10:20 test.txt
cat /backup/test.txt 
hello sjf
5.1 inotify-master部署
注:
inotify是rsync客户端安装和执行的
企业场景压力测试200-300个同步限制,受网卡,磁盘,带宽等的制约。
5.1.1 查看当前系统是否支持inotify
 ll /proc/sys/fs/inotify/
total 0-rw-r--r-- 1 root root 0 Oct 29 10:43 max_queued_events
-rw-r--r-- 1 root root 0 Oct 29 10:43 max_user_instances
-rw-r--r-- 1 root root 0 Oct 29 10:43 max_user_watches#显示这三个文件则证明支持。番外:
   /proc/sys/fs/inotify/max_queued_evnets      
表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
   /proc/sys/fs/inotify/max_user_instances 
表示每一个real user ID可创建的inotify instatnces的数量上限。
   /proc/sys/fs/inotify/max_user_watches 
表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
5.1.2 inotify的安装
inotify可以编译安装也可以直接yum安装
我这里采用的是yum安装的方式
yum -y install inotify-tools
当然我这里也给出编译安装的方法
wget  http://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
tar zxf inotify-tools-3.14.tar.gzcd inotify-tools-3.14./configure --prefix=/usr/local/inotify-3.14 
#配置inotify,并指定安装路径为/usr/local/inotify-3.14make && make install
注意这里安装完成后记得将命令所在路径引入环境变量
5.1.3 inotify之inotifywait命令常用参数详解
inotifywait --help
-r|--recursive   Watch directories recursively. 
#递归查询目录-q|--quiet      Print less (only print events). 
#打印监控事件的信息-m|--monitor   Keep listening for events forever.  Without this option, inotifywait 
will exit after one  event is received.        
#始终保持事件监听状态--excludei <pattern>  Like --exclude but case insensitive.    
#排除文件或目录时,不区分大小写。--timefmt <fmt> strftime-compatible format string for use with %T in--format string. 
#指定时间输出的格式--format <fmt>  Print using a specified printf-like format string; 
read the man page for more details.
#打印使用指定的输出类似格式字符串-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s).  
If omitted, all events are  listened for.  
 #通过此参数可以指定需要监控的事件如下所示:
Events:
access           file or directory contents were read       
#文件或目录被读取。modify           file or directory contents were written    
#文件或目录内容被修改。attrib           file or directory attributes changed      
#文件或目录属性被改变。close            file or directory closed, regardless of read/write mode   
 #文件或目录封闭,无论读/写模式。open             file or directory opened                    
 #文件或目录被打开。moved_to         file or directory moved to watched directory    
 #文件或目录被移动至另外一个目录。
move             file or directory moved to or from watched directory    
#文件或目录被移动另一个目录或从另一个目录移动至当前目录。
create           file or directory created within watched directory     
#文件或目录被创建在当前目录delete           file or directory deleted within watched directory     
#文件或目录被删除
unmount          file system containing file or directory unmounted  
#文件系统被卸载
5.1.4 编写监控脚本并加载到后台执行
#!/bin/bash#sjfhost01=192.168.42.116  
#inotify-slave的ip地址src=/backup/                   
#本地监控的目录dst=backup                     
#inotify-slave的rsync服务的模块名user=rsync_backup              
#inotify-slave的rsync服务的虚拟用户rsync_passfile=/etc/rsync.password           
#本地调用rsync服务的密码文件#inotify_home=/usr/share/doc/inotify-3.14     
#inotify的安装目录inotify_home=/usr   
#这个是yum安装的软件的安装所在目录#judgeif [ -z "$src" ] \
||  [ -z  "${rsync_passfile}" ] \
|| [ -z  "${inotify_home}/bin/inotifywait" ] \
|| [ -z  "/usr/bin/rsync" ];thenecho "Check File and Folder"  
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' 
--format '%T %w%f' -e close_write,delete,create,attrib $src \
 | while read filedo#  rsync -avzP --delete --timeout=100 
 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1cd $src 
 && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst 
 --password-file=${rsync_passfile} >/dev/null 2>&1doneexit 0


 #将脚本加入后台执行
 bash inotify.sh  &
[1] 3357
5.1.5 实时同步测试
inotify-master操作:
cd /backup/for i in `seq 200`;do  touch   $i  ;done  
#创建200个文件ls -l --time-style=full-iso
total 0
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.183409656 +0800 1
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.225409783 +0800 10
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.526410667 +0800 100
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.528410673 +0800 101
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.530410679 +0800 102
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.533410688 +0800 103
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.535410694 +0800 104
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.538410703 +0800 105
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.539410707 +0800 106
-rw-r--r-- 1 root root 0 2016-10-29 11:38:16.552410742 +0800 107
inotify-slave检查
ll  --time-style=full-iso
total 0
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 1
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 10
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 100
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 101
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 102
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 103
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 104
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 105
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 106
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 107
-rw-r--r-- 1 rsync rsync 0 2016-10-29 11:38:16.000000000 +0800 108
这样我们结合inotify的测试就完成了,是不是很简单!



超详细CentOS6.5配置rsync+inotify实现同步


rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用

下行同步

超详细CentOS6.5配置rsync+inotify实现同步

在远程同步任务中,负责发起rsync同步操作的客户机称为发起端
而负责响应来自客户机的rsync同步操作的服务器称为同步源
在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置具有读取权限

超详细CentOS6.5配置rsync+inotify实现同步

inotify是一个Linux特性,它监控文件系统操作,比如读取、写入和创建。inotify反应灵敏,用法非常简单,并且比cron任务的繁忙轮询高效得多。将rsync工具与inotify机制相结合,可以实现实时同步,只要原始位置发生变化,就立即启动增量备份操作,否则处于静默等待状态,这样避免了按固定周期备份时存在的延时性、周期过密性等问题

实验环境

  • 系统环境:centos6.5

  • 同步源IP:192.168.100.96/24

  • 同步源主机名:redhat6_4

  • 发起端IP:192.168.100.97/24

  • 发起端主机名:redhat6_5

  • 共享模块站点:/var/www/html

搭建步骤:

一、准备工作

1、同步源端、发起关闭防火墙、Selinux

[root@redhat6_4 ~]# vim /etc/sysconfig/selinux
超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_4 ~]# chkconfig iptables off #开机启动关闭防火墙
超详细CentOS6.5配置rsync+inotify实现同步

二、配置同步源服务器

1、建立并编辑主配置文件

[root@redhat6_4 ~]# rpm -qa | grep 'rsync'
超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_4 html]# vim /etc/rsyncd.conf #centos6中,配置文件需要手工建立,但是在centos7中系统事先已经建立

全局配置

uid = nobody #指定执行用户(系统伪用户)
gid = nobody #指定用户组
use chroot = yes #将用户禁锢在当前目录下(源目录)
address = 192.168.100.96 #服务器监听地址
port 873 #服务端口
log file = /var/log/rsyncd.log #日志存放位置
pid file = /var/run/rsyncd.pid #进程ID文件存放位置
host allow = 192.168.100.0/24 #允许访问的客户端地址

模块定义

[page] #共享模块名称
path = /var/www/html #共享目录的实际路径
conmment = This is the site directory of a site #描述信息
read only = yes #是否为只读文件,yes代表只读,反之可以写,这里暂时制作备份,定义"yes"即可
dont compress = .gz .bz2 .tgz .zip .rar .z #同步时不再压缩的文件类型
auth users = rcuser #指定授权账户,如果多个用户以空格进行分割,此用户不是系统用户,只是指定一个专门进行的备份的一个账户而已
secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件

2、建立并编辑账户数据文件

[root@redhat6_4 ~]# vim /etc/rsyncd_users.db
超详细CentOS6.5配置rsync+inotify实现同步
[root@redhat6_4 ~]# chmod 600 /etc/rsyncd_users.db #修改权限,这里必须改,否则客户端远程同步会报错,认证有问题,亲测!!!

3、同步源安装httpd并写入测试文件

[root@redhat6_4 ~]# yum -y install httpd[root@redhat6_4 ~]# /etc/init.d/httpd start[root@redhat6_4 ~]# cd /var/www/html/[root@redhat6_4 html]# rm -rf *[root@redhat6_4 html]# echo "<h1>hello world </h1>" > index.html[root@redhat6_4 html]# chmod 777 /var/www/html/ #修改权限最大化,方便测试

4、启动rsync服务

[root@redhat6_4 ~]# rsync --daemon[root@redhat6_4 ~]# netstat -anpt | grep ":873"

超详细CentOS6.5配置rsync+inotify实现同步

三、发起端使用rsync备份工具进行下行同步

1、使用rsync命令实现下行同步

[root@redhat6_5 ~]# rsync -avz rcuser@192.168.100.96::page /root/ #访问同步源模块page,下载到本地/root目录下进行备份

参数解析:

-r:递归模式,包含目录及子目录中的所有文件
-l:对于符号链接文件仍然复制为符号链接文件(软链接)
-v:显示同步过程的详细(verbose)信息
-a:归档模式,保留文件的权限、属性等信息,等同于组合选项"-rlptgoD"
-z:在传输文件时进行压缩(compress)
-p:保留文件的权限标记
-t:保留文件的时间标记
-g:保留文件的属组标记(仅超级用户使用)
-o:保留文件的属主标记(仅超级用户使用)
-H:保留硬连接文件
-A:保留ACL属性信息
-D:保留设备文件及其他特殊文件
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据校验和(而不是文件大小、修改时间)来决定是否跳过文件

超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_5 ~]# mkdir /opt/pub [root@redhat6_5 ~]# rsync -avz rsync://rcuser@192.168.100.96/page /opt/pub/ #再次请求同步源服务将页面备份到/opt/pub/下

超详细CentOS6.5配置rsync+inotify实现同步

2、删除同步源服务器共享模块文件测试

[root@redhat6_4 ~]# cd /var/www/html/[root@redhat6_4 html]# rm -rf *[root@redhat6_4 html]# echo "123" > file1#写入测试文件[root@redhat6_4 html]# echo "456" > file2

3、发起端再次发起下行同步

[root@redhat6_5 ~]# rsync -avzH --delete rcuser@192.168.100.96::page /opt/pub/
超详细CentOS6.5配置rsync+inotify实现同步

4、引入周期性计划任务,通过免密访问进行同步管理

[root@redhat6_5 ~]# vim /etc/server.pass #创建用户密码文件
超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_5 ~]# chmod 600 /etc/server.pass #密码文件权限最小化,否则报错[root@redhat6_5 ~]# crontab -e

超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_5 ~]# /etc/init.d/crond restart[root@redhat6_5 ~]# chkconfig crond on

四、配置rsync+inotify实时同步

1、调整inotify内核参数

[root@redhat6_5 ~]# vim /etc/sysctl.conf

添加以下参数

fs.inotify.max_queued_events = 16384 #监控事件队列
fs.inotify.max_user_instances = 1024 #最多监控实例数
fs.inotify.max_user_watches = 1048576 #每个实例最多监控文件数

[root@redhat6_5 ~]# sysctl -p #重新刷新文件

2、安装inotify-tools

[root@redhat6_5 ~]# yum -y install gcc gcc-c++ #安装编译器[root@redhat6_5 ~]# tar xvfz inotify-tools-3.14.tar.gz  #解压[root@redhat6_5 ~]# cd inotify-tools-3.14 [root@redhat6_5 inotify-tools-3.14]# ./configure #配置[root@redhat6_5 inotify-tools-3.14]# make && make install #编译及编译安装

3、使用inotifywait命令进行监控测试

[root@redhat6_5 ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html #对本地网站目录进行递归式实时监控,监控的事件包括修改、创建、移动、删除……

-m:持续的进行监控
-r:递归整个目录
-q:简化
-e:指定监控的事件
modify:修改
create:创建
move:移动
delete:删除
attrib:属性更改
/var/www/html:对此目录进行监控

4、对网站目录内容进行增、删、改、移

超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_5 ~]# cd /var/www/html/ [root@redhat6_5 html]# touch abc.txt          #创建测试文件[root@redhat6_5 html]# echo "12345" > abc.txt #修改测试文件内容[root@redhat6_5 html]# mv abc.txt fgh.txt     #修改测试文件名称[root@redhat6_5 html]# rm -rf fgh.txt         #删除测试文件

5、查看监控信息

超详细CentOS6.5配置rsync+inotify实现同步

6、编写触发式同步脚本(将本地站点文件上行源端)

[root@redhat6_5 ~]# vim inotify_rsync.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ rcuser@192.168.100.96::page/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done

解析脚本:

①.INOTIFY_CMD:执行实时监控
②.RSYNC_CMD:将本地网站目录上行到源端
③.DIRECTORY EVENT FILE指动态监控的信息赋予给这三个变量变量
DIRECTORY :目录,输出的是/var/www/html
EVENT :发生事件,输出的是如create、modify、move……
FILE:文件的名称,输出的是修改、新建的文件名称
④.循环判断此时此刻rsync是否在运行,如果没有,将本地网站目录上行至源端

[root@redhat6_5 ~]# chmod +x inotify_rsync.sh  #添加执行权限[root@redhat6_5 ~]# echo '/root/inotify_rsync.sh' >> /etc/rc.local #将输出追加至/etc/rc.local中

7、修改同步源配置文件

[root@redhat6_4 ~]# vim /etc/rsyncd.conf
超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_4 run]# kill -9 $(cat /var/run/rsyncd.pid) #杀死rsync进程[root@redhat6_4 run]# rm -rf /var/run/rsyncd.pid         #必须删除进程文件,再次启动,系统自动生成[root@redhat6_4 ~]# rsync --daemon                       #启动rsync

[root@redhat6_4 ~]# ll -d /var/www/html/ #查看站点权限

超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_4 ~]# chown nobody:nobody /var/www/html/ #修改所有者、所属组为nobody系统进程进程用户[root@redhat6_4 ~]# cd /var/www/html/[root@redhat6_4 html]# rm -rf *  #删除站点目录下的所有文件,方便测试

8、发起端运行同步脚本测试

[root@redhat6_5 ~]# chmod 777 /var/www/html/ #发起端修改权限,设置为最大化[root@redhat6_5 html]# chown nobody:nobody /var/www/html/ #修改所有者、所属组为nobody系统进程进程用户[root@redhat6_5 ~]# cd /var/www/html/[root@redhat6_5 html]# rm -rf *      #清空文件方便测试[root@redhat6_5 ~]# sh -x inotify_rsync.sh  #执行同步脚本

超详细CentOS6.5配置rsync+inotify实现同步

[root@redhat6_5 html]# echo "this is test html" &gt; /var/www/html/index.html #创建测试页面
超详细CentOS6.5配置rsync+inotify实现同步

9、同步源查看同步结果

[root@redhat6_4 ~]# cat /var/www/html/index.html

超详细CentOS6.5配置rsync+inotify实现同步