国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
sersync基于rsync+inotify實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步 | HelloDog

前言

提到數(shù)據(jù)同步就必然會(huì)談到rsync,一般簡(jiǎn)單的服務(wù)器數(shù)據(jù)傳輸會(huì)使用ftp/sftp等方式,但是這樣的方式效率不高,不支持差異化增量同步也不支持實(shí)時(shí)傳輸。針對(duì)數(shù)據(jù)實(shí)時(shí)同步需求大多數(shù)人會(huì)選擇rsync+inotify-tools的解決方案,但是這樣的方案也存在一些缺陷(文章中會(huì)具體指出),sersync是國(guó)人基于前兩者開發(fā)的工具,不僅保留了優(yōu)點(diǎn)同時(shí)還強(qiáng)化了實(shí)時(shí)監(jiān)控,文件過(guò)濾,簡(jiǎn)化配置等功能,幫助用戶提高運(yùn)行效率,節(jié)省時(shí)間和網(wǎng)絡(luò)資源。

可靠高效的數(shù)據(jù)實(shí)時(shí)同步方式


更新歷史

2015年08月14日 - 更新GitHub源碼安裝包
2015年08月13日 - 初稿

閱讀原文 - http://wsgzao.github.io/post/sersync/

擴(kuò)展閱讀

基于rsync+sersync的服務(wù)器文件同步實(shí)戰(zhàn) - http://www.markdream.com/technologies/server/syncfile-by-rsync.shtml
通過(guò) rsync sersync 實(shí)現(xiàn)高效的數(shù)據(jù)實(shí)時(shí)同步架構(gòu) - https://www.cnhzz.com/rsync_sersync/
Rsync+sersync實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步 - http://www.cnblogs.com/wjoyxt/p/4581410.html
rsync - https://rsync.samba.org/
inotify-tools - https://github.com/rvoicilas/inotify-tools
sersync - http://code.google.com/p/sersync/


原理

Synchronize files and folders between servers -using inotiy and rsync with c++ 服務(wù)器實(shí)時(shí)同步文件,服務(wù)器鏡像解決方案

sersync主要用于服務(wù)器同步,web鏡像等功能?;赽oost1.43.0,inotify api,rsync command.開發(fā)。目前使用的比較多的同步解決方案是inotify-tools+rsync ,另外一個(gè)是google開源項(xiàng)目Openduckbill(依賴于inotify- tools),這兩個(gè)都是基于腳本語(yǔ)言編寫的。相比較上面兩個(gè)項(xiàng)目,本項(xiàng)目?jī)?yōu)點(diǎn)是:

  1. sersync是使用c++編寫,而且對(duì)linux系統(tǒng)文件系統(tǒng)產(chǎn)生的臨時(shí)文件和重復(fù)的文件操作進(jìn)行過(guò)濾(詳細(xì)見附錄,這個(gè)過(guò)濾腳本程序沒(méi)有實(shí)現(xiàn)),所以在結(jié)合rsync同步的時(shí)候,節(jié)省了運(yùn)行時(shí)耗和網(wǎng)絡(luò)資源。因此更快。
  2. 相比較上面兩個(gè)項(xiàng)目,sersync配置起來(lái)很簡(jiǎn)單,其中bin目錄下已經(jīng)有基本上靜態(tài)編譯的2進(jìn)制文件,配合bin目錄下的xml配置文件直接使用即可。
  3. 另外本項(xiàng)目相比較其他腳本開源項(xiàng)目,使用多線程進(jìn)行同步,尤其在同步較大文件時(shí),能夠保證多個(gè)服務(wù)器實(shí)時(shí)保持同步狀態(tài)。
  4. 本項(xiàng)目有出錯(cuò)處理機(jī)制,通過(guò)失敗隊(duì)列對(duì)出錯(cuò)的文件重新同步,如果仍舊失敗,則按設(shè)定時(shí)長(zhǎng)對(duì)同步失敗的文件重新同步。
  5. 本項(xiàng)目自帶crontab功能,只需在xml配置文件中開啟,即可按您的要求,隔一段時(shí)間整體同步一次。無(wú)需再額外配置crontab功能。
  6. 本項(xiàng)目socket與http插件擴(kuò)展,滿足您二次開發(fā)的需要。

針對(duì)上圖的設(shè)計(jì)架構(gòu),這里做幾點(diǎn)說(shuō)明,來(lái)幫助大家閱讀和理解該圖

1 ) 線程組線程是等待線程隊(duì)列的守護(hù)線程,當(dāng)事件隊(duì)列中有事件產(chǎn)生的時(shí)候,線程組守護(hù)線程就會(huì)逐個(gè)喚醒同步線程。當(dāng)隊(duì)列中 Inotify 事件較多的時(shí)候,同步線程就會(huì)被全部喚醒一起工作。這樣設(shè)計(jì)的目的是為了能夠同時(shí)處理多個(gè) Inotify 事件,從而提升服務(wù)器的并發(fā)同步能力。同步線程的最佳數(shù)量=核數(shù) x 2 + 2。
2 ) 那么之所以稱之為線程組線程,是因?yàn)槊總€(gè)線程在工作的時(shí)候,會(huì)根據(jù)服務(wù)器上新寫入文件的數(shù)量去建立子線程,子線程可以保證所有的文件與各個(gè)服務(wù)器同時(shí)同步。當(dāng)要同步的文件較大的時(shí)候,這樣的設(shè)計(jì)可以保證每個(gè)遠(yuǎn)程服務(wù)器都可以同時(shí)獲得需要同步的文件。
3 ) 服務(wù)線程的作用有三個(gè):

  • 處理同步失敗的文件,將這些文件再次同步,對(duì)于再次同步失敗的文件會(huì)生成 rsync_fail_log.sh 腳本,記錄失敗的事件。
  • 每隔10個(gè)小時(shí)執(zhí)行 rsync_fail_log.sh 腳本一次,同時(shí)清空腳本。
  • crontab功能,可以每隔一定時(shí)間,將所有路徑整體同步一次。

4 ) 過(guò)濾隊(duì)列的建立是為了過(guò)濾短時(shí)間內(nèi)產(chǎn)生的重復(fù)的inotify信息,例如在刪除文件夾的時(shí)候,inotify就會(huì)同時(shí)產(chǎn)生刪除文件夾里的文件與刪除文件夾的事件,通過(guò)過(guò)濾隊(duì)列,當(dāng)刪除文件夾事件產(chǎn)生的時(shí)候,會(huì)將之前加入隊(duì)列的刪除文件的事件全部過(guò)濾掉,這樣只產(chǎn)生一條刪除文件夾的事件,從而減輕了同步的負(fù)擔(dān)。同時(shí)對(duì)于修改文件的操作的時(shí)候,會(huì)產(chǎn)生臨時(shí)文件的重復(fù)操作。

角色

注意主從配置的區(qū)別,記得調(diào)整SELinux和防火墻

iptables配置實(shí)踐 - http://wsgzao.github.io/post/iptables/
LTMP手動(dòng)編譯安裝以及全自動(dòng)化部署實(shí)踐 - http://wsgzao.github.io/post/ltmp/

  1. 服務(wù)器A(主服務(wù)器)
  2. 服務(wù)器B(從服務(wù)器/備份服務(wù)器)
  3. rsync默認(rèn)TCP端口為873

服務(wù)器B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#在服務(wù)器B上安裝rsync
cd /app/local
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
tar zxf rsync-3.1.1.tar.gz
cd rsync-3.1.1
./configure
make && make install


#設(shè)置rsync的配置文件
vi /etc/rsyncd.conf

#服務(wù)器B上的rsyncd.conf文件內(nèi)容
uid=root
gid=root
#最大連接數(shù)
max connections=36000
#默認(rèn)為true,修改為no,增加對(duì)目錄文件軟連接的備份
use chroot=no
#定義日志存放位置
log file=/var/log/rsyncd.log
#忽略無(wú)關(guān)錯(cuò)誤
ignore errors = yes
#設(shè)置rsync服務(wù)端文件為讀寫權(quán)限
read only = no
#認(rèn)證的用戶名與系統(tǒng)帳戶無(wú)關(guān)在認(rèn)證文件做配置,如果沒(méi)有這行則表明是匿名
auth users = rsync
#密碼認(rèn)證文件,格式(虛擬用戶名:密碼)
secrets file = /etc/rsync.pass
#這里是認(rèn)證的模塊名,在client端需要指定,可以設(shè)置多個(gè)模塊和路徑
[rsync]
#自定義注釋
comment = rsync
#同步到B服務(wù)器的文件存放的路徑
path=/app/data/site/
[img]
comment = img
path=/app/data/site/img

#創(chuàng)建rsync認(rèn)證文件 可以設(shè)置多個(gè),每行一個(gè)用戶名:密碼,注意中間以“:”分割
echo "rsync:rsync" > /etc/rsync.pass

#設(shè)置文件所有者讀取、寫入權(quán)限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass

#啟動(dòng)服務(wù)器B上的rsync服務(wù)
#rsync --daemon -v
rsync --daemon

#監(jiān)聽端口873
netstat -an | grep 873
lsof -i tcp:873

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 31445 root 4u IPv4 443872 0t0 TCP *:rsync (LISTEN)
rsync 31445 root 5u IPv6 443873 0t0 TCP *:rsync (LISTEN)


#設(shè)置rsync為服務(wù)啟動(dòng)項(xiàng)(可選)
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local

#要 Kill rsync 進(jìn)程,不要用 kill -HUP {PID} 的方式重啟進(jìn)程,以下3種方式任選
#ps -ef|grep rsync|grep -v grep|awk '{print $2}'|xargs kill -9
#cat /var/run/rsyncd.pid | xargs kill -9
pkill rsync
#再次啟動(dòng)
/usr/local/bin/rsync --daemon

服務(wù)器A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#安裝rsync
cd /app/local
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
tar zxf rsync-3.1.1.tar.gz
cd rsync-3.1.1
./configure
make && make install

#安裝inotify-tools
cd /app/local
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/app/local/inotify
make && make install

#安裝sersync
cd /app/local
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv /app/local/GNU-Linux-x86/ /app/local/sersync
cd /app/local/sersync
#配置下密碼文件,因?yàn)檫@個(gè)密碼是要訪問(wèn)服務(wù)器B需要的密碼和上面服務(wù)器B的密碼必須一致
echo "rsync" > /app/local/sersync/user.pass
#修改權(quán)限
chmod 600 /app/local/sersync/user.pass
#修改confxml.conf
vi /app/local/sersync/confxml.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="true"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.php"></exclude>
<exclude expression="^data/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>

<sersync>
<localpath watch="/home/"> <!-- 這里填寫服務(wù)器A要同步的文件夾路徑-->
<remote ip="8.8.8.8" name="rsync"/> <!-- 這里填寫服務(wù)器B的IP地址和模塊名-->
<!--<remote ip="192.168.28.39" name="tongbu"/>-->
<!--<remote ip="192.168.28.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync" passwordfile="/app/local/sersync/user.pass"/> <!-- rsync+密碼文件 這里填寫服務(wù)器B的認(rèn)證信息-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--><!-- 修改失敗日志記錄(可選)-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>

<!-- 下面這些有關(guān)于插件你可以忽略了 -->
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>

<plugin name="socket">
<localpath watch="/home/demo">
<deshost ip="210.36.158.xxx" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cdn.markdream.com/site/">
<cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://cdn.markdream.com/cms"/>
<regexurl regex="false" match="cdn.markdream.com/site([/a-zA-Z0-9]*).cdn.markdream.com/images"/>
</localpath>
</plugin>
</head>
1
2
3
4
5
6
7
8
#運(yùn)行sersync
nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/confxml.xml >/app/local/sersync/rsync.log 2>&1 &
nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/img.xml >/app/local/sersync/img.log 2>&1 &

-d:啟用守護(hù)進(jìn)程模式
-r:在監(jiān)控前,將監(jiān)控目錄與遠(yuǎn)程主機(jī)用rsync命令推送一遍
-n: 指定開啟守護(hù)線程的數(shù)量,默認(rèn)為10個(gè)
-o:指定配置文件,默認(rèn)使用confxml.xml文件

GitHub源碼倉(cāng)庫(kù)

1
2
3
4
file://E:\sersync   (0 folders, 3 files, 1.88 MB, 1.88 MB in total.)
inotify-tools-3.14.tar.gz 350.36 KB
rsync-3.1.1.tar.gz 869.26 KB
sersync2.5.4_64bit_binary_stable_final.tar.gz 710.24 KB

sersync - https://github.com/wsgzao/sersync

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Linux運(yùn)維Sersync講解 Linux周末學(xué)習(xí)
Sersync服務(wù)器同步程序 項(xiàng)目簡(jiǎn)介與設(shè)計(jì)框架
linux下sersync同步軟件的安裝及配置
Sersync+Rsync實(shí)現(xiàn)服務(wù)器數(shù)據(jù)實(shí)時(shí)同步及xml高級(jí)配置!
Sersync實(shí)時(shí)同步
linux下實(shí)現(xiàn)web數(shù)據(jù)同步的四種方式(性能比較)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服