rsync+inotify實時同步數據

一、無差異同步數據

1.首先,在實踐實時同步之前我們先來了解一下rsync無差異同步
無差異推送數據加--delete
1)備份 --delete風險
本地有,遠端就有。本地沒有遠端有也會刪除,服務器端的目錄數據可能會丟失

無差異拉取數據
2)代碼發佈、下載 --delete風險
遠端有,本地就有。遠端沒有的本地有也會刪除,本地的目錄數據可能丟失
rsync+inotify實時同步數據
ps:上圖會將遠程端的/hzftp/test裏的文件完全克隆到客戶端的/data1目錄中


服務端上操作:

多模塊共享配置,只需在服務器端/etc/rsyncd.conf新增模塊目錄地址即可,其他
rsync+inotify實時同步數據


inotify參考資料wiki:https://github.com/rvoicilas/inotify-tools/wiki
inotify實施(切記在客戶端上安裝)

前提:需要rsync daemon已經搭建完成的情況下,可以在服務端上推拉數據
inotifu安裝:
檢查服務端的rsync服務是正常啓用的,並且可以在客戶端往服務端推送文件
檢查內核版本:uname -r
[root@localhost inotify]# ls
max_queued_events max_user_instances max_user_watches
[root@localhost inotify]# ll /proc/sys/fs/inotify
總用量 0
-rw-r--r--. 1 root root 0 6月 27 22:58 max_queued_events #生產中調大一些
-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_instances
-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_watches #隊列大小,生產中調大500000000

然後從互聯網上下載inotify源碼安裝包
下載好後,解壓:tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
[root@localhost inotify]# ll /usr/local/inotify
總用量 4
drwxr-xr-x. 2 root root 43 6月 27 23:10 bin #inotify執行命令
drwxr-xr-x. 3 root root 25 6月 27 23:10 include #inotify所需頭文件
drwxr-xr-x. 2 root root 4096 6月 27 23:10 lib #動態鏈接庫文件
drwxr-xr-x. 4 root root 26 6月 27 23:10 share #幫助文檔

使用inotify來監控目錄文件,當被監控的目錄發生變化會產生輸出:
監聽創建:create
監聽刪除:delete
監聽寫入:close_write
文件屬性:attrib
ps:當create和close_write同時使用時,創建文件會被監控兩遍
======create,delete,close_write同時使用用逗號隔開=======
/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create /backup
rsync+inotify實時同步數據


開發inotify數據同步腳本(rsync客戶端上操作)

創建腳本存放路徑:mkdir -p /server/scripts
進入腳本存放目錄:cd /server/scripts
[root@localhost scripts]# vim inotify.sh

#!/bin/bash```

#para
host01=172.17.0.112
src=/backup
dst=backup
user=backup
rsync_passfile=/etc/rsync.pwd
inotify_home=/usr/local/inotify-tools-3.14/

#judge
if [ ! -e "$src" ]
[ ! -e "${rsync_passfile}" ]
[ ! -e "${inotify_home}bin/inotifywait" ]
[ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi

${inotify_home}/bin/inotifywait -mrq --timefmt '%y%m%d %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
註釋:# rsync -avzP --delete --timout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0


##############################################################
![](http://i2.51cto.com/images/blog/201806/28/7236e98b80e0b85a2b6d998cff15afb9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

執行腳本文件sh -x inotify.sh,觀察遠端共享文件目錄
最後,按照需求將腳本添加到定時任務。

小知識點:

實時同步併發測試:(使用paste命令並列顯示)
[root@localhost backup]# seq 10 >a.log
[root@localhost backup]# echo {a..j}|tr " " "\n" >b.log
[root@localhost backup]# paste a.log b.log >c.log
[root@localhost backup]# cat c.log
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
###########################################
ps:命令解釋:
1.seq按序排列
2.|tr 替換(\n是換行符)
3.paste按列合併

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章