rsync+inotify組合的起源git
經過rsync能夠實現對遠程服務器數據的增量備份同步,但rsync自身也有瓶頸,同步數據時,rsync採用核心算法對遠程服務器的目標文件進行比對,只進行差別同步。咱們能夠想象一下,若是服務器的文件數量達到了百萬甚至千萬級,那麼文件對比將是很是耗時的。並且發生變化的每每是其中很小的一部分,這是很是低效的方式,inotify的出現,能夠緩解rsync不足之處,取長補短。github
Inotify是創建在rsync上的,首先必需要保證客戶端能夠推送文件算法
[root@eric6 scripts]# rsync -avz /home/liuyalei/tools rsync_backup@10.0.0.250::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,顯示這三個文件就是支持bash
[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軟件ide
[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實時監控腳本spa
[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監控腳步(後臺執行)orm
[root@eric6 scripts]# sh inotify.sh &
6)檢查inotify是否啓動server
[root@eric6 scripts]# ps -ef|grep inotify