天天看點

Rsync+Inotify-tools實作資料實時同步

   rsync 是一個快速增量檔案傳輸工具,它可以用于在同一主機備份内部的備分,我們還可以把它作為不同主機網絡備份工具之用。本文主要講述的是如何自架rsync伺服器,以實作檔案傳輸、備份和鏡像。相對tar和wget來說,rsync 也有其自身的優點,比如速度快、安全、高效。

伺服器環境:

源伺服器:192.168.1.10

備份伺服器:192.168.1.1 192.168.1.2 

一:在rsync服務端(備份伺服器)進行以下操作:

1、關閉SELINUX:

vi /etc/selinux/config #編輯防火牆配置檔案

#SELINUX=enforcing #注釋掉

#SELINUXTYPE=targeted #注釋掉

SELINUX=disabled #增加

:wq! #儲存,退出

setenforce 0  #立即生效

2、開啟防火牆tcp 873端口(Rsync預設端口)

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

service iptables save

service iptables restart 

二、在備份伺服器安裝Rsync服務端:

   # yum install rsync   

 >源碼包安裝

 # tar zxvf  rsync-3.1.1.tar.gz

 # cd rsync-3.1.1

 # ./configure --prefix=/usr/local/rsync/

   # make &&make install   

    注:在用源碼包編譯安裝之前,您得安裝gcc等編譯開具才行;

2、配置檔案:

  rsync的主要有以下三個配置檔案rsyncd.conf(主配置檔案)、rsyncd.secrets(密碼檔案)、rsyncd.motd(rysnc伺服器資訊)

 # touch /etc/rsyncd.conf                 #建立rsyncd.conf,這是rsync伺服器的配置檔案。

# touch /etc/rsyncd.secrets             #建立rsyncd.secrets ,這是使用者密碼檔案。

 # touch /etc/rsyncd.motd

   # vi /etc/rsyncd.conf

      全局參數

     uid = root                            #運作RSYNC守護程序的使用者

     gid = root                            #運作RSYNC守護程序的組

     use chroot = no                  #預設為true,修改為no,增加對目錄檔案軟連接配接的備份

     max connections = 4          # 最大連接配接數為4

     timeout = 600                     #設定逾時時間     

     strict modes =yes               #是否檢查密碼檔案的權限

     port = 873                          #預設端口873

     pid file = /var/run/rsyncd.pid         #pid檔案的存放位置

     lock file = /var/run/rsync.lock       #鎖檔案的存放位置

     log file = /var/log/rsyncd.log      #日志記錄檔案的存放位置     

     motd file = /etc/rsyncd.motd                #rsync啟動時歡迎資訊頁面檔案位置(檔案内容自定義)

     secrets file = /etc/rsyncd.secrets           #密碼和使用者名對比表,密碼檔案自己生成   

     子產品參數

     [backup]                                 #這裡是認證的子產品名,在client端需要指定

     path = /data/html/       #需要做鏡像的目錄,不可缺少!

     comment = web file            #這個子產品的注釋資訊 

     ignore errors                        #可以忽略一些無關的IO錯誤

     read only = no                   

     write only = no

     hosts allow = *                    #允許主機

     #hosts deny = *                  #禁止主機

     list = false                           #不顯示rsync服務端資源清單

     auth users = backup              #認證的使用者名,如果沒有這行則表明是匿名,此使用者與系統無關

     #transfer logging = yes         #使rsync伺服器使用ftp格式的檔案來記錄下載下傳和上載操作在自己單獨的日志中。

     注:關于auth users是必須在伺服器上存在的真實的系統使用者,如果你想用多個使用者以,号隔開,比如auth users = easylife,root

3、建立密碼檔案:

  # echo "backup:123456" > /etc/rsyncd.secrets                   #建立rsync證檔案

   # chmod 600 /etc/rsyncd.secrets

      # chown root:root /etc/rsyncd.secrets

   注:1、将rsyncd.secrets這個密碼檔案的檔案屬性設為root擁有, 且權限要設為600, 否則無法備份成功!           出于安全目的,檔案的屬性必需是隻有屬主可讀。

   

4、設定rsyncd.motd 檔案:

   它是定義rysnc伺服器資訊的,也就是使用者登入資訊。比如讓使用者知道這個伺服器是誰提供的等;類似ftp伺服器登入時,我們所看到的 linuxsir.org ftp ……。 當然這在全局定義變量時,并不是必須的,你可以用#号注掉,或删除;我在這裡寫了一個 rsyncd.motd的内容為:

  ++++++++++++++++++++++++++++++++++++++++++++++

Welcome to use the mike.org.cn rsync services!

++++++++++++++++++++++++++++++++++++++++++++++

5、 獨立運作 rsync 服務:

     # ln -s /usr/local/rsync/bin/rsync /usr/bin/rsync

     # rsync --daemon                                     #啟動服務

     # echo "/usr/bin/rsync --daemon"  >> /etc/rc.local   #開機自啟動

三、源伺服器配置:

1、建立密碼檔案:

 # echo "123456" > /etc/rsyncd.secrets   #建立rsync證檔案

 # chmod 600 /etc/rsyncd.secrets

   # chown root:root /etc/rsyncd.secrets

2、同步伺服器端檔案到本地

     rsync -avzP --delete --progress --bwlimit=2000  /date/html/  [email protected]::backup --password-file=/etc/rsyncd.secrets >/dev/null 2>&1

   1、編輯/etc/ld.so.conf檔案,添加一行:

   /usr/local/lib

  2、儲存後運作ldconfig。

  3、再啟動運作rsyncd服務運作,問題得到解決。

  (注:ld.so.conf和ldconfig用于維護系統動态連結庫)

四、源伺服器安裝Inotify-tools工具,實時觸發rsync進行同步

1、檢視伺服器核心是否支援inotify

ll /proc/sys/fs/inotify   #列出檔案目錄,出現下面的内容,說明伺服器核心支援inotify

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_instances

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_watches

2、安裝inotify-tools

yum install make  gcc gcc-c++  #安裝編譯工具

inotify-tools下載下傳位址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar zxvf inotify-tools-3.14.tar.gz  

cd inotify-tools-3.14 

./configure --prefix=/usr/local/inotify  

make &&make install  

3、設定系統環境變量,添加軟連接配接

echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh

source /etc/profile.d/inotify.sh  #使設定立即生效

echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf

ln -s /usr/local/inotify/include  /usr/include/inotify

4、修改inotify預設參數(inotify預設核心參數值太小)

檢視系統預設參數值

sysctl -a | grep max_queued_events

結果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

結果是:fs.inotify.max_user_watches = 8192

sysctl -a | grep max_user_instances

結果是:fs.inotify.max_user_instances = 128

修改參數:

sysctl -w fs.inotify.max_queued_events="99999999"

sysctl -w fs.inotify.max_user_watches="99999999"

sysctl -w fs.inotify.max_user_instances="65535"

vi /etc/sysctl.conf #添加以下代碼

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

:wq! #儲存退出

參數說明:

max_queued_events:

inotify隊列最大長度,如果值太小,會出現"** Event Queue Overflow **"錯誤,導緻監控檔案不準确

max_user_watches:

要同步的檔案包含多少目錄,可以用:find /home/www.osyunwei.com -type d | wc -l 統計,必須保證max_user_watches值大于統計結果(這裡/home/www.osyunwei.com為同步檔案目錄)

max_user_instances:

每個使用者建立inotify執行個體最大值

5、建立腳本,實時觸發rsync進行同步

vi /usr/local/inotify/rsync.sh   #編輯,添加以下代碼

======================================

#!/bin/sh

srcdir=/data/html/

dstdir=backup

excludedir=/usr/local/inotify/exclude.list      

rsyncuser=backup

rsyncpassdir=/etc/rsyncd.secrets  

dstip="192.168.1.1,192.168.1.2"                             

for ip in $dstip

do

rsync -avH --port=873 --progress --delete --bwlimit=2000 --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir >/dev/null 2>&1

done

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir |  while read file

echo "  ${file} was rsynced" >> /tmp/rsync.log 2>&1

chmod +x /usr/local/inotify/rsync.sh   #添加腳本執行權限

腳本參數說明:

srcdir=/data/html/  #源伺服器同步目錄

dstdir=backup    #目标伺服器rsync同步目錄子產品名稱

excludedir=/usr/local/inotify/exclude.list   

#不需要同步的目錄,如果有多個,每一行寫一個目錄,使用相對于同步子產品的路徑;

#例如:不需要同步/data/html/目錄下的a目錄和b目錄下面的b1目錄,exclude.list檔案可以這樣寫

a/

b/b1/

rsyncuser=backup  #目标伺服器rsync同步使用者名

rsyncpassdir=/etc/rsyncd.secrets    #目标伺服器rsync同步使用者的密碼在源伺服器的存放路徑

dstip="192.168.1.1,192.168.1.2"     #目标伺服器ip,多個ip用空格分開

/tmp/rsync.log  #腳本運作日志記錄

6、設定腳本開機自動執行

vi /etc/rc.d/rc.local  #編輯,在最後添加一行

sh /usr/local/inotify/rsync.sh & #設定開機自動在背景運作腳本

:wq!  #儲存退出

7、測試inotify實時觸發rsync同步腳本是否正常運作

在源伺服器192.168.21.129上建立檔案test.txt

touch /data/html/test.txt

檢視兩台目标伺服器192.168.1.1,192.168.1.2的/data/html/下是否有test檔案夾

如果以上測試通過,說明inotify實時觸發rsync同步腳本運作正常。

至此,Linux下Rsync+Inotify-tools實作資料實時同步完成。

擴充閱讀:

============================================

inotify參數

-m 是保持一直監聽

-r 是遞歸檢視目錄

-q 是列印出事件

-e create,move,delete,modify,attrib 是指 “監聽 建立 移動 删除 寫入 權限” 事件

rsync參數

-v, --verbose 詳細模式輸出

-q, --quiet 精簡輸出模式

-c, --checksum 打開校驗開關,強制對檔案傳輸進行校驗

-a, --archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgoD

-r, --recursive 對子目錄以遞歸模式處理

-R, --relative 使用相對路徑資訊

-b, --backup 建立備份,也就是對于目的已經存在有同樣的檔案名時,将老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。

--backup-dir 将備份檔案(如~filename)存放在在目錄下。

-suffix=SUFFIX 定義備份檔案字首

-u, --update 僅僅進行更新,也就是跳過所有已經存在于DST,并且檔案時間晚于要備份的檔案。(不覆寫更新的檔案)

-l, --links 保留軟鍊結

-L, --copy-links 想對待正常檔案一樣處理軟鍊結

--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鍊結

--safe-links 忽略指向SRC路徑目錄樹以外的鍊結

-H, --hard-links 保留硬鍊結

-p, --perms 保持檔案權限

-o, --owner 保持檔案屬主資訊

-g, --group 保持檔案屬組資訊

-D, --devices 保持裝置檔案資訊

-t, --times 保持檔案時間資訊

-S, --sparse 對稀疏檔案進行特殊處理以節省DST的空間

-n, --dry-run現實哪些檔案将被傳輸

-W, --whole-file 拷貝檔案,不進行增量檢測

-x, --one-file-system 不要跨越檔案系統邊界

-B, --block-size=SIZE 檢驗算法使用的塊尺寸,預設是700位元組

-e, --rsh=COMMAND 指定使用rsh、ssh方式進行資料同步

--rsync-path=PATH 指定遠端伺服器上的rsync指令所在路徑資訊

-C, --cvs-exclude 使用和CVS一樣的方法自動忽略檔案,用來排除那些不希望傳輸的檔案

--existing 僅僅更新那些已經存在于DST的檔案,而不備份那些新建立的檔案

--delete 删除那些DST中SRC沒有的檔案

--delete-excluded 同樣删除接收端那些被該選項指定排除的檔案

--delete-after 傳輸結束以後再删除

--ignore-errors 及時出現IO錯誤也進行删除

--max-delete=NUM 最多删除NUM個檔案

--partial 保留那些因故沒有完全傳輸的檔案,以是加快随後的再次傳輸

--force 強制删除目錄,即使不為空

--numeric-ids 不将數字的使用者群組ID比對為使用者名群組名

--timeout=TIME IP逾時時間,機關為秒

-I, --ignore-times 不跳過那些有同樣的時間和長度的檔案

--size-only 當決定是否要備份檔案時,僅僅察看檔案大小而不考慮檔案時間

--modify-window=NUM 決定檔案是否時間相同時使用的時間戳視窗,預設為0

-T --temp-dir=DIR 在DIR中建立臨時檔案

--compare-dest=DIR 同樣比較DIR中的檔案來決定是否需要備份

-P 等同于 --partial

--progress 顯示備份過程

-z, --compress 對備份的檔案在傳輸時進行壓縮處理

--exclude=PATTERN 指定排除不需要傳輸的檔案模式

--include=PATTERN 指定不排除而需要傳輸的檔案模式

--exclude-from=FILE 排除FILE中指定模式的檔案,檔案裡面填寫要排除的目錄(一行一個檔案/目錄)

--include-from=FILE 不排除FILE指定模式比對的檔案

--version 列印版本資訊

--address 綁定到特定的位址

--config=FILE 指定其他的配置檔案,不使用預設的rsyncd.conf檔案

--port=PORT 指定其他的rsync服務端口

--blocking-io 對遠端shell使用阻塞IO

-stats 給出某些檔案的傳輸狀态

--progress 在傳輸時現實傳輸過程

--log-format=formAT 指定日志檔案格式

--password-file=FILE 從FILE中得到密碼

--bwlimit=KBPS 限制I/O帶寬,KBytes per second

-h, --help 顯示幫助資訊

參考連結:http://www.osyunwei.com/archives/7435.html

本文轉自奔跑在路上部落格51CTO部落格,原文連結http://blog.51cto.com/qiangsh/1582747如需轉載請自行聯系原作者

qianghong000