是一種異步的檔案系統事件監控機制
inotify程序 rsync daemon 服務
/data/ /data/
寫入一個檔案 通知rsync,執行指令同步
rsync -avz /data/xx [email protected]::hequan
實作軟體
inotify,sersync,lsyncd
實施準備
rsync daemon
用戶端
# uname -r
2.6.32-431.el6.x86_64
# ls -l /proc/sys/fs/inotify/
-rw-r--r-- 1 root root 0 4月 6 23:14 max_queued_events 監控隊列的最大值
-rw-r--r-- 1 root root 0 4月 6 23:14 max_user_instances
-rw-r--r-- 1 root root 0 4月 6 23:14 max_user_watches
下載下傳源碼包
<a></a>
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
cd ../
ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
drwxr-xr-x 2 root root 4096 4月 6 23:21 bin 執行指令
drwxr-xr-x 3 root root 4096 4月 6 23:21 include 頭檔案
drwxr-xr-x 2 root root 4096 4月 6 23:21 lib 動态連結的庫檔案
drwxr-xr-x 4 root root 4096 4月 6 23:21 share 幫助文檔
|-- bin
| |-- inotifywait
| `-- inotifywatch
./bin/inotifywait --help
監控
# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
06/04/16 23:39 /data/xxx
-e create,delete,close_write 并行監控
簡化
# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --format '%w%f' -e create /data
腳本
#!/bin/sh
he="/usr/local/inotify-tools-3.14/bin/inotifywait"
$he -mrq --format '%w%f' -e create,close_write,delete /data | \
while read line
do
rsync -az $line [email protected]::hequan --password-file=/etc/rsync.password
done
# sh -x inotify.sh
# echo {a..d} | tr " " "\n"
a
b
c
d
#paste a b
#split -1 2 a
inotify參數
-m 是保持一直監聽
-r 是遞歸檢視目錄
-q 是列印出事件
-e create,move,delete,modify,attrib 是指 “監聽 建立 移動 删除 寫入 權限” 事件
rsync參數
============================================
-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對檔案傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-R, --relative 使用相對路徑資訊
-b, --backup 建立備份,也就是對于目的已經存在有同樣的檔案名時,将老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。
--backup-dir 将備份檔案(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份檔案字首
-u, --update 僅僅進行更新,也就是跳過所有已經存在于DST,并且檔案時間晚于要備份的檔案。(不覆寫更新的檔案)
-l, --links 保留軟鍊結
-L, --copy-links 想對待正常檔案一樣處理軟鍊結
--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鍊結
--safe-links 忽略指向SRC路徑目錄樹以外的鍊結
-H, --hard-links 保留硬鍊結
-p, --perms 保持檔案權限
-o, --owner 保持檔案屬主資訊
-g, --group 保持檔案屬組資訊
-D, --devices 保持裝置檔案資訊
-t, --times 保持檔案時間資訊
-S, --sparse 對稀疏檔案進行特殊處理以節省DST的空間
-n, --dry-run現實哪些檔案将被傳輸
-W, --whole-file 拷貝檔案,不進行增量檢測
-x, --one-file-system 不要跨越檔案系統邊界
-B, --block-size=SIZE 檢驗算法使用的塊尺寸,預設是700位元組
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行資料同步
--rsync-path=PATH 指定遠端伺服器上的rsync指令所在路徑資訊
-C, --cvs-exclude 使用和CVS一樣的方法自動忽略檔案,用來排除那些不希望傳輸的檔案
--existing 僅僅更新那些已經存在于DST的檔案,而不備份那些新建立的檔案
--delete 删除那些DST中SRC沒有的檔案
--delete-excluded 同樣删除接收端那些被該選項指定排除的檔案
--delete-after 傳輸結束以後再删除
--ignore-errors 及時出現IO錯誤也進行删除
--max-delete=NUM 最多删除NUM個檔案
--partial 保留那些因故沒有完全傳輸的檔案,以是加快随後的再次傳輸
--force 強制删除目錄,即使不為空
--numeric-ids 不将數字的使用者群組ID比對為使用者名群組名
--timeout=TIME IP逾時時間,機關為秒
-I, --ignore-times 不跳過那些有同樣的時間和長度的檔案
--size-only 當決定是否要備份檔案時,僅僅察看檔案大小而不考慮檔案時間
--modify-window=NUM 決定檔案是否時間相同時使用的時間戳視窗,預設為0
-T --temp-dir=DIR 在DIR中建立臨時檔案
--compare-dest=DIR 同樣比較DIR中的檔案來決定是否需要備份
-P 等同于 --partial
--progress 顯示備份過程
-z, --compress 對備份的檔案在傳輸時進行壓縮處理
--exclude=PATTERN 指定排除不需要傳輸的檔案模式
--include=PATTERN 指定不排除而需要傳輸的檔案模式
--exclude-from=FILE 排除FILE中指定模式的檔案
--include-from=FILE 不排除FILE指定模式比對的檔案
--version 列印版本資訊
--address 綁定到特定的位址
--config=FILE 指定其他的配置檔案,不使用預設的rsyncd.conf檔案
--port=PORT 指定其他的rsync服務端口
--blocking-io 對遠端shell使用阻塞IO
-stats 給出某些檔案的傳輸狀态
--progress 在傳輸時現實傳輸過程
--log-format=formAT 指定日志檔案格式
--password-file=FILE 從FILE中得到密碼
--bwlimit=KBPS 限制I/O帶寬,KBytes per second
-h, --help 顯示幫助資訊
本文轉自 295631788 51CTO部落格,原文連結:http://blog.51cto.com/hequan/1764193,如需轉載請自行聯系原作者