前提:rsync 能够正常推送和拉取

1、下载inotify-tools

下载地址:https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

# rpm -qa gcc-c++ gcc     #检查是否安装编译工具

gcc-c++-4.4.7-3.el6.x86_64

gcc-4.4.7-3.el6.x86_64

#yum install make  gcc gcc-c++  #如果没有,安装编译工具

# tar xf inotify-tools-3.14.tar.gz #解压文件

# cd inotify-tools-3.14     #进入到解压目录

#./configure --prefix=/usr/local/inotify  #配置

#make && make install #编译安装

#echo $? #检查是否安装成功 0表示安装成功

0


inotify 常用参数


-r|--recursive    Watch directories recursively.

-q|--quiet        Print less (only print events)

-m|--monitor       Keep listening for events forever. 

-e|--event 

access file or directory contents were read

modify file or directory contents were written

attrib file or directory attributes changed

close_write file or directory closed, after being opened in

          writeable mode

close_nowrite file or directory closed, after being opened in

          read-only mode

close file or directory closed, regardless of read/write mode

open file or directory opened

moved_to file or directory moved to watched directory

moved_from file or directory moved from 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

delete_self file or directory was deleted

unmount file system containing file or directory unmounted


测试脚本1:

#!/bin/bash
####
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib /backup/ \
|  while read file;
        do
        cd /backup/ && rsync -az -R --delete ./ rsync_backup@192.168.234.130::data1 --password-file=/etc/rsync.password >/dev/null 2>&1
       done
exit 0

测试脚本2:

#!/bin/bash
####
/usr/local/inotify/bin/inotifywait -mrq  -e close_write,delete,create,attrib /backup | \
  while read line
        do
        rsync -az  --delete $line rsync_backup@192.168.234.130::data1 --password-file=/etc/rsync.password >/dev/null 2>&1
       done
exit 0

参考资料:http://www.osyunwei.com/archives/7435.html

      https://github.com/rvoicilas/inotify-tools/wiki#getting