天天看點

Linux rsync+sersync實作檔案時時同步

一.簡介

sersync是基于Inotify開發的,類似于Inotify-tools的工具;

sersync可以記錄下被監聽目錄中發生變化的(包括增加、删除、修改)具體某一個檔案或某一個目錄的名字;

rsync在同步的時候,隻同步發生變化的這個檔案或者這個目錄(每次發生變化的資料相對整個同步目錄資料來說是很小的,rsync在周遊查找比對檔案時,速度很快),是以,效率很高;

:當同步的目錄資料量不大時,建議使用Rsync+Inotify-tools;當資料量很大(幾百G甚至1T以上)、檔案很多時,建議使用Rsync+sersync。

二.具體操作

作業系統:centos6.6

源伺服器(sersync):192.168.0.1  --> 目标伺服器(rsync):192.168.0.2

目的:使用root使用者,把源伺服器上的/test1目錄下的檔案實時同步到目标伺服器的/test2目錄下

(一)目标伺服器(rsync):192.168.0.2 1、安裝rsync

yum -y install rsync

2、建立rsyncd.conf配置檔案

vi /etc/rsyncd.conf

--------------------------

uid = root

gid = root

max connections = 10

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

[test]     #子產品自定義名稱 ---這個需要與源伺服器Sersync對應

path = /test2 #需要同步到這個目錄

comment = Mirror to test

ignore errors = yes

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

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

hosts allow = 192.168.0.1 #允許通路源伺服器IP

hosts deny = 0.0.0.0/32 #拒絕所有IP連接配接,先允許後拒絕

exclude =/data1 /data2  不同步的目錄

:這裡隻是列出一些配置項而已,其他在配置時按需選擇。

------------------------------

3、啟動服務

/usr/bin/rsync --daemon /etc/rsyncd.conf

(二)源伺服器(sersync):192.168.0.1

2、同步資料

rsync -avzP /test1/ [email protected]::test/  測試同步檔案

--/test1/ 同步目錄

--192.168.0.2 目的伺服器,安裝并配置完畢rsync

--test 子產品自定義名稱,即目的伺服器的rsync配置檔案中配置的

3、安裝sersync工具,實時觸發rsync進行同步

#cd /usr/local/src

#wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

#tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz

    GNU-Linux-x86/

    GNU-Linux-x86/sersync2

    GNU-Linux-x86/confxml.xml

#mv /usr/local/src/GNU-Linux-x86/ /usr/local/sersync/

#vi /usr/local/sersync/confxml.xml

需要修改如下幾個地方:

注:如果目的伺服器的rsync有配置賬号密碼時,這裡需要配置/etc/rsyncd.secret

4、啟動sersync

/usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml

繼續閱讀