Linux下Rsync+Inotify-tools實現資料即時同步

來源:互聯網
上載者:User

標籤:

地址: http://www.osyunwei.com/archives/7435.html

說明:

作業系統:CentOS 5.X

原始伺服器:192.168.21.129

目標伺服器:192.168.21.127,192.168.21.128

目的:把原始伺服器上/home/www.osyunwei.com目錄即時同步到目標伺服器的/home/www.osyunwei.com下

具體操作:

第一部分:分別在兩台目標伺服器192.168.21.127,192.168.21.128上操作

一、分別在兩台在目標伺服器安裝Rsync服務端

1、關閉SELINUX

vi /etc/selinux/config #編輯防火牆設定檔

#SELINUX=enforcing #注釋掉

#SELINUXTYPE=targeted #注釋掉

SELINUX=disabled #增加

:wq! #儲存,退出

setenforce 0  #立即生效

2、開啟防火牆tcp 873連接埠(Rsync預設連接埠)

vi /etc/sysconfig/iptables #編輯防火牆設定檔

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

:wq! #儲存,退出

/etc/init.d/iptables restart #最後重啟防火牆使配置生效

3、安裝Rsync服務端軟體

yum install rsync xinetd #安裝

vi /etc/xinetd.d/rsync #編輯設定檔,設定開機啟動rsync

disable = no #修改為no

:wq! #儲存退出

/etc/init.d/xinetd start #啟動(CentOS中是以xinetd來管理Rsync服務的)

4、建立rsyncd.conf設定檔

vi /etc/rsyncd.conf #建立設定檔,添加以下代碼

log file = /var/log/rsyncd.log #記錄檔位置,啟動rsync後自動產生這個檔案,無需提前建立

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

lock file = /var/run/rsync.lock  #支援max connections參數的鎖檔案

secrets file = /etc/rsync.pass  #使用者認證設定檔,裡面儲存使用者名稱稱和密碼,後面會建立這個檔案

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

[home_www.osyunwei.com] #自訂名稱

path = /home/www.osyunwei.com/ #rsync服務端資料目錄路徑

comment = home_www.osyunwei.com #模組名稱與[home_www.osyunwei.com]自訂名稱相同

uid = root #設定rsync運行許可權為root

gid = root #設定rsync運行許可權為root

port=873  #預設連接埠

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

read only = no  #設定rsync服務端檔案為讀寫權限

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

max connections = 200 #最大串連數

timeout = 600  #設定逾時時間

auth users = home_www.osyunwei.com_user #執行資料同步的使用者名稱,可以設定多個,用英文狀態下逗號隔開

hosts allow = 192.168.21.129  #允許進行資料同步的用戶端IP地址,可以設定多個,用英文狀態下逗號隔開

hosts deny = 192.168.21.254 #禁止資料同步的用戶端IP地址,可以設定多個,用英文狀態下逗號隔開

:wq!  #儲存,退出

5、建立使用者認證檔案

vi /etc/rsync.pass #設定檔,添加以下內容

home_www.osyunwei.com_user:123456  #格式,使用者名稱:密碼,可以設定多個,每行一個使用者名稱:密碼

:wq!  #儲存,退出

6、設定檔案許可權

chmod 600 /etc/rsyncd.conf  #設定檔案所有者讀取、寫入許可權

chmod 600 /etc/rsync.pass  #設定檔案所有者讀取、寫入許可權

7、啟動rsync

/etc/init.d/xinetd start  #啟動

service xinetd stop   #停止

service xinetd restart #重新啟動

第二部分:在原始伺服器192.168.21.129上操作

一、安裝Rsync用戶端

1、關閉SELINUX

vi /etc/selinux/config #編輯防火牆設定檔

#SELINUX=enforcing #注釋掉

#SELINUXTYPE=targeted #注釋掉

SELINUX=disabled #增加

:wq! #儲存,退出

setenforce 0 #立即生效

2、開啟防火牆tcp 873連接埠(Rsync預設連接埠,做為用戶端的Rsync可以不用開啟873連接埠)

vi /etc/sysconfig/iptables #編輯防火牆設定檔

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

:wq! #儲存,退出

/etc/init.d/iptables restart #最後重啟防火牆使配置生效

系統營運  www.osyunwei.com  溫馨提醒:qihang01原創內容 著作權,轉載請註明出處及原文連結

3、安裝Rsync用戶端軟體

whereis rsync   #查看系統是否已安裝rsync,出現下面的提示,說明已經安裝

rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

yum install  xinetd  #只安裝xinetd即可,CentOS中是以xinetd來管理rsync服務的

yum install rsync xinetd #如果預設沒有rsync,運行此命令進行安裝rsync和xinetd

vi /etc/xinetd.d/rsync #編輯設定檔,設定開機啟動rsync

disable = no #修改為

/etc/init.d/xinetd start #啟動(CentOS中是以xinetd來管理rsync服務的)

4、建立認證密碼檔案

vi /etc/passwd.txt  #編輯檔案,添加以下內容

123456 #密碼

:wq! #儲存退出

chmod 600 /etc/passwd.txt #設定檔案許可權,只設定檔案所有者具有讀取、寫入許可權即可

5、測試原始伺服器192.168.21.129到兩台目標伺服器192.168.21.127,192.168.21.128之間的資料同步

mkdir /home/www.osyunwei.com/ceshi #在原始伺服器上建立測試檔案夾,然後在原始伺服器運行下面2行命令

rsync -avH --port=873 --progress --delete  /home/www.osyunwei.com/  [email protected]192.168.21.127::home_www.osyunwei.com --password-file=/etc/passwd.txt

rsync -avH --port=873 --progress --delete  /home/www.osyunwei.com/  [email protected]192.168.21.128::home_www.osyunwei.com --password-file=/etc/passwd.txt

運行完成後,分別在兩台目標伺服器192.168.21.127,192.168.21.128上查看,在/home/www.osyunwei.com目錄下有ceshi檔案夾,說明資料同步成功。

二、安裝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

備忘:Linux下支援inotify的核心最小為2.6.13,可以輸入命令:uname -a查看核心

CentOS 5.X 核心為2.6.18,預設已經支援inotify

2、安裝inotify-tools

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

inotify-tools:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

上傳inotify-tools-3.14.tar.gz到/usr/local/src目錄下

cd /usr/local/src

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執行個體最大值

系統營運  www.osyunwei.com  溫馨提醒:qihang01原創內容 著作權,轉載請註明出處及原文連結

5、建立指令碼,即時觸發rsync進行同步

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

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

#!/bin/sh

srcdir=/home/www.osyunwei.com/

dstdir=home_www.osyunwei.com

rsyncuser=home_www.osyunwei.com_user

rsyncpassdir=/etc/passwd.txt

dstip="192.168.21.127 192.168.21.128"

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  $srcdir [email protected]$ip::$dstdir --password-file=$rsyncpassdir

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

do

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  $srcdir [email protected]$ip::$dstdir --password-file=$rsyncpassdir

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

done

done

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

chmod +x /usr/local/inotify/rsync.sh   #添加指令碼執行許可權

指令碼參數說明:

srcdir=/home/www.osyunwei.com/  #原始伺服器同步目錄

dstdir=home_www.osyunwei.com    #目標伺服器rsync同步目錄模組名稱

rsyncuser=home_www.osyunwei.com_user  #目標伺服器rsync同步處理的使用者名

rsyncpassdir=/etc/passwd.txt  #目標伺服器rsync同步處理的使用者的密碼在原始伺服器的存放路徑

dstip="192.168.21.127 192.168.21.128"  #目標伺服器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上建立檔案inotify_rsync_ceshi

mkdir /home/www.osyunwei.com/inotify_rsync_ceshi

重新啟動原始伺服器:192.168.21.129

等系統啟動之後,查看兩台目標伺服器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi檔案夾

然後再在原始伺服器192.168.21.129建立檔案夾inotify_rsync_ceshi_new

mkdir /home/www.osyunwei.com/inotify_rsync_ceshi_new

繼續查看兩台目標伺服器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi_new檔案夾

如果以上測試都通過,說明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 顯示協助資訊

Linux下Rsync+Inotify-tools實現資料即時同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.