rsync+inotify实现数据实时同步实战

Linux 内核从 2.6.13 版本开始提供了 inotify 通知接口,用来监控文件系统的各种变化情况,如文件存取、删除、移动等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。可以监控某个用户,什么时间,做了什么动作!利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。   

使用 rsync 工具与 inotify 机制相结合,可以实现触发式备份(实时同步),只要原始位置的文档发生变化,则立即启动增量备份操作,否则处于静态等侍状态,这样一来,就避免了按固定周期备份进存在的延迟性、周期过密等问题。

软件下载地址:http://sourceforge.net/projects/inotify-tools/            #notify-tools-3.13

或者到Linux公社资源站下载:

------------------------------------------分割线------------------------------------------

具体下载目录在 /2016年资料/11月/29日/rsync+inotify实现数据实时同步实战/

------------------------------------------分割线------------------------------------------

试验要求:将test2主机/var/www/html目录实时同步到test1主机/var/www/html目录中;

试验拓补图

rsync+inotify实现数据实时同步实战

  [root@test2 ~]#uname -r                                                                                  #查看linux内核

   2.6.32-431.el6.x86_64

  [root@test2 ~]#yum -y install xinetd rsync;                                                        #安装rsync服务

    [root@test2 ~]#yum -y install gcc-c++,openssh-clients                                                         

将inotify-tools上传并进行解压安装

  [root@test2 ~]# tar xvf inotify-tools-3.13.tar.gz -C /usr/local/src/                      #解压

  [root@test2 ~]# cd /usr/local/src/inotify-tools-3.13/                                            #切换目录

  [root@test2 inotify-tools-3.13]#./configure --prefix=/usr/local/inotify-tools ;        #编译安装

  [root@test2 inotify-tools-3.13]#make ; make install                                                                                               

测试

使用inotifywait命令监控网站目录/var/www/html发生的变化,然后在另一个终端向/var/www/html目录下添加文件、移动文件,查看屏幕输出结果。

  inotifywait常用参数:

  -e  用来指定要监控哪些事件。这些事件包括: create 创建,move 移动,delete 删除,modify 修改文件内容,attrib 属性更改。

  -m 表示持续监控

  -r  表示递归整个目录

  -q 表示简化输出信息

[root@test2 ~]# inotifywait -mrq -e create,move,delete,modify /var/www/html/

然后再另开一终端,进入/var/www/html目录进行新增删除测试;

  [root@test2 html]#mkdir test

  [root@test2 html]#touch test.txt

  [root@test2 html]#rm -rf test.txt

rsync+inotify实现数据实时同步实战     

创建触发式脚本自动同步数据

ssh免密码登录

  [root@test2~]#ssh-keygen                                                                                              #生成密钥

  [root@test2~]#ssh-copy-id [email protected]                                                              #发布公钥

编写脚本

  [root@test2~]#vim inotify.sh

  #!/bin/bash

  SRC=/var/www/html

  [email protected]:/var/www/html

  inotifywait -mrq -e modify,delete,create,attrib ${SRC}|while read D E F

                      do

                          /usr/bin/rsync -avz --delete $SRC $DST

                      done

  [root@test2~]#chmod +x inotify.sh                                                    #添加执行权限

测试自动同步的效果

  [root@test2~]#./inotify.sh                                                                  #运行脚本

  另开一终端进行新增删除修改动作

  [root@test2~]#cd /var/www/html/

  [root@test2~]#touch bb.txt

然后登录到192.168.1.190服务器进行查看备份目录

rsync+inotify实现数据实时同步实战

Rsync 的详细介绍请点这里
Rsync 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-11/137629.htm