rsync+inotify數據同步(四)

rsync+inotify組合的起源

通過rsync可以實現對遠程服務器數據的增量備份同步,但rsync自身也有瓶頸,同步數據時,rsync採用核心算法對遠程服務器的目標文件進行比對,只進行差異同步。我們可以想象一下,如果服務器的文件數量達到了百萬甚至千萬級,那麼文件對比將是非常耗時的。而且發生變化的往往是其中很小的一部分,這是非常低效的方式,inotify的出現,可以緩解rsync不足之處,取長補短。

Inotify是建立在rsync上的,首先必須要保證客戶端可以推送文件

[root@eric6 scripts]# rsync -avz /home/liuyalei/tools  [email protected]::backup --password-file=/etc/rsyns.password
sending incremental file list
sent 2616 bytes  received 22 bytes  5276.00 bytes/sec
total size is 3618169  speedup is 1371.56

1)檢查客戶端系統是否支持inotify,顯示這三個文件就是支持

[root@eric6 scripts]# ll /proc/sys/fs/inotify/
-rw-r--r-- 1 root root 0 9月  29 01:38 max_queued_events
-rw-r--r-- 1 root root 0 9月  29 01:38 max_user_instances
-rw-r--r-- 1 root root 0 9月  29 01:40 max_user_watches

2)下載inotify軟件

[root@eric6 scripts]# cd /home/liuyalei/tools/
[root@eric6 tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

3)解壓安裝inotify軟件

[root@eric6 tools]# tar -zxvf inotify-tools-3.14.tar.gz
[root@eric6 tools]# cd inotify-tools-3.14
[root@eric6 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@eric6 inotify-tools-3.14]# make&&make install

4)編寫inotify實時監控腳本

[root@eric6 tools]# cd /server/scripts/
[root@eric6 scripts]# cat inotify.sh
#!/bin/bash
#para
host01=10.0.0.250   #服務端ip
src=/home/liuyalei/tools   #客戶端同步目錄
dst=backup   #模塊
user=rsync_backup  #虛擬認證用戶
rsync_passfile=/etc/rsync.password   #密碼文件存放位置
inotify_home=/usr/local/inotify-tools-3.14/   #inotify軟件安裝位置
#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 '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
#  rsync -avzP --delete --timeout=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

5)執行inotify監控腳步(後臺執行)

[root@eric6 scripts]# sh inotify.sh &

6)檢查inotify是否啓動

[root@eric6 scripts]# ps  -ef|grep inotify

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