1. 程式人生 > >rsync+inotify實現伺服器之間檔案實時同步

rsync+inotify實現伺服器之間檔案實時同步

之前做了“ssh信任與scp自動傳輸指令碼”的技術文件,此方案是作為公司裡備份的方法,但在實際的執行中,由於主伺服器在給備份伺服器傳輸的時候,我們的主伺服器需要備份的檔案是實時、不停的產生的,造成不知道主伺服器給備份伺服器傳輸了多少檔案,磁碟空間就那麼大,做備份的原因:一個是為了保持檔案,另外一個是解決主伺服器的磁碟飽滿問題,但由於不知道備份伺服器到底接收了多少檔案,所以主伺服器裡的檔案不敢刪除(如果沒有備份的情況下刪除,問題就嚴重了,我這個是政府的專案,伺服器裡的檔案都是重要的,刪錯了就走人~~~~(>_<)~~~~ ),所以我就採用了rsync+inotify的方式來實時同時伺服器之間的檔案,而且傳輸的過程是加密的,比scp安全多了(即使scp採用ssh信任,用金鑰,也不是萬無一失的)。

以下是我給公司運維做的備份技術文件,分享給大家,希望對大家有幫助。

先介紹一下rsync與inotify,都在在網上找的資料。先宣告下面的rsync與inotify介紹不是我自己寫的。

1、rsync

 與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支援增量備份等優點,通過rsync可以解決對實時性要求不高的資料備份需求,例如定期的備份檔案伺服器資料到遠端伺服器,對本地磁碟定期做資料映象等。
隨著應用系統規模的不斷擴大,對資料的安全性和可靠性也提出的更好的要求,rsync在高階業務系統中也逐漸暴露出了很多不足,首先,rsync同步資料時,需要掃描所有檔案後進行比對,進行差量傳輸。如果檔案數量達到了百萬甚至千萬量級,掃描所有檔案將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步資料,雖然它可以通過linux守護程序的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端資料可能出現不一致,無法在應用故障時完全的恢復資料。基於以上原因,rsync+inotify組合出現了!

2、inotify

 Inotify 是一種強大的、細粒度的、非同步的檔案系統事件監控機制,linux核心從2.6.13起,加入了Inotify支援,通過Inotify可以監控檔案系統中新增、刪除,修改、移動等各種細微事件,利用這個核心介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools就是這樣的一個第三方軟體。
在上面章節中,我們講到,rsync可以實現觸發式的檔案同步,但是通過crontab守護程序方式進行觸發,同步的資料和實際資料會有差異,而inotify可以監控檔案系統的各種變化,當檔案有任何變動時,就觸發rsync同步,這樣剛好解決了同步資料的實時性問題。

接下面我們來開始進行rsync與inotify的安裝、配置、測試。

下面是2個伺服器的結構,分別為主機名、ip、狀態、核心、位數、同步的目錄,並將2臺伺服器均是redhat5.4發行版本。

一、主伺服器(server端,我這裡是nginx) 其中主伺服器需要安裝rsync與inotify,主伺服器作為server,向備份伺服器client傳輸檔案 1、安裝rsync
  1. [[email protected] ~]# cd /usr/src/  
  2. [[email protected] src]# ll  
  3. total 16  
  4. drwxr-xr-x 2 root root 4096 Jan 26  2010 debug  
  5. drwxr-xr-x 2 root root 4096 Jan 26  2010 kernels  
  6. [[email protected] src]# wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz  
  7. [[email protected] src]# tar zxvf rsync-3.0.9.tar.gz  
  8. [[email protected] src]# cd rsync-3.0.9  
  9. [[email protected] rsync-3.0.9]# ./configure --prefix=/usr/local/rsync  
  10. [[email protected] rsync-3.0.9]# make  
  11. [[email protected] rsync-3.0.9]# make install  
2、建立密碼認證檔案
  1. [[email protected] rsync-3.0.9]# cd /usr/local/rsync/  
  2. [[email protected] rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync.passwd   
其中rsync-pwd可以自己設定密碼,rsync.passwd名字也可以自己設定
  1. [[email protected] rsync]# chmod 600 rsync.passwd 
無論是為了安全,還是為了避免出現以下錯誤,密碼檔案都需要給600許可權
  1. password file must not be other-accessible  
  2. continuing without password file  
3、安裝inotify
  1. [[email protected] rsync]# cd /usr/src/  
  2. [[email protected] src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  
  3. [[email protected] src]# tar zxvf inotify-tools-3.14.tar.gz  
  4. [[email protected] src]# cd inotify-tools-3.14  
  5. [[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify  
  6. [[email protected] inotify-tools-3.14]# make  
  7. [[email protected] inotify-tools-3.14]# make install  
4、建立rsync複製指令碼 此項功能主要是將server端的目錄/tmp裡的內容,如果修改了(無論是新增、修改、刪除檔案)能夠通過inotify監控到,並通過rsync實時的同步給client的/tmp裡,下面是通過shell指令碼實現的。
  1. #!/bin/bash  
  2. host=192.168.10.221  
  3. src=/tmp/         
  4. des=web
  5. user=webuser
  6. /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \  
  7. | while read files  
  8. do  
  9. /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src [email protected]$host::$des  
  10. echo "${files} was rsynced" >>/tmp/rsync.log 2>&1  
  11. done  
注意:經過1樓的提示,我發現如果把rsync.log的放到tmp(備份的目錄)或傳送一直複製的問題,所以建議各位吧rsync的日誌放到其他的目錄下(非備份目錄)。 其中host是client的ip,src是server端要實時監控的目錄,des是認證的模組名,需要與client一致,user是建立密碼檔案裡的認證使用者。 把這個指令碼命名為rsync.sh,放到監控的目錄裡,比如我的就放到/tmp下面,並給予764許可權
  1. [[email protected] tmp]# chmod 764 rsync.sh 
然後執行這個指令碼
  1. [[email protected] tmp]# sh /tmp/rsync.sh & 
請記住,只有在備份伺服器client端的rsync安裝並啟動rsync之後,在啟動rsync.sh指令碼,否則有時候會滿屏出現:
  1. rsync: failed to connect to 192.168.10.221: Connection refused (111)  
  2. rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]  
我們還可以把rsync.sh指令碼加入到開機啟動項裡
  1. [[email protected] tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local 

二、備份伺服器(client,我這裡為nginx-backup

1、安裝rsync(備份伺服器只安裝rsync

  1. [[email protected] ~]# cd /usr/src/  
  2. [[email protected] src]# ll  
  3. total 16  
  4. drwxr-xr-x 2 root root 4096 Jan 26  2010 debug  
  5. drwxr-xr-x 2 root root 4096 Jan 26  2010 kernels  
  6. [[email protected] src]# wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz  
  7. [[email protected] src]# tar zxvf rsync-3.0.9.tar.gz  
  8. [[email protected] src]# cd rsync-3.0.9  
  9. [[email protected] rsync-3.0.9]# ./configure --prefix=/usr/local/rsync  
  10. [[email protected] rsync-3.0.9]# make  
  11. [[email protected] rsync-3.0.9]# make install  
2、建立使用者與密碼認證檔案
  1. [[email protected] rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd 
請記住,在server端建立的密碼檔案,只有密碼,沒有使用者名稱;而在備份服務端client裡建立的密碼檔案,使用者名稱與密碼都有。
  1. [[email protected] rsync]# chmod 600 rsync.passwd 
需要給密碼檔案600許可權 3、建立rsync配置檔案
  1. uid = root 
  2. gid = root
  3. use chroot = no
  4. max connections = 10
  5. strict modes = yes
  6. pid file = /var/run/rsyncd.pid  
  7. lock file = /var/run/rsync.lock  
  8. log file = /var/log/rsyncd.log  
  9. [web]  
  10. path = /tmp/  
  11. comment = web file  
  12. ignore errors  
  13. read only = no
  14. write only = no
  15. hosts allow = 192.168.10.220  
  16. hosts deny = *  
  17. list = false
  18. uid = root
  19. gid = root
  20. auth users = webuser
  21. secrets file = /usr/local/rsync/rsync.passwd  
其中web是server服務端裡的認證模組名稱,需要與主伺服器裡的一致,以上的配置我的自己伺服器裡的配置,以供參考。 把配置檔案命名為rsync.conf,放到/usr/local/rsync/目錄裡 啟動rsync
  1. [[email protected] rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf 
如果出現以下問題:
  1. /usr/local/rsync/bin/rsync: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory, 
可以採用下面方法解決
  1. [[email protected] rsync]# whereis libiconv.so.2  
  2. libiconv.so: /usr/local/lib/libiconv.so.2 /usr/local/lib/libiconv.so  
找到所需模組所在的目錄,然後把此目錄新增到/etc/ld.so.conf裡,並更新庫檔案
  1. [[email protected] rsync]# echo "/usr/local/lib/" >> /etc/ld.so.conf  
  2.  [[email protected] rsync]# ldconfig  
我們可以把rsync指令碼加入到開機啟動項裡
  1. [[email protected] rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local 
現在rsync與inotify在server端安裝完成,rsync在備份伺服器client端也安裝完成 下面是server端的tmp檔案情況

下面是client端tmp的檔案情況

接下來我們來做一下測試,在server裡建立個test-rsync檔案,看看client是否能收到

在看client端是否有test-rsync檔案,同時client端的tmp目錄檔案是否與server端的檔案完全一致

可以看到在client端,已經收到了test-rsync檔案,而且client的tmp裡的檔案與server裡tmp的檔案完全相同、檔案數目一致。 現在rsync與inotify的搭建與配置完成了,並實現了伺服器直接資料的實時同步;大家可以根據自己的需要來進行相應的配置。 BTW:在rsync+inotify這種備份方法的時候,我公司遇到了一個問題,那就是主服務已經給備用服務同步完資料了,但主伺服器磁碟看見滿了,需要把已經備份的檔案刪除,但同時在備份伺服器裡保留主伺服器裡的檔案,也就是說主伺服器裡刪除檔案的時候,備份伺服器裡不跟著刪除檔案,我查看了很多英文文件,測試了很多遍,最後找到了一個解決方法,那就是在主伺服器裡,把rsync.sh這個腳本里第9行的--delete引數給去掉,就可以解決這個問題。

在Linux下使用rsync,將遠端目錄下的檔案同步到本地目錄時,可能會出現以下錯誤:

@ERROR: auth failed on module XXX

其中,XXX 表示你的遠端rsync服務模組名稱。

出現這種情況,先檢查你的使用者名稱和密碼是否正確,如果都正確,有一個可能是原因是:遠端rsync伺服器的帳戶密碼檔案的許可權必須為600,例如,你在rsyncd.conf中設定了secrets file = /etc/rsyncd/rsync_pwd

那麼你就必須確保rsync_pwd的訪問許可權為600:

chmod 600 /etc/rsyncd/rsync_pwd

然後問題可能就解決了。

說明:配置檔案的許可權很重要,基本是讀寫許可權即可,當移植到嵌入式平臺的時候有可能會改變許可權,此問題需要注意


相關推薦

centos6.5 rsync+inotify實現伺服器之間檔案實時同步

1. rsync的優點與不足 與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支援增量備份等優點,通過rsync可以解決對實時性要求不高的資料備份需求,例如定期的備份檔案伺服器資料到遠端伺服器,對本地磁碟定期做資料映象等。 隨著應用系統規模的不斷擴大,對資料的安全性和可靠性也提出的更好的

rsync+inotify實現伺服器之間檔案實時同步

之前做了“ssh信任與scp自動傳輸指令碼”的技術文件,此方案是作為公司裡備份的方法,但在實際的執行中,由於主伺服器在給備份伺服器傳輸的時候,我們的主伺服器需要備份的檔案是實時、不停的產生的,造成不知道主伺服器給備份伺服器傳輸了多少檔案,磁碟空間就那麼大,做備份的原因:一個是為了保持檔案,另外一個是解決主伺

配置rsync+inotify實現站點文件實時同步

rep ron 文件實時同步 .tar.gz exp area 調整 資源 centos 一、rsync簡介 rsync是linux系統下的數據鏡像備份工具。可以在不同主機之間鏡像同步整個目錄樹,支持增量備份,保持鏈接和權限,且采用優化的同步算法,在傳輸前執行壓縮,因此非

Linux伺服器檔案實時同步實現

使用場景 現有伺服器A和伺服器B,如果伺服器A的指定目錄(例如 /home/paul/rsync/ )中的內容發生變更(增刪改和屬性變更),實時將這些變更同步到伺服器B的目標目錄中(例如 /home/paul/rsync/ )。 資料映象備份工具Rsync Rsy

centos7中配置rsync+inotify實現自動監控數據同步

ping通 ping down 自動監控 type 用戶密碼 mark RoCE 關閉 rsync服務可以實現數據的同步,但不是自動同步,所以在一些網站服務等, 需要用到inotify進行自動監控。 實驗環境:centos7 兩臺 能互相ping通

linux下rsync+inotify實現兩臺伺服器檔案實時同步

假設兩個伺服器: 192.168.0.1 源伺服器  有目錄 /opt/test/ 192.168.0.2 目標伺服器  有目錄 /opt/bak/test/ 實現的目的就是保持這兩個伺服器某個檔案目錄保持實時同步 實現方式: 通過rsync+inotify-too

【Windows】Windows伺服器之間實現檔案實時同步

Windows伺服器之間實現檔案實時同步? 做公安專案,要實現內外網檔案同步,需要用到同步工具。有一個工具叫 “前置機”。 前置機的原理不是很清楚,應該是通過地址對映過去,然後跟公安內網某個內網IP伺

伺服器架構inotify+rsync檔案實時同步

釋出伺服器上下載 wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 在程式碼釋出伺服器上安裝inotify,執行如下命令 tar xzvf inotify-tools-3.14.t

rsync+inotify-toos實現檔案實時同步和引數詳解

文章摘自:http://lxw66.blog.51cto.com/5547576/1331048 文章摘自:http://www.cnblogs.com/smail-bao/p/5667287.html rsync 幫助文件:http://man.linuxde.ne

Rsync+inotify實現檔案實時同步#附shell指令碼

強烈推薦先仔細看此文 https://segmentfault.com/a/1190000002427568 實驗環境 centos 7.3 vm2:192.168.221.128 同步伺服器 vm1:192.168.221.129 上傳伺服器 關閉seliunx 關閉防火牆 同步伺服器vm2的配置 yum

linux:使用rsyncinotify-tools實現檔案實時同步

首先手頭有兩臺linux伺服器 系統為CentOS 1臺作為主伺服器,另1臺作為同步伺服器(主伺服器上新增/修改/刪除檔案後將會同步給同步伺服器) 首先先檢查主伺服器是否安裝所需的工具 1.檢查rsync是否已經安裝 rpm -qa | grep rs

Rsync+inotify實現文件實時同步

數據同步、rsync、rsync inotify-tools 數據備份、文件備份是運維、DBA等崗位最熟悉不過的話題,這裏不介紹數據庫的備份,簡單介紹一下文件同步工具,這樣的工具有很多,Windows環境下有Goodsync、FreeFileSync等,Linux下rsync、unison等,常用的實時同步,

Centos利用 rsync+inotify實現實時同步

rsync inotify 1.1 inotify介紹inotify是一種強大的、細粒度的、異步的文件系統事件控制機制。linux內核從2.6.13起,加入了inotify支持,通過inotify可以監控文件系統中添加、刪除、修改、移動等各種事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的

rsync+inotify實現代碼實時同步

rsync inotify 實時同步 rsync rsync是lunix系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步。優點:1)、可以鏡像保存整個目錄樹和文件系統。2)、可以很容易做到保持原來文件的權限、

RSYNC04-配置rsync+inotify實現實時同步

cccccc ror 編譯速度 響應 大於 grep 軟件開發 介紹 cdn 1,背景介紹 Linux內核從2.6.13版本開始提供了inotify通知接口,用來監控文件系統的各種變化情況,如文件存取、刪除、移動等。利用這一機制,可以非常方便地實現文件異動告警、

rsync+inotify實現實時同步

rsync inotify 一、前期準備 1、準備兩臺主機,我這裏的系統是rhel7,分別是192.168.4.11和192.168.4.22二、rsync命令的用法:1、基本格式: rsync [選項] 目錄1/ 目錄2/ 註意加斜線和不加斜線的區別,加斜線就是只同步目錄下的文件2、

rsync+inotify實現文件實時同步-步驟詳解

rsync inotify實驗拓撲(centos7下):192.168.80.181 服務器端(主機名www.aa.com)192.168.80.182 客戶端(主機名www.ab.com)1、使用SSH源:安裝rsync,服務端和客戶端同時安裝,只使用客戶端命令就OK了。systemctl stop fir

Rsync+inotify實現數據實時同步

文件差異同步下載軟件wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 2.驗證Rsyncrsync -avz -e ssh /home/wwwroot/default/images/ root

rsync+inotify實現實時同步、增量備份

version rev pts 常用 無需 ESS 成功 dex http 主機A:被備份的源主機主機B:備份的目的主機 在linux內核中,默認的inotify機制提供了三個調控參數 [root@fudanwuxi html]# uname -r 3.10.0-69

rsync+inotify實現上行實時同步

watch data all pre tro 使用 delet 權限 rec rsync:一款開源備份工具;實現不同主機間鏡像同步整個目錄樹;支持增量備份、權限、壓縮等 rsync角色 發起端:負責發起rsync同步,操作客戶機(相當於C端) 備份源:負責響應rsync