1. 程式人生 > >sersync 多臺服務器、多個目錄的時時同步、備份

sersync 多臺服務器、多個目錄的時時同步、備份

sersync sersync時時同步 同步軟件

一、為什麽要用Rsync+sersync架構?

1、sersync是基於Inotify開發的,類似於Inotify-tools的工具

2、sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或某一個目錄的名字,然後使用rsync同步的時候,只同步發生變化的這個文件或者這個目錄。

二、Rsync+Inotify-tools與Rsync+sersync兩種架構有什麽區別?

1、 Rsync+Inotify-tools

(1):Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增加、刪除、修改),並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來;

(2):rsync在同步的時候,並不知道具體是哪個文件或者哪個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此,效率很低。

2、 Rsync+sersync

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

(2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來說是很小的,rsync在遍歷查找比對文件時,速度很快),因此,效率很高。

小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件很多時,建議使用Rsync+sersync。

三、服務器準備

sersync服務器主: 192.168.230.99

客戶機從1:192.168.230.134

客戶機從2:192.168.230.129

四、搭建sersync服務

1、服務器從1、從2都進行操作步驟相同 :

# yum install -y rsync

# vim /etc/rsyncd.conf

#port=873

uid=root

gid=root

use chroot=no

max connections=2000

timeout = 600

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsync.lock

ignore errors

read only = false

list = false

hosts allow = 192.168.230.0/24

hosts deny = 0.0.0.0/32

auth users=user

secrets file=/etc/rsyncd.passwd

[rsync]

path=/rsync

[www]

path=/data

[mysql]

path=/data/mysql

# vim /etc/rsyncd.passwd

user:654321

# chmod 600 /etc/rsyncd.passwd

# mkdir -p /rsync /data/mysql /data/www/

啟動rsync服務

# rsync --daemon

2、服務器主:

創建需要同步的目錄,也就是模塊中path指向的

# yum install -y rsync

# mkdir -p /rsync /data/mysql /data/www/

先在服務器主上同步測試

# rsync -avzP /rsync/ [email protected]::rsync/ --password-file=/etc/rsyncd.password

# rsync -avzP /rsync/ [email protected]::rsync/ --password-file=/etc/rsyncd.password

# rsync -avzP /rsync/ [email protected]::www/ --password-file=/etc/rsyncd.password

# rsync -avzP /rsync/ [email protected]::www/ --password-file=/etc/rsyncd.password

# rsync -avzP /rsync/ [email protected]::mysql/ --password-file=/etc/rsyncd.password

# rsync -avzP /rsync/ [email protected]::mysql/ --password-file=/etc/rsyncd.password

3、主 下載sersync

# cd /usr/local/src/

64位下載地址:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

32位下載地址:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5_32bit_binary_stable_final.tar.gz

# wget --no-check-certificate https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5_32bit_binary_stable_final.tar.gz

# tar -zxvf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/local/

# cd /usr/local/

# mv GNU-Linux-x86 sersync

# cd sersync/

# mkdir conf bin logs

# mv confxml.xml conf

# mv sersync2 bin/sersync

4、主 配置sersync

先進行備份

# cp conf/confxml.xml /root/sersync.confxml.log$(date +%F)

# vim conf/confxml.xml

找到第24行並修改:

<localpath watch="/rsync">

<remote ip="192.168.230.134" name="rsync"/>

<remote ip="192.168.230.129" name="rsync"/>

</localpath>

<!-- ############################# -->

<localpath watch="/data/mysql">

<remote ip="192.168.230.134" name="mysql"/>

<remote ip="192.168.230.129" name="mysql"/>

</localpath>

<!-- ############################# -->

<localpath watch="/data/www">

<remote ip="192.168.230.134" name="www"/>

<remote ip="192.168.230.129" name="www"/>

</localpath>

<!-- ############################# -->

找到:

<rsync>

<commonParams params="-artuz"/>

<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>

<userDefinedPort start="false" port="874"/><!-- port=874 -->

<timeout start="false" time="100"/><!-- timeout=100 -->

<ssh start="false"/>

</rsync>

修改為:

<rsync>

<commonParams params="-artuz"/>

<auth start="true" users="user" passwordfile="/etc/rsyncd.password"/>

<userDefinedPort start="false" port="874"/><!-- port=874 -->

<timeout start="false" time="100"/><!-- timeout=100 -->

<ssh start="false"/>

</rsync>

##上面配置就是在拼接rsync -avzP /rsync/ [email protected]::rsync/ --password-file=/etc/rsyncd.password 這個同步命令而已

找到:

<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 6

0mins execute once-->

修改為:

<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/>

<!--default every 60mins execute once-->

##日誌位置就是剛才創建的/usr/local/sersync/logs/

保存退出

加入環境變量

# echo 'export PATH=$PATH:/usr/local/sersync/bin' >> /etc/profile

# source /etc/profile

創建同步的測試文件

# touch /rsync/ddd /data/www/ddd /data/mysql/ddd

啟動sersync

# sersync -r -d -o /usr/local/sersync/conf/confxml.xml

## -o 指定配置文件 -d 後臺啟動守護進程 -r 主先與客戶機同步一次,再有新數據就會時時同步

在同步測試後發現只有第一個模塊成功同步了

修改配置

# cp conf/confxml.xml /root/sersync.confxml2.log$(date +%F)

# cd /usr/local/sersync/conf/

# cp confxml.xml rsync_confxml.xml

# cp confxml.xml mysql_confxml.xml

# cp confxml.xml www_confxml.xml

# vim www_confxml.xml

去掉其他模塊內容並修改日誌:

<failLog path="/usr/local/sersync/www_logs/rsync_fail_log.sh" timeToExec

ute="60"/><!--default every 60mins execute once-->

# vim rsync_confxml.xml

去掉其他模塊內容並修改日誌:

<failLog path="/usr/local/sersync/rsync_logs/rsync_fail_log.sh" timeToExec

ute="60"/><!--default every 60mins execute once-->

# vim mysql_confxml.xml

去掉其他模塊內容並修改日誌:

<failLog path="/usr/local/sersync/mysql_logs/rsync_fail_log.sh" timeToExec

ute="60"/><!--default every 60mins execute once-->

殺死之前的sersync 進程,並啟動3個新的模塊的配置

# sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml

# sersync -r -d -o /usr/local/sersync/conf/mysql_confxml.xml

# sersync -r -d -o /usr/local/sersync/conf/rsync_confxml.xml

主上創建文件 測試:

# touch /rsync/ggg /data/www/ggg /data/mysql/ggg

在從上觀察有沒有新的文件ggg

寫入10000個文件批量同步測試:

# for n in `seq 10000`;do echo aaa > /data/mysql/$n.tst;done


sersync 多臺服務器、多個目錄的時時同步、備份