文章來源:王琦的個人博客-使用OpenResty和Lua實現git pull ,互聯網打雜,喜歡多語言編程,記錄一些知識碎片,分享一些心得。
inotify+rsync進行實時同步流程
安裝inotify
1、更新epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2、yum安裝
yum install -y inotify-tools
3、rpm -ql inotify-tools
下兩個命令
/usr/bin/inotifywait # 實現對數據目錄信息變化的監控
/usr/bin/inotifywatch # 監控數據信息變化,對變化的數據進行統計
inotify命令和事件信息
inotifywait
- -m 保持事件監聽狀態
- -r 遞歸監控
- -q 無用的輸出信息不進行顯示
- –timefmt 設定日期格式
- –format 命令執行過程中輸出格式信息
- -e 指定監控事件的信息
主要用到的事件信息有:create創建,delete刪除,moved_to移入,close_wait修改
比較完整的命令:
inotifywait -mrq --timefmt "%F" --format "%T %w%f 事件信息:%e"
touch test.txt
# 輸出信息
2016-03-25 ./test.txt 事件信息:CREATE
2016-03-25 ./test.txt 事件信息:OPEN
2016-03-25 ./test.txt 事件信息:ATTRIB
2016-03-25 ./test.txt 事件信息:CLOSE_WRITE,CLOSE
比較常用的命令:
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write ./
touch test2.txt
# 輸出信息
./test2.txt
用shell腳本實現inotify+rsync實時同步
#! /bin/bash
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /backup |\
while read line
do
rsync -az --delete /backup/ [email protected]::backup --password-file=/etc/rsync.password
done