1. 程式人生 > >rsync + inotify 實現無密碼驗證實時同步

rsync + inotify 實現無密碼驗證實時同步

rsync

rsync是lunix系統下的資料映象備份工具。使用快速增量備份工具Remote Sync可以遠端同步,支援本地複製,或者與其他SSH、rsync主機同步。

優點:

1)、可以映象儲存整個目錄樹和檔案系統。 2)、可以很容易做到保持原來檔案的許可權、時間、軟硬連結等等。 3)、無須特殊許可權即可安裝。 4)、快速:第一次同步時 rsync 會複製全部內容,但在下一次只傳輸修改過的檔案。rsync 在傳輸資料的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的頻寬。 5)、安全:可以使用scp、ssh等方式來傳輸檔案,當然也可以通過直接的socket連線。 6)、支援匿名傳輸,以方便進行網站鏡象。

缺點:

1)、同步資料,需要掃描所有檔案進行對比,才進行差量傳輸。如果檔案數量達到百萬甚至千萬級,掃描檔案對比檔案將非常耗時,降低了rsync效率。 2)、rsync不能實時地區監測、同步資料。雖然可以通過守護程序方式觸發同步,但兩次動作間有時間差,導致資料不一致,無法應對出現故障時完全恢復資料。 大他把參考

rsync的4種工作模式

1) 拷貝本地檔案 rsync -a /data /backup# 2)從遠端rsync伺服器中拷貝檔案到本地機:rsync -av [email protected]:/databack /opt 3) 從本地機器拷貝檔案到遠端rsync伺服器中 :rsync -av /opt

[email protected]:/databack 4)列出遠端機的檔案列表:rsync -v [email protected]:/databack

rsync選項描述

-n:測試同步過程,不做實際修改 –delete:刪除目標資料夾內多餘的文件 -a:歸檔模式,相當於-rlptgoD -v:顯示詳細操作資訊 -z:傳輸過程中啟用壓縮/解壓

inotify

inotify 是Linux 的一個核心特性,是一種強大的、細粒度的、非同步的檔案系統事件監控機制。它可以監控檔案系統中的新增、刪除、修改、移 動等各種細微事件,並且及時向專門的應用程式發出相關的事件警告,比如刪除、讀、寫和解除安裝操作等。

rsync+inotify

rsync可以實現觸發式的檔案同步,但是通過crontab守護程序方式進行觸發,同步的資料和實際資料會有差異,而inotify可以監控檔案系統的各種變化,當檔案有任何變動時,就觸發rsync同步,這樣剛好解決了rsync同步資料的實時性問題。

inotifywait監控

– inotifywait [選項] 目標資料夾 • 常用命令選項 – -m,持續監控(捕獲一個事件後不退出) – -r,遞迴監控、包括子目錄及檔案 – -q,減少螢幕輸出資訊 – -e,指定監視的 modify、move、create、delete、attrib 等事件類別

本例要求為兩臺Web伺服器svr7、pc207的網頁文件目錄配置映象同步,主要基於inotifywait監控技術實現實時觸發操作,需要完成下列任務: ---------以 svr7 為發起方,原始目錄為 /var/www/html/ ---------以 pc207 為同步目標,基於SSH免密驗證 編寫 inotify+rsync 同步指令碼,驗證實時同步效果

一 :為主機svr7、pc207部署同步目錄 1 確認svr7的目錄內容(向目錄提供測試檔案)

[[email protected] ~]# yum  -y  install  httpd
[[email protected] ~]# touch  /var/www/html/text
[[email protected] ~]# ls /var/www/html
text

2 確認pc207的目錄內容

[[email protected] ~]# yum -y install httpd
[[email protected] ~]# ls  /var/www/html

二 :為svr7配置到pc207的SSH金鑰對驗證,實現免密碼互動 1 檢查當前使用者是否已經有可用的SSH金鑰對檔案

[[email protected] ~]# ls  .ssh/id_*
/root/.ssh/id_rsa  /root/.ssh/id_rsa.pub

如果找不到id_rsa、id_rsa.pub金鑰對檔案,則需要執行下列操作建立

[[email protected] ~]# ssh-keygen                      (# 生成公鑰,私鑰對)
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 yes.
Your public key has been saved in yes[[email protected] ~]#.pub.
The key fingerprint is:
SHA256:Bfz6TOoAEJ0RPQf/YASomPasiJspRe/n5ytO92IhbhE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|  ..o*o+o        |
|   .+ ooo.       |
| o..   o+..      |
|o.o.  E. +.      |
|..o..  .S..      |
|  .o..o o .      |
|o... .oo.*       |
|++  ..=o=.o      |
|=.   =o*+o.      |
+----[SHA256]-----+

[[email protected] ~]# ls  .ssh               // 檢視PC207上的遠端管理檔案  
 known_hosts



2 將當前使用者的SSH公鑰部署到遠端主機

[[email protected] ~]# ssh-copy-id  [email protected]
[[email protected] ~]# ls  .ssh   #當svr7將公鑰傳遞給pc207後,/root/.ssh檔案內多了公鑰authorized_keys
authorized_keys  known_hosts

3 驗證免密碼登入效果

[[email protected] ~]# ssh  [email protected]
Last login: Mon Oct  8 18:42:28 2018 from 192.168.4.254
[[email protected] ~]#                          //確認已免密碼連入遠端主機
[[email protected] ~]# exit                   //退出SSH登入環境
[[email protected] ~]#                             //已返回客戶機

三 在虛擬機器sv7中安裝inotify-tools工具,實現監控目錄內容的變化 1 真機傳遞軟體包到虛擬機器sv7中

[[email protected] ~]# scp /root/桌面/tools.tar.gz  [email protected]:/
[email protected]'s password: 
tools.tar.gz                                  100%  766KB  20.0MB/s   00:00 

2 在虛擬機器svr7中解包tools.tar.gz,並安裝原始碼包

[[email protected] ~]# tar -xf /tools.tar.gz -C /root/
[[email protected] ~]# ls
anaconda-ks.cfg       isync.sh  yes      模板  文件  桌面
initial-setup-ks.cfg  passwd    yes.pub  視訊  下載
inotify-tools-3.13.tar.gz  tools     公共     圖片  音樂                  
[[email protected] ~]# tar -xf /root/tools/inotify-tools-3.13.tar.gz -C .    //解壓縮到當前目錄i
[[email protected] ~]# ls 
anaconda-ks.cfg       isync.sh  yes      模板  文件  桌面
initial-setup-ks.cfg  passwd    yes.pub  視訊  下載
inotify-tools-3.13    tools     公共     圖片  音樂
[[email protected] ~]# cd inotify-tools-3.13/                      //切換到此目錄
[[email protected] inotify-tools-3.13]# yum -y install gcc make    //安裝編譯工具
[[email protected] inotify-tools-3.13]#  ./configure       //完成系統檢測與配置,並指定預設安裝路徑
[[email protected] inotify-tools-3.13]# make                //編譯
[[email protected] inotify-tools-3.13]# make install      //安裝
[[email protected] //確認已沒有監控任務/]# which inotifywait  #查詢命令所對應的程式
/usr/local/bin/inotifywait
[[email protected] inotify-tools-3.13]# ls /usr/local/bin/   
inotifywait  inotifywatch

四 編寫映象同步指令碼並測試效果 1 編寫指令碼檔案/root/isync.sh

[[email protected] inotify-tools-3.13]# cd 
[[email protected] ~]# vim  /root/isync.sh
#!/bin/bash
a="/var/www/html/"
b="rsync -az --delete $a [email protected]:/var/www/html/"
while inotifywait -rqq $a
do//確認已沒有監控任務
      $b
done &

[[email protected] ~]# chmod  +x  /root/isync.sh             //新增執行許可權

2 執行指令碼

[[email protected] ~]# /root/isync.sh
[[email protected] ~]#  pgrep  -l  inotify        //確認任務在執行
7318 inotifywait

3 測試同步效果,在svr7上向/var/www/html/目錄下新增一個測試網頁(觸發同步):

[[email protected] ~]# touch /var/www/html/a.txt
[[email protected] ~]# ls /var/www/html/
a.txt  text

4 在pc207上檢查/var/www/html/目錄,內容應該已經與svr7上的同名目錄一致:

[[email protected] ~]# ls /var/www/html     
a.txt  text           
#  只要svr7的程式一直在執行,就能一直監控並同步pc207上Documentroot下的檔案

5 結束測試後,在svr7上停止監控任務

[[email protected] ~]# pkill  -9  inotify
[[email protected] ~]# /root/isync.sh: 行 7:  7367 已殺死               inotifywait -rqq $a
[[email protected] ~]# pgrep  -l  inotify                   //確認已沒有監控任務