rsync+inotify实现数据实时同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync+inotify实现数据实时同步相关的知识,希望对你有一定的参考价值。

           inotify简介

inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13开始引入,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。

配置inotify需要rsync服务能直接传输数据(免密码传输)。

rsync配置请参考我的另一篇利用rsync进行数据同步。

服务端已配置完毕,inotify配置在客户端。

1.[[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz    ##inotify下载


2.将安装包解压到/usr/local目录。

[[email protected] ~]# tar -xzvf inotify-tools-3.14.tar.gz -C /usr/local/

3.创建安装目录。

[[email protected] local]# mkdir inotify

4.inotify配置

[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify

5.编译于安装

[[email protected] inotify-tools-3.14]# make&&make install

6.写一个脚本实现当客户端/tmp/tuwei/下的文件有变化时,把此目录下的数据自动同步到服务器端的/rsydata下

vim /server/scripts/inotify/inotify.sh

#!/bin/bash

host01=192.168.1.10

src=/tmp/tuwei/

dst=tuwei

user=rsync_backup

rsync_passfile=/etc/rsync.password

inotify_home=/usr/local/inotify


#judge the file and server,it is the basic

if [ ! -e "$src" ] \

|| [ ! -e "${rsync_passfile}" ] \

|| [ ! -e "${inotify_home}/bin/inotifywait" ] \

|| [ ! -e "/usr/bin/rsync" ];

then 

   echo "pls check your file and try again"

    exit 1

fi

${inotify_home}/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e close_write,delete,modify,create,attrib $src \

| while read file

    do

/usr/bin/rsync -az  --delete $src   [email protected]$host01::$dst --password-file=${rsync_passfile}

 done

脚本内容如上。

修改脚本权限。chmod +x  /server/scripts/inotify/inotify.sh

后台执行脚本 sh /server/scripts/inotify/inotify.sh &

[[email protected] tuwei]# sh /server/scripts/inotify/inotify.sh &

[1] 15111

                    测试验证

测试前

服务端 

[[email protected] rsydata]# pwd

/rsydata

[[email protected] rsydata]# ll

total 0

客户端

[[email protected] tuwei]# pwd

/tmp/tuwei

[[email protected] tuwei]# ll

total 0

          客户端添加文件。


[[email protected] tuwei]# touch {1..5}

[[email protected] tuwei]# ll

total 0

-rw-r--r--. 1 root root 0 Apr 16 21:50 1

-rw-r--r--. 1 root root 0 Apr 16 21:50 2

-rw-r--r--. 1 root root 0 Apr 16 21:50 3

-rw-r--r--. 1 root root 0 Apr 16 21:50 4

-rw-r--r--. 1 root root 0 Apr 16 21:50 5

服务端

[[email protected] rsydata]# ll

total 0

-rw-r--r-- 1 root root 0 Apr 16 21:50 1

-rw-r--r-- 1 root root 0 Apr 16 21:50 2

-rw-r--r-- 1 root root 0 Apr 16 21:50 3

-rw-r--r-- 1 root root 0 Apr 16 21:50 4

-rw-r--r-- 1 root root 0 Apr 16 21:50 5

    客户端删除文件1  2


[[email protected] tuwei]# rm -rf 1 2

[[email protected] tuwei]# ll

total 0

-rw-r--r--. 1 root root 0 Apr 16 21:50 3

-rw-r--r--. 1 root root 0 Apr 16 21:50 4

-rw-r--r--. 1 root root 0 Apr 16 21:50 5

服务端

[[email protected] rsydata]# ll

total 0

-rw-r--r-- 1 root root 0 Apr 16 21:50 3

-rw-r--r-- 1 root root 0 Apr 16 21:50 4

-rw-r--r-- 1 root root 0 Apr 16 21:50 5

其他操作类似。



以上是关于rsync+inotify实现数据实时同步的主要内容,如果未能解决你的问题,请参考以下文章

inotify+rsync实现实时同步

inotify和rsync实现数据实时同步

0基础linux运维教程 Rsync结合inotify实现数据实时同步

rsync+inotify实时同步

inotify实时同步数据

rsync+inotify实时同步