Linux中sersync數(shù)據(jù)實(shí)時(shí)同步的方法-創(chuàng)新互聯(lián)

這篇文章主要介紹了Linux中sersync數(shù)據(jù)實(shí)時(shí)同步的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、昆山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

sersync其實(shí)是利用inotify和rsync兩種軟件技術(shù)來實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步功能的,inotify是用于監(jiān)聽sersync所在服務(wù)器上的文件變化,結(jié)合rsync軟件來進(jìn)行數(shù)據(jù)同步,將數(shù)據(jù)實(shí)時(shí)同步給客戶端服務(wù)器。

工作過程:在同步主服務(wù)器上開啟sersync,負(fù)責(zé)監(jiān)聽文件系統(tǒng)的變化,然后調(diào)用rsync命令把更新的文件同步到目標(biāo)服務(wù)器上,主服務(wù)器上安裝sersync軟件,目標(biāo)服務(wù)器上安裝rsync服務(wù)。

Linux中sersync數(shù)據(jù)實(shí)時(shí)同步的方法

1、客戶端配置

[root@localhost2 ~]# cat /etc/rsyncd.conf 
##created by cai at 2018-2-24
uid=rsync
gid=rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[data]
path = /data/
ignore errors
read only = false
list = false
hosts allow = 192.168.181.128:52000/24
hosts deny = 0.0.0.0/32
auth users = rsync_body
secrets file = /etc/rsync.password
[root@localhost2 ~]# ls -ld /data/
drwxrwxrwx. 3 rsync rsync 4096 Feb 24 16:58 /data/

[root@localhost2 ~]# cat /etc/rsync.password 
rsync_body:admin

[root@localhost2 ~]# ls -ld /etc/rsync.password 
-rw-------. 1 root root 17 Feb 24 16:26 /etc/rsync.password  #600權(quán)限

[root@localhost2 ~]# netstat -lntup| grep "rsync"
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1497/rsync 
tcp 0 0 :::873 :::* LISTEN 1497/rsync

2、主服務(wù)器配置

[root@localhost1 ~]# cat /etc/rsync.password 
admin
[root@localhost1 ~]# ls -ld /etc/rsync.password 
-rw------- 1 root root 6 Feb 24 03:54 /etc/rsync.password

3、安裝sersync服務(wù)

采用inotify來對文件進(jìn)行監(jiān)控,當(dāng)監(jiān)控到文件有文件發(fā)生改變的時(shí)候,就會調(diào)用rsync實(shí)現(xiàn)觸發(fā)式實(shí)時(shí)同步!

安裝sersync(注意sersync是工作在rsync的源服務(wù)器上,也就是客戶端上)

[root@salt-client01 ~]# cd /usr/local/src/
[root@salt-client01 src]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@salt-client01 src]# cd /usr/local/
[root@salt-client01 local]# mv GNU-Linux-x86 sersync
[root@salt-client01 local]# cd sersync/
[root@salt-client01 sersync]# mkdir conf bin log
[root@salt-client01sersync]# mv confxml.xml conf
[root@salt-client01sersync]# mv sersync2 bin/sersync

修改配置文件

***********************************30行開始******************************
  <commonParams params="-artuz"/> #-artuz為rsync同步時(shí)的參數(shù)
  <authstart="true" users="rsync的虛擬用戶名(rsync_backup)" passwordfile="rsync的密碼文件"/>
  <userDefinedPort start="true"port="873"/><!-- port=874 -->
  <timeout start="false" time="100"/><!--timeout=100 -->
  <sshstart="false"/>
    ************************************第36行***********************************
    <failLogpath="自己定義的log文件夾(/usr/local/sersync/log)rsync_fail_log.sh"
    timeToExecute="60"/><!--defaultevery 60mins execute once-->
    *******************************************************************************
    *注:若有多個(gè)目錄備份可以穿件多個(gè)配置文件在啟動(dòng)時(shí)的-o參數(shù)中添加即可

[root@salt-client01 conf]# diff confxml.xml confxml.xml.bak
24,25c24,25
<   <localpath watch="/data/">  #data就是本地需要同步的文件夾到服務(wù)器端的目錄
<     <remote ip="192.168.91.166" name="data"/> #data (server的模塊名)是rsync 服務(wù)端的文件夾,也就是推送到服務(wù)器端的目標(biāo)文件夾,可以配置多個(gè),
---
>   <localpath watch="/opt/tongbu">
>     <remote ip="127.0.0.1" name="tongbu1"/>
31c31
<     <auth start="true" users="rsync_body" passwordfile="/etc/rsync.password"/>  #true 才能生效,rsync_body同步時(shí)候虛擬賬號,后面是密碼文件
---
>     <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
33c33
<     <timeout start="true" time="100"/><!-- timeout=100 -->          #true 才能生效
---
>     <timeout start="false" time="100"/><!-- timeout=100 -->
36c36
<   <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #檢測rsync進(jìn)程判斷,沒有自動(dòng)啟
---
>   <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default evePry 60mins execute once-->

啟動(dòng)sersync

[root@salt-client01 src]# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile #聲明環(huán)境變量
[root@salt-client01 src]# source /etc/profile
[root@salt-client01 src]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml #啟動(dòng)
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r   rsync all the local files to the remote servers before the sersync work
option: -d   run as a daemon
option: -o   config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost  host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is  rsync_body
passwordfile is   /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ --timeout=100 rsync_body@192.168.91.166::data --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data #此時(shí)可以看出sersync已經(jīng)啟動(dòng)成功了

#檢測腳本
[root@salt-client01 log]# pwd
/usr/local/sersync/log
[root@salt-client01 log]# vim rsync_fail_log.sh
[root@salt-client01 log]# chmod +x rsync_fail_log.sh
[root@salt-client01 ~]# cat /usr/local/sersync/log/rsync_fail_log.sh
#!/bin/bash
#Purpose: Check sersync whether it is alive
#Author: cai meng zhi
SERSYNC="/usr/local/sersync/bin/sersync2"
CONF_FILE="/usr/local/sersync/conf/confxml.xml"
STATUS=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $STATUS -eq 0 ];
then
    $SERSYNC -d -r -o $CONF_FILE &
else
    exit 0;
fi
腳本寫好以后,添加到計(jì)劃任務(wù)中去
*/1 * * * * /bin/bash /usr/local/sersync/log/rsync_fail_log.sh > /dev/null 2>&1

測試同步:
客戶端新增文件
[root@salt-client01 data]# cp /etc/passwd 192.168.91.156.passwd
[root@salt-client01 data]# ll
total 4
-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd
-rw-r--r-- 1 root root  0 Nov 10 17:27 3
服務(wù)端檢測
[root@salt-master data]# cd /data/
[root@salt-master data]# ll
total 8
-rw-r--r-- 1 root root 1928 Nov 10 18:15 192.168.91.156.passwd #說明已經(jīng)同步過來了
-rw-r--r-- 1 root root  0 Nov 10 17:27 3
drwxr-xr-x 2 root root 4096 Nov 10 17:27 data

客戶端測試刪除
[root@salt-client01 data]# rm rf 192.168.91.156.passwd 
rm: cannot remove `rf': No such file or directory
rm: remove regular file `192.168.91.156.passwd'? y
[root@salt-client01 data]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 10 17:27 3
[root@salt-client01 data]#

服務(wù)器端:
[root@salt-master data]# ll
total 4
-rw-r--r-- 1 root root  0 Nov 10 17:27 3  #說明已經(jīng)刪除掉了
drwxr-xr-x 2 root root 4096 Nov 10 17:27 data

4、常見錯(cuò)誤匯總

錯(cuò)誤一:
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed(90 bytes read so far)
rsync error: error in rsync protocoldata stream (code 12) at io.c(150)
說明:這是因?yàn)槊艽a設(shè)置錯(cuò)了,無法登入成功,檢查一下rsync.pwd,看客服是否匹配。還有服務(wù)器端沒啟動(dòng)rsync 服務(wù)也會出現(xiàn)這種情況。
 
錯(cuò)誤二:
password file must not beother-accessible
continuing without password file
Password:
說明:這是因?yàn)閞syncd.pwdrsyncd.sec的權(quán)限不對,應(yīng)該設(shè)置為600。如:chmod600 rsyncd.pwd
 
錯(cuò)誤三:
@ERROR: chroot failed
rsync: connection unexpectedly closed(75 bytes read so far)
rsync error: error in rsync protocoldata stream (code 12) at io.c(150)
說明:這是因?yàn)槟阍?nbsp;rsync.conf中設(shè)置的 path 路徑不存在,要新建目錄才能開啟同步
 
錯(cuò)誤四:
rsync: failed to connect to218.107.243.2: No route to host (113)
rsync error: error in socket IO (code10) at clientserver.c(104) [receiver=2.6.9]
說明:防火墻問題導(dǎo)致,這個(gè)最好先徹底關(guān)閉防火墻,排錯(cuò)的基本法就是這樣,無論是S還是C,還有ignore errors選項(xiàng)問題也會導(dǎo)致
 
錯(cuò)誤五:
@ERROR: access denied to www fromunknown (192.168.1.123)
rsync: connection unexpectedly closed(0 bytes received so far) [receiver]
rsync error: error in rsync protocoldata stream (code 12) at io.c(359)
說明:此問題很明顯,是配置選項(xiàng)hostallow的問題,初學(xué)者喜歡一個(gè)允許段做成一個(gè)配置,然后模塊又是同一個(gè),致使導(dǎo)致
 
錯(cuò)誤六:
rsync error: received SIGINT,SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code19) at main.c(1182) [receiver=2.6.9]
說明:導(dǎo)致此問題多半是服務(wù)端服務(wù)沒有被正常啟動(dòng),到服務(wù)器上去查查服務(wù)是否有啟動(dòng),然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是殺死已經(jīng)啟動(dòng)了服務(wù),然后再次啟動(dòng)服務(wù)或者讓腳本加入系統(tǒng)啟動(dòng)服務(wù)級別然后shutdown -r now服務(wù)器

錯(cuò)誤七:
rsync: read error: Connection resetby peer (104)
rsync error: error in rsync protocoldata stream (code 12) at io.c(604) [sender=2.6.9]
說明:原數(shù)據(jù)目錄里沒有數(shù)據(jù)存在

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Linux中sersync數(shù)據(jù)實(shí)時(shí)同步的方法”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

標(biāo)題名稱:Linux中sersync數(shù)據(jù)實(shí)時(shí)同步的方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://muchs.cn/article24/ddcsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站商城網(wǎng)站、網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化虛擬主機(jī)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作