天天看点

inotify结合rsync监控目录的实时变化

Inotigy安装使用

/bin/sh/server/scripts/inotify.sh & 放入rc.local配置文件,自行启动

优点:监控文件系统事件变化,通过同步工具实现数据实时同步,

缺点:并发大于200个文件(10-100k)同步就会延迟,

[root@NFS-Server data]# mkdir /home/zytest01/tools-p

[root@NFS-Server data]# cd  /home/zytest01/tools -p       

[root@NFS-Servertools]#wget   http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@NFS-Server tools]# ll

total 352

-rw-r--r-- 1 root root 358772 Mar 14  2010 inotify-tools-3.14.tar.gz

[root@NFS-Server tools]# tar zxfinotify-tools-3.14.tar.gz

[root@NFS-Server tools]# ls

inotify-tools-3.14 inotify-tools-3.14.tar.gz

[root@NFS-Server tools]# cd inotify-tools-3.14

[root@NFS-Server inotify-tools-3.14]# echo $?            

[root@NFS-Server inotify-tools-3.14]# cd ../

[root@NFS-Server tools]# ln -s/usr/local/inotify-tools-3.14 /usr/local/inotify 目的是方便使用

[root@NFS-Server tools]# ls -l /usr/local/|grepinotify

lrwxrwxrwx  1root root   29 Nov  5 15:16 inotify ->/usr/local/inotify-tools-3.14

drwxr-xr-x  6root root 4096 Nov  5 15:14inotify-tools-3.14

[root@NFS-Server tools]# ls -l /usr/local/inotify-tools-3.14/

total 16

drwxr-xr-x 2 root root 4096 Nov  5 15:14 bin

drwxr-xr-x 3 root root 4096 Nov  5 15:14 include

drwxr-xr-x 2 root root 4096 Nov  5 15:14 lib

drwxr-xr-x 4 root root 4096 Nov  5 15:14 share

#!/bin/bash

/usr/bin/inotifywait -mrq  --format '%w%f' -e create,close_write,delete/backup \

|while read file

do

  cd /backup&&

  rsync -az./ --delete [email protected]::backup \

 --password-file=/etc/rsync.password

done

/usr/bin inotifywait -mrq  --format '%w%f' -e create,close_write,dele

te /backup \

| while read file

  do

    rsync -az"$file" --delete [email protected]::backup--password-file=/etc/rsync.password

  done  脚本2、

以上两个脚本只同步变化的文件

 本文转自 wjw555 51CTO博客,原文链接:http://blog.51cto.com/wujianwei/1962472