Rsync+inotify实时数据同步

1、rsync+inotify组合的起源

    Rsync(remote sync)远程同步工具,通过Rsync可以实现对远程服务器数据的增量备份同步,但Rsync自身也有瓶颈,同步数据时,Rsync采用核心算法对远程服务器的文件进行对比,只进行差异同步。我们可以想象一下,如果服务器的文件数量达到了百万甚至千万量级,那么文件对比将是非常耗时的。而且发生变化的往往是其中很少的一部分,这是非常低效的方式。inotify的出现,可以缓解Rsync不足之处,取长补短。

2、inotify简介

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

3、Rsync+inotify数据实时同步原理图

rsync+inotify.png4、inotify实施准备

    前提是rsync daemon服务配置成功,可以在客户端推送拉取数据,然后才能配置inotify服务。

    Rsync daemon服务配置:

    https://blog.51cto.com/13716812/2396183

5、inotify开始安装

1)查看系统是否支持inotify

[root@inotify /]# uname -r
2.6.32-431.el6.x86_64
[root@inotify /]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 11月 27 17:43 max_queued_events
-rw-r--r-- 1 root root 0 11月 27 17:43 max_user_instances
-rw-r--r-- 1 root root 0 11月 27 17:43 max_user_watches

2)下载inotify源码包

[root@inotify tools]# pwd
/home/oldboy/tools
[root@inotify tools]# wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@inotify tools]# ll
总用量 768
-rw-r--r-- 1 root root 389473 1月   1 2008 inotify-tools-3.13.tar.gz

3)编译安装inotify

[root@inotify tools]#tar zxf inotify-tools-3.13.tar.gz 
[root@inotify tools]#cd inotify-tools-3.13
[root@inotify inotify-tools-3.13]#yum -y install gcc gcc-c++
[root@inotify inotify-tools-3.13]#./configure --prefix=/usr/local/inotify-tools-3.13
[root@inotify inotify-tools-3.13]#make && make install
[root@inotify inotify-tools-3.13]# cd ../
[root@inotify tools]# ln -s /usr/local/inotify-tools-3.13 /usr/local/inotify
[root@inotify tools]# ll /usr/local/inotify
lrwxrwxrwx 1 root root 29 11月 27 18:10 /usr/local/inotify -> /usr/local/inotify-tools-3.13

参数:

--prefix=PATH   指定编译安装的路径

提示:更多的编译参数可以使用./configure –h查看。

编译成功后会生成4个目录,分别是:

[root@inotify inotify]# ls -l

total 16

drwxr-xr-x 2 root root 4096 Nov 27 18:04 bin  #inotify执行命令(二进制)

drwxr-xr-x 3 root root 4096 Nov 27 18:04 include  #inotify程序所需用的头文件

drwxr-xr-x 2 root root 4096 Nov 27 18:04 lib  #动态链接的库文件

drwxr-xr-x 4 root root 4096 Nov 27 18:04 share  #帮助文档

[root@inotify inotify]# tree

.

|-- bin

|   |-- inotifywait

|   `-- inotifywatch

|-- include

|   `-- inotifytools

|       |-- inotify-nosys.h

|       |-- inotify.h

|       `-- inotifytools.h

|-- lib

|   |-- libinotifytools.a

|   |-- libinotifytools.la

|   |-- libinotifytools.so -> libinotifytools.so.0.4.1

|   |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1

|   `-- libinotifytools.so.0.4.1

`-- share

    |-- doc

    |   `-- inotify-tools

    `-- man

        `-- man1

            |-- inotifywait.1

            `-- inotifywatch.1

9 directories, 12 files

[root@inotify inotify]# ./bin/inotifywait --help

inotifywait 3.13

-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  #文件系统被卸载

4)人工测试监控事件

①  测试create(新建)

#监听命令(当前目录创建文件或目录)

[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data

28/11/18 14:28 /data/a.log

28/11/18 14:28 /data/wahaha.txt

#另外连接一个终端,创建文件或目录看监听是否有变化(编辑文件监听也会有变化的)

[root@inotify data]# vim a.log

[root@inotify data]# touch wahaha.txt

②  测试delete(删除)

#监听命令(删除)

[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /data

28/11/18 14:36 /data/a.log

28/11/18 14:36 /data/wahaha.txt

28/11/18 14:37 /data/inotify.log

#另外连接一个终端,删除文件或目录看监听是否有变化

[root@inotify data]# ls

a.log  wahaha.txt

[root@inotify data]# rm a.log

rm:是否删除普通文件 "a.log"y

[root@inotify data]# rm wahaha.txt

rm:是否删除普通空文件 "wahaha.txt"y

[root@inotify data]# ls

[root@inotify data]# touch inotify.log

③  close_write(写)

#监听命令(写入文件)

[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T%w%f' -e create,delete,close_write /data

28/11/18 14:50/data/inotify.log

#另外连接一个终端,写入文件看监听是否有变化

[root@inotify data]# echo 'www' >> inotify.log

[root@inotify data]# cat inotify.log

1aaaa11

www

5)编写inotify监控脚本

[root@inotify scripts]# vim rsync+inotify.sh 

#!/bin/bash
#qq:1431975440
#rsync+inotify
serverID=10.90.3.105
back_path=/data
rsync_module=oldboy
rsync_user=rsync_backup
rsync_passwd=/etc/rsync.password
inotify_path=/opt/inotify/bin/inotifywait

#测试rsync数据传输是否正确
/usr/bin/rsync -avz /dev/null $rsync_user@$serverID::$rsync_module --password-file=${rsync_passwd} >/dev/null 2>&1

[ $? -ne 0 ] && echo "Rsync error ,Please check rsync." exit 2

#监控文件变化及传输数据
$inotify_path -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write,attrib $back_path \
| while  read file
do
        /usr/bin/rsync -avz --progress --delete $back_path/ --timeout=100 $rsync_user@$serverID::$rsync_module --password-file=${rsync_passwd} >/dev/null 2>&1

done

exit 0



6)执行inotify监控脚本(加&符,后台执行)

[root@inotify scripts]# sh rsync+inotify.sh &
[2] 24109

7)查看rsync+inotify.sh脚本是否执行成功

[root@inotify scripts]# ps -ef|grep rsync+inotify
root      17208  17096  0 May17 pts/2    00:00:00 vim rsync+inotify.sh
root      24109  17096  0 11:04 pts/2    00:00:00 sh rsync+inotify.sh
root      24112  24109  0 11:04 pts/2    00:00:00 sh rsync+inotify.sh
root      24114  17096  0 11:04 pts/2    00:00:00 grep --color=auto rsync+inotify

8)编写监控rsync+inotify脚本进程,只保留一个进程即可

编写监控rsync+inotify脚本进程状态,如果脚本没有启动则启动脚本,如果脚本进程过多,则删除进程,只留一个进程即可。

[root@inotify scripts]# vim check_rsync_inotify_status.sh 

#!/bin/bash

if [ `ps -ef|grep -v grep|grep -v vim|grep rsync+inotify.sh|wc -l` -ne 1 ];then
        for n in `ps -ef|grep rsync+inotify.sh|grep -v grep|awk '{print $2}'`;do kill            -9 $n;done
        /usr/bin/sh /server/scripts/rsync+inotify.sh &

fi

9)把监控脚本check_rsync_inotify_status.sh放在定时任务

把监控脚本放在定时任务当中,每分钟执行一次。

[root@inotify scripts]# crontab -e

#check rsync+inotify status
* * * * * /usr/bin/sh /server/scripts/check_rsync_inotify_status.sh

10)检查rsync+inotify进程

[root@inotify scripts]# ps -ef|grep -v grep|grep rsync+inotify
root      24815      1  0 13:31 ?        00:00:00 /usr/bin/sh /server/scripts/rsync+inotify.sh
root      24818  24815  0 13:31 ?        00:00:00 /usr/bin/sh /server/scripts/rsync+inotify.sh

11)测试数据实时同步

①  创建新文件和新目录

查看rsync服务端数据:(空)

[root@rsync-server oldboy]# pwd
/oldboy
[root@rsync-server oldboy]# ll
total 0

查看rsync客户端(inotify)服务器文件或目录,并且创建新文件或目录

[root@inotify data]# pwd
/data
[root@inotify data]# ll
total 0
[root@inotify data]# touch oldboy{01..03}
[root@inotify data]# ls
oldboy01  oldboy02  oldboy03
[root@inotify data]# mkdir data_{a..c}
[root@inotify data]# ll
total 0
drwxr-xr-x. 2 root root 6 May 20 13:39 data_a
drwxr-xr-x. 2 root root 6 May 20 13:39 data_b
drwxr-xr-x. 2 root root 6 May 20 13:39 data_c
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy01
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy02
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy03

查看rsync服务端是否有新建的文件和目录

[root@rsync-server oldboy]# pwd
/oldboy
[root@rsync-server oldboy]# ll
total 0
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_a
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_b
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_c
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy01
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy02
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy03

②  删除文件和目录,修改文件命令和目录名

rsync客户端(inotify)服务器上删除和修改文件或目录

[root@inotify data]# rm -f oldboy01
[root@inotify data]# mv oldboy02 inotify02
[root@inotify data]# rm -rf data_a
[root@inotify data]# mv data_b accp
[root@inotify data]# ll
total 0
drwxr-xr-x. 2 root root 6 May 20 13:39 accp
drwxr-xr-x. 2 root root 6 May 20 13:39 data_c
-rw-r--r--. 1 root root 0 May 20 13:38 inotify02
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy03

rsync服务端上查看数据是否有变化

[root@rsync-server oldboy]# ll
total 0
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 accp
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_c
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 inotify02
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy03

③  向文件中添加数据

rsync客户端(inotify)文件中添加数据

[root@inotify data]# echo wahahahahaha > oldboy03 
[root@inotify data]# echo oldboy > inotify02 
[root@inotify data]# cat oldboy03 
wahahahahaha
[root@inotify data]# cat inotify02 
oldboy

rsync服务端上查看文件内容,是否有添加的新数据

[root@rsync-server oldboy]# ls
accp  data_c  inotify02  oldboy03
[root@rsync-server oldboy]# cat oldboy03 
wahahahahaha
[root@rsync-server oldboy]# cat inotify02 
oldboy

在rsync客户端(inotify)服务器上增删改文件或目录,而rsync服务端上的数据也随之变化,则rsync+inotify实时数据同步成功。


猜你喜欢

转载自blog.51cto.com/13716812/2397299