rsync+innotify實現數據自動同步
IP規劃:
172.16.0.111
172.16.0.112
1.Rsync的安裝(默認rsync是已經安裝的):
注:本文章的重點是inotify,所以這裏rsync直接使用yum 安裝
yum install –y rsync
2.inotify安裝:
[root@master package]# tar xf inotify-tools-3.14.tar.gz
[root@master package]# cd inotify-tools-3.14
[root@master inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
編寫自動同步腳本
[root@master ~]# cd /usr/local/inotify/
[root@master inotify]# vim inotify.sh
#!/bin/bash
SRC=/tmp/a/ #定義源目錄
DST=/tmp/a #定義目標目錄
DIP=172.16.0.112 #定義目錄IP,如果用多個IP可以放入一個配置文件,用for循環逐個遍歷。
DUSER=root #同步使用的用戶
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib /tmp/a/ | while read x y z
do
rsync $SRC $DUSER@$DIP:$DST -aHt --delete
done
rsync參數說明:
-a 規檔模式,複製目錄、符號鏈接,等價於 -rlptgoD
-H 保留硬鏈接
-t 保存修改時間
更多參數: rsync -h
注:有的時候當網絡負載壓力比較大的時候可以使用rsync進行限速。
rsync可以對複製進行限速:
比如rsync限制爲 300k Bytes/s:
#rsync -auvzP --bwlimit=300 本地的文件 遠程的文件
參數說明(根據情況自行選擇):
v:詳細提示
a:以archive模式操作,複製目錄、符號連接,等價於 -rlptgoD 。
z:壓縮
u:只進行更新,防止本地新文件被重寫,注意兩者機器的時鐘的同時
P:是綜合了--partial --progress兩個參數,
所以此時的rsync支持了斷點續傳
inotify 參數說明:
-m 監控
-r 遞歸
-q 靜默模式
-e 指定你要同步的事件
modify 修改
delete 刪除
create 創建
attrib 屬性
更多參數請查看[root@master bin]# ./inotifywait --help