rsync遠程同步 +inotify實時同步

rsync是一個開源的快速備份工具,支持增量備份,保持鏈接和權限。負責發起rsync同步操作到客戶機稱爲發起端,而負責響應到服務器稱爲備份源。
rsync往往默認安裝了

[root@localhost ~]# rpm -qa rsync
rsync-3.0.6-5.el6_0.1.i686

1.SSH備份源,通常需要輸入交互驗證密碼,這一定程度上限制了計劃任務的自動執行。所以一般採用SSH備份源到無交互驗證,即使用密鑰對。

[root@rhel6 ~]# ssh-keygen -t rsa                //創建密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):     //回車
Enter passphrase (empty for no passphrase):      //回車
Enter same passphrase again:                    //回車
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
2c:56:1d:bf:af:82:69:4c:19:56:5a:18:7e:af:41:24 root@rhel5
[root@rhel6 ~]# ssh-copy-id [email protected]    //將公鑰發給服務器
[email protected]'s password:            //輸入密碼
Now try logging into the machine, with "ssh '[email protected]'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@localhost 桌面]# ssh [email protected]          //不用密碼登錄
Last login: Sun Jan 25 10:57:48 2015 from 192.168.130.133
[root@rhel6 ~]#

測試遠程備份。

[root@rhel6 ~]# ll /wwwroot/
total 0

在服務器上創建/var/www/html/test文件。

[root@localhost 桌面]# touch /var/www/html/test

回到客戶機上同步服務器

[root@rhel6 ~]#rsync -avz [email protected]:/var/www/html /wwwroot/
receiving incremental file list
./
test
sent 33 bytes received 83 bytes 232.00 bytes/sec
total size is 0  speedup is 0.00
[root@rhel6 ~]#ll /wwwroot/
total 0
-rw-r--r-- 1 root root 0 Jan 25 11:16 test

驗證成功,不用輸入密碼。可寫成腳本加入任務計劃。

2.rsync備份源的無交互驗證:
在服務器上創建test2文件。

touch /var/www/html/test2

回到客戶端
可以使用環境變量RSYNC_PASSWORD來保存密碼。

[root@rhel6 ~]#export RSYNC_PASSWORD=pwd123
[root@rhel6 ~]#rsync -avz [email protected]:/var/www/html/ /wwwroot/
[root@rhel6 ~]#ls /wwwroot/
test test2

驗證成功!!
#######rsync 命令選項#######
rsync [選項] 原始位置 目標位置
-r:遞歸,包含目錄及子目錄
-l:複製符號鏈件文件爲符號鏈件文件
-v:顯示同步過程詳細
-a:歸檔,保留文件權限,屬性
-z:傳輸文件時壓縮
-p:保留文件權限標記
-t:保留文件時間標記
-g:保留文件屬組標記
-o:保留文件屬主標記
-H:保留硬鏈文件
-A:保留ACL屬性
-D:保留設備文件其他特殊文件
--delete:刪除目標位置有而原始位置沒有的文件
--checksum:校對決定是否跳過文件



******rsync+inotify實時同步*******


1.調整inotify內核參數

[root@localhost ~]# vim /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

//根據實際調整,監控數大於監控目標總文件數。


2.安裝inotify-tools工具

[root@localhost ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@localhost ~]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure && make && make install

以監控/var/www/html爲例,先執行inotifywait命令,然後在另一終端在該目錄下修改、創建、移動、刪除等,查看屏幕輸出結果。inotifywait可以監控modify(修改)、create(創建)、move(移動)、delete(刪除)、attrib(屬性更改)等。
終端一:

[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE test3
/var/www/html/ MODIFY test2
/var/www/html/ DELETE test

終端二:

[root@localhost html]# touch test3
[root@localhost html]# cat >> /var/www/html/test2 << end
> 123
> end
[root@localhost html]# rm -rf test

其中終端一選項-mrq -e爲:-e指定監控到事件,-m持續監控,-r遞歸整個目錄,-q簡化輸出信息。


3.編寫觸發同步腳本。
使用inotifywait輸出到監控結果中,每行記錄依次爲目錄、事件、文件。因此可以識別變動情況。

vim /opt/inotify_rsync.sh

 

#!/bin/bash 
inotify_cmd="inotifywait -mrq -e modify,create,move,delete,attrib /var/www/html/" 
rsync_cmd="rsync -avz --delete /var/www/html/  [email protected]:/var/www/html/" 
 
$inotify_cmd | while read DIRECTORY EVENT FILE 
do 
        $rsync_cmd 
done

保存退出
可以編寫多個rsync_cmd1、2、3、.....歸屬不同客戶機,在循環do裏增加相應變量,就可以在服務器發生變化,多臺客戶機自動增刪改。

chmod a+x /opt/inotify_rsync.sh

開機實現監控,並觸發同步

echo "/opt/inotify_rsync.sh" >>/etc/rc.local


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