Linux下inotify監控文件夾狀態,發生變化后觸發rsync同步


1、安裝工具
--inotify
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install

--rsync
下載:http://rsync.samba.org/
tar -zxvf rsync-3.1.0.tar.gz
cd rsync-3.1.0
./configure --prefix=/usr/local/rsync
make && make install

 

2、編寫運行腳本:
[root@192.168.1.1 script]$ vim monitor_dircharge.sh
#!/bin/sh
## -------------------------------------------------

## @監控目錄狀態,有變化后觸發rsync同步
## -------------------------------------------------

srcdir="/data/www/New/html/www"
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T%w%f' -e modify,delete,create,attrib,move ${srcdir} \
| while read file
do
sudo rsync -rlptDvzHS --delete --port=9789 --password-file=/etc/rsyncd.password_client /data/www/New/html/www/ root@192.168.1.2::Server/New/html/www/
done

 

3、啟動腳本
nohup sudo sh /data/script/monitor_dircharge.sh >> /tmp/inotify.log 2>&1 &

 

4、添加開機啟動
echo 'nohup sudo sh /data/script/monitor_dircharge.sh >> /tmp/inotify.log 2>&1 &' >> /etc/rc.local

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM