欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux下實(shí)現(xiàn)web數(shù)據(jù)同步的四種方式(性能比較)

 更新時(shí)間:2013年09月23日 15:30:00   作者:  
這篇文章主要介紹了linux下常用的四種web數(shù)據(jù)同步方法,并且說(shuō)明了每個(gè)方法的功能與優(yōu)勢(shì),需要的朋友可以參考下

實(shí)現(xiàn)web數(shù)據(jù)同步的四種方式

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

1、nfs實(shí)現(xiàn)web數(shù)據(jù)共享
2、rsync +inotify實(shí)現(xiàn)web數(shù)據(jù)同步
3、rsync+sersync更快更節(jié)約資源實(shí)現(xiàn)web數(shù)據(jù)同步
4、unison+inotify實(shí)現(xiàn)web數(shù)據(jù)雙向同步

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

一、nfs實(shí)現(xiàn)web數(shù)據(jù)共享

 nfs能實(shí)現(xiàn)數(shù)據(jù)同步是通過(guò)NAS(網(wǎng)絡(luò)附加存儲(chǔ)),在服務(wù)器上共享一個(gè)文件,且服務(wù)器需要設(shè)置文件系統(tǒng)的權(quán)限和配置文件設(shè)置的權(quán)限,權(quán)限兩者之間取交集,然后客戶端把共享的文件掛載到本地,客戶端對(duì)文件有讀寫權(quán)限,則實(shí)現(xiàn)數(shù)據(jù)的同步。

nfs+web:服務(wù)器端的配置:

1)、安裝相關(guān)軟件,httpd提供web服務(wù),nfs-utils提供nfs服務(wù)

[root@jie1 ~]# yum -y install httpd nfs-utils

2)、設(shè)置web的相關(guān)配置,使得web能夠提供web服務(wù)


復(fù)制代碼 代碼如下:

[root@jie1 ~]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.1:80
#DocumentRoot "/var/www/html"   #提供虛擬主機(jī),注釋默認(rèn)存放網(wǎng)頁(yè)文件的路徑
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /web/htdocs
</VirtualHost>
#######################################
[root@jie1 ~]# mkdir -pv /web/htdocs   #創(chuàng)建存放網(wǎng)頁(yè)的目錄
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# touch index.html test.html test.php
[root@jie1 htdocs]# ls
index.html  test.html  test.php
[root@jie1 htdocs]# echo "This is Jie1 Web+nfs Server" >index.html
[root@jie1 htdocs]# httpd -t         #檢查web的配置文件是否有語(yǔ)法錯(cuò)誤
Syntax OK
[root@jie1 htdocs]# service httpd start  #開(kāi)啟web服務(wù)
Starting httpd:                                            [  OK  ]

3)、設(shè)置nfs的相關(guān)配置,共享網(wǎng)頁(yè)文件

復(fù)制代碼 代碼如下:

[root@jie1 htdocs]# id apache #安裝httpd軟件后,系統(tǒng)會(huì)創(chuàng)建apache用戶,查看apache的id號(hào)
uid=48(apache) gid=48(apache) groups=48(apache)
[root@jie1 htdocs]# vim /etc/exports
######################################
/web/htdocs  172.16.22.3(rw,sync,root_squash,anonuid=48,anongid=48)
#nfs是以id號(hào)來(lái)確定是否能訪問(wèn)共享的文件的,因?yàn)閮蓚€(gè)服務(wù)器都安裝了httpd軟件,都會(huì)有apache用戶,所以apache用戶的id號(hào)能訪問(wèn)共享的文件
#/web/htdocs 共享的目錄
#172.16.22.3 指定客戶端能共享此文件,多個(gè)客戶端用逗號(hào)隔開(kāi)
#rw,讀寫權(quán)限
#sync,同步方式
#root_squash,壓縮root用戶的權(quán)限
#anonuid=48,指定此用戶的id能訪問(wèn)共享文件
#anongid=48指定此組的id能訪問(wèn)共享文件
######################################
[root@jie1 htdocs]# service nfs start  #開(kāi)啟nfs服務(wù)
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Stopping RPC idmapd:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
[root@jie1 htdocs]#

web:客戶端的配置

1)、安裝httpd的軟件

復(fù)制代碼 代碼如下:
[root@jie3 /]# yum -y install httpd

2)、設(shè)置web的相關(guān)配置,使得web能夠提供web服務(wù)

復(fù)制代碼 代碼如下:

[root@jie3 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.3:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /website   #存放網(wǎng)頁(yè)文件的路徑
</VirtualHost>
#######################################
[root@jie3 /]# mkdir /website
[root@jie3 /]# httpd -t
Syntax OK
[root@jie3 /]# service httpd start
Starting httpd:                                            [  OK  ]
[root@jie3 ~]# cd /website/
[root@jie3 website]# ls   #現(xiàn)在查看是沒(méi)有任何文件
[root@jie3 website]#

實(shí)現(xiàn)同步:

1)服務(wù)器端設(shè)置apache用戶對(duì)共享的文件有讀寫權(quán)限

復(fù)制代碼 代碼如下:
[root@jie1 htdocs]#setfacl -R -m u:apache:rwx /web/ #設(shè)置apache用戶對(duì)此中所有文件有讀寫可執(zhí)行權(quán)限

2)客戶端掛載服務(wù)器的共享文件,查看客戶端是否已經(jīng)同步服務(wù)器端的文件

復(fù)制代碼 代碼如下:

[root@jie3 website]#cd /root
[root@jie3 ~]# mount -t nfs 172.16.22.1:/web/htdocs /website/ #通過(guò)nfs掛載服務(wù)器端的文件
[root@jie3 /]#echo "172.16.22.1:/web/htdocs  /website       nfs    defaults,_netdev 0 0" >>/etc/fstab  #實(shí)現(xiàn)開(kāi)機(jī)掛載
[root@jie3 ~]# cd /website/
[root@jie3 website]# ls  #查看文件已經(jīng)同步過(guò)來(lái)
index.html  test.html  test.php
[root@jie3 website]#

3)客戶端在共享的文件中新增文件,查看服務(wù)器端是否同步文件

復(fù)制代碼 代碼如下:

[root@jie3 ~]# cd /website/
[root@jie3 website]# ls
index.html  test.html  test.php
[root@jie3 website]# touch website.html  #在客戶端創(chuàng)建此文件
[root@jie3 website]# ls
index.html  test.html  test.php  website.html

[root@jie1 htdocs]# ls  #服務(wù)器端,可以查看來(lái)著客戶端上傳的文件
index.html  test.html  test.php  website.html

所有的數(shù)據(jù)其實(shí)都保存到了nfs服務(wù)器,不論用戶訪問(wèn)哪臺(tái)Web服務(wù)器,都要來(lái)nfs服務(wù)器獲取數(shù)據(jù),這樣勢(shì)必照成nfs服務(wù)器的性能下降,而且客戶端對(duì)nfs服務(wù)器的依賴性較大,如果nfs服務(wù)器down掉之后,客戶端的web服務(wù)器就無(wú)法工作了。(動(dòng)態(tài)的那種數(shù)據(jù),而且數(shù)據(jù)量很大的數(shù)據(jù),就不要用nfs服務(wù)器來(lái)實(shí)現(xiàn)數(shù)據(jù)共享了,一般適應(yīng)于,靜態(tài)頁(yè)面和數(shù)據(jù)較小的文件)

二、rsync +inotify實(shí)現(xiàn)web數(shù)據(jù)同步

rsync(remote sync)的特性:

   可以鏡像保存整個(gè)目錄樹和文件系統(tǒng)
   可以同步增量同步數(shù)據(jù),文件傳輸效率高,因而同步時(shí)間很短
   可以保持原有文件的權(quán)限、時(shí)間等屬性
   加密傳輸數(shù)據(jù),保證了數(shù)據(jù)的安全性
   支持匿名傳輸
         rsync也能實(shí)現(xiàn)同步,但是需要自己手動(dòng)的去同步數(shù)據(jù),當(dāng)數(shù)據(jù)量非常的頻繁時(shí),無(wú)疑是加大了運(yùn)維人員的工作,inotify是一種強(qiáng)大的、細(xì)粒度的、異步的文件系統(tǒng)事件監(jiān)控機(jī)制,inotify-tools工具的出現(xiàn),解決了這種工作,安裝inotify軟件的主機(jī)會(huì)監(jiān)聽(tīng)服務(wù)器端的主機(jī)是否數(shù)據(jù)和本機(jī)不一樣,(因?yàn)樵谏蟼鲾?shù)據(jù)時(shí),運(yùn)維人員先上傳到安裝inotify主機(jī)上),不一樣就用rsync命令直接把數(shù)據(jù)傳輸過(guò)去。客戶端安裝rsync軟件是為了調(diào)用rsync的命令,安裝inotify軟件是監(jiān)聽(tīng)和數(shù)據(jù)是否發(fā)生改變,服務(wù)器端安裝rsync軟件時(shí)為了提供rsync服務(wù)。

rsync+web服務(wù)端的配置:

1)、安裝相關(guān)軟件

復(fù)制代碼 代碼如下:
[root@jie1 ~]# yum -y install rsync xinetd httpd
#rsync服務(wù)通?;诔?jí)守護(hù)進(jìn)程xinetd管理的方式來(lái)實(shí)現(xiàn),因此需要事先安裝rysnc和xinetd

2)、web的相關(guān)配置,使得web能夠提供服務(wù)


復(fù)制代碼 代碼如下:

[root@jie1 ~]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.1:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /web/htdocs
</VirtualHost>
#######################################
[root@jie1 ~]# mkdir -pv /web/htdocs
[root@jie1 ~]# cd /web/htdocs   #服務(wù)器端,沒(méi)有任何的網(wǎng)頁(yè)文件
[root@jie1 ~]# ls
[root@jie1 ~]#

3)、rsync服務(wù)的相關(guān)配置

*****建立rsync的配置文件和密碼文件************
       touch /etc/rsyncd.conf(rsync的配置文件)
       touch /etc/rsyncd.pwd(用戶的密碼文件) 
       chmod 600 /etc/rsyncd.pwd(權(quán)限要設(shè)置為600,否則無(wú)法備份成功)

復(fù)制代碼 代碼如下:

[root@jie1 ~]# vim /etc/rsyncd.conf
############vim /etc/rsyncd.conf########################################
uid = nobody                    #備份以什么身份進(jìn)行,用戶ID
gid = nobody                    #備份以什么身份進(jìn)行,組ID
use chroot = no                 #禁錮在源目錄
max connections = 3             #最大連接數(shù),0代表沒(méi)有限制
strict modes = yes              #是否檢查口令文件的權(quán)限
pid file = /var/run/rsyncd.pid  #運(yùn)行進(jìn)程的pid文件
log file = /var/log/rsyncd.log  #日志記錄文件
[htdocs]                        #指定認(rèn)證的備份模塊名
path = /web/htdocs              #需要備份的目錄的路徑
ignore errors = yes             #忽略一些無(wú)關(guān)的IO錯(cuò)誤
read only = no                  #設(shè)置為no,即可以傳至服務(wù)器的相應(yīng)目錄。
write only = no                 #設(shè)置為no,表示客戶端可以下載文件
hosts allow = 172.16.22.3       #可以連接rsync服務(wù)器的主機(jī)的IP地址
hosts deny = *                  #設(shè)置禁止連接rsync服務(wù)器的主機(jī)地址,*表示  拒絕所有除了hosts allow定義的
uid = root
gid = root
auth users = backuper            #連接模塊的用戶名
secrets file = /etc/rsyncd.pwd   #連接模塊用戶名的密碼文件存放路徑
#####################################################################
[root@jie1 ~]#vim  /etc/rsyncd.pwd  #用戶的密碼文件
#####################################################################
backuper:pwd123        #用戶名:密碼
#####################################################################
[root@jie1 ~]# chmod 600  /etc/rsyncd.pwd   #權(quán)限給600
[root@jie1 ~]# chkconfig rsync on
[root@jie1 ~]# chkconfig xinetd on
[root@jie1 ~]# service  xinetd start
Starting xinetd:                                           [  OK  ]
[root@jie1 ~]# netstat -pant | grep 873
tcp        0      0 :::873                      :::*                        LISTEN      19876/xinetd

rsync+inotify+web客戶端的配置:

1)、inotify-tools軟件的安裝及設(shè)置

復(fù)制代碼 代碼如下:

[root@jie3 ~]#wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz             #下載inotify-tools軟件
[root@jie3 ~]# ls
anaconda-ks.cfg            install.log    
inotify-tools-3.14.tar.gz  install.log.syslog
[root@jie3 ~]# tar xf inotify-tools-3.14.tar.gz          #解壓軟件
[root@jie3 ~]# cd inotify-tools-3.14
[root@jie3 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install                 #編譯安裝軟件
[root@jie3 ~]#cd /usr/local/inotify/
[root@jie3 inotify]# echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh             #設(shè)置能與系統(tǒng)關(guān)聯(lián)的path路徑
[root@jie3 inotify]# source /etc/profile.d/inotify.sh
[root@jie3 inotify]# echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf         #設(shè)置系統(tǒng)能識(shí)別軟件的庫(kù)文件
[root@jie3 inotify]# ldconfig -v | grep inotify
/usr/local/inotify/lib:
    libinotifytools.so.0 -> libinotifytools.so.0.4.1
[root@jie3 inotify]# ln -sv /usr/local/inotify/include/ /usr/include/inotify                      #鏈接頭文件到系統(tǒng)能識(shí)別的路徑下
`/usr/include/inotify' -> `/usr/local/inotify/include/'
[root@jie3 inotify]#

2)、web的相關(guān)配置,使得web能夠提供服務(wù)

復(fù)制代碼 代碼如下:

[root@jie3 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.3:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /website
</VirtualHost>
#######################################
[root@jie3 /]# mkdir /website
[root@jie3 /]# httpd -t
Syntax OK
[root@jie3 /]# service httpd start
Starting httpd:                                            [  OK  ]
[root@jie3 ~]# cd /website/
[root@jie3 website]#  ls
[root@jie3 website]#
[root@jie3 ~]#

3)、配置能連接rsync的密碼文件和傳輸數(shù)據(jù)的腳本

復(fù)制代碼 代碼如下:

[root@jie3 ~]# vim /etc/rsyncd.pwd
#############################################
pwd123  #密碼與rsync服務(wù)器的密碼相同
###############################################
[root@jie3 ~]# chmod 600 /etc/rsyncd.pwd
[root@jie3 ~]# vim  rsync.sh
#####################################################################
#!/bin/bash
host=172.16.22.1
src=/website
des=htdocs
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
  do
/usr/bin/rsync -vzrtopg  --progress --password-file=/etc/rsyncd.secrets $src backuper@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
####################################################################

驗(yàn)證實(shí)現(xiàn)同步:

復(fù)制代碼 代碼如下:

##1、先開(kāi)啟監(jiān)控的腳本(inotify主機(jī)上)
[root@jie3 ~]# bash -x rsync.sh &
#不放在后臺(tái)可以查看同步的詳細(xì)過(guò)程,生成環(huán)境中,建議把此腳本放到后臺(tái)執(zhí)行,此腳本會(huì)監(jiān)控客戶端數(shù)據(jù)是否方式變化,如果變化腳本就運(yùn)行,數(shù)據(jù)不變化,腳本就會(huì)等待著用戶的輸入
##2、在開(kāi)一個(gè)終端,在此目錄創(chuàng)建文件(inotify主機(jī)上)
[root@jie3 ~]# cd /website/
[root@jie3 website]# touch index.html test.php testdb.php  inotify.php
[root@jie3 website]# ls
index.html  testdb.php  test.php inotify.php
[root@jie3 website]#
##3、看服務(wù)器端,數(shù)據(jù)是否已經(jīng)同步過(guò)去
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# ls
index.html  testdb.php  test.php inotify.php  #數(shù)據(jù)已經(jīng)被同步過(guò)來(lái)
[root@jie1 htdocs]#

rsync +inotify這種能實(shí)現(xiàn)數(shù)據(jù)的同步,但是當(dāng)網(wǎng)絡(luò)很繁忙,且文件變化比較頻繁時(shí),而且需要同步的rsync服務(wù)器端比較多時(shí),rsync+inotify肯定是滿足不了需求的,于是rsync+sersync這種更快更節(jié)約資源實(shí)現(xiàn)web數(shù)據(jù)同步可以彌補(bǔ)rsync+inotify帶來(lái)的不足,rsync+inotify還有一個(gè)重大的缺點(diǎn)就是數(shù)據(jù)傳輸只是單向的,當(dāng)運(yùn)維人員由于“粗心”把數(shù)據(jù)直接傳輸rsync服務(wù)器端時(shí),inotify主機(jī)是得不到rsync服務(wù)器端的數(shù)據(jù),于是unison+inotify實(shí)現(xiàn)web數(shù)據(jù)雙向同步,解決了rsync+inotify的這一缺點(diǎn)。

三、rsync+sersync更快更節(jié)約資源實(shí)現(xiàn)web數(shù)據(jù)同步



 

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

rsync+web服務(wù)器端的配置:

1)、安裝相關(guān)軟件

復(fù)制代碼 代碼如下:

[root@jie1 ~]# yum -y install rsync xinetd httpd
#rsync服務(wù)通?;诔?jí)守護(hù)進(jìn)程xinetd管理的方式來(lái)實(shí)現(xiàn),因此需要事先安裝rysnc和xinetd

2)、web的相關(guān)配置,使得web能夠提供服務(wù)

復(fù)制代碼 代碼如下:

[root@jie1 ~]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.1:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot  /web/htdocs
</VirtualHost>
#######################################
[root@jie1 ~]# mkdir -pv /web/htdocs
[root@jie1 ~]# cd /web/htdocs   #服務(wù)器端,沒(méi)有任何的網(wǎng)頁(yè)文件
[root@jie1 ~]# ls
[root@jie1 ~]#

3)、rsync服務(wù)的相關(guān)配置

復(fù)制代碼 代碼如下:

###====此配置文件的解釋,在rsync+inotify中已經(jīng)解釋了=====####
[root@jie1 ~]# vim /etc/rsyncd.conf
############vim /etc/rsyncd.conf###############
uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file= /var/run/rsyncd.pid
log file= /var/log/rsyncd.log
[htdocs]
path = /web/htdocs
ignore errors = yes
readonly = no
write only = no
hosts allow = 172.16.22.3
hosts deny = *
list = false
uid = root
gid = root
auth users= backuper
secrets file= /etc/rsyncd.pwd
##############################################
[root@jie1 ~]#vim /etc/rsyncd.pwd
backuper:pwd123
[root@jie1 ~]# chmod 600 /etc/rsyncd.pwd
[root@jie1 ~]# chkconfig rsync on
[root@jie1 ~]# chkconfig xinetd on
[root@jie1 ~]# service  xinetd start
Starting xinetd:                                           [  OK  ]
[root@jie1 ~]# netstat -pant | grep 873
tcp        0      0 :::873                      :::*                        LISTEN      19876/xinetd

sersync+web客戶端的配置:

1)、先下載安裝sersync軟件,做初始設(shè)置

復(fù)制代碼 代碼如下:

[root@jie3 ~]#wget --no-check-certificate https://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
[root@jie3 ~]# ls
anaconda-ks.cfg  install.log.syslog
install.log      sersync2.5_64bit_binary_stable_final.tar.gz
 mkdir /usr/local/sersync
[root@jie3 ~]#mkdir -pv /usr/local/sersync/{conf,bin,log}
mkdir: created directory `/usr/local/sersync'
mkdir: created directory `/usr/local/sersync/conf'
mkdir: created directory `/usr/local/sersync/bin'
mkdir: created directory `/usr/local/sersync/log'
[root@jie3 ~]# tar xf sersync2.5_64bit_binary_stable_final.tar.gz
[root@jie3 ~]# cd GNU-Linux-x86/
[root@jie3 GNU-Linux-x86]# ls
confxml.xml  sersync2
[root@jie3 GNU-Linux-x86]# mv confxml.xml /usr/local/sersync/conf/
[root@jie3 GNU-Linux-x86]# mv sersync2  /usr/local/sersync/bin/
[root@jie3 GNU-Linux-x86]# cd /usr/local/sersync/
[root@jie3 sersync]# echo "PATH=/usr/local/sersync/bin:$PATH" >>/etc/profile.d/sersync.sh
[root@jie3 sersync]# source /etc/profile.d/sersync.sh
[root@jie3 sersync]# echo "pwd123" >/usr/local/sersync/sersync.pwd
[root@jie3 sersync]# chmod 600 /usr/local/sersync/sersync.pwd

2)、修改sersync的配置文件

復(fù)制代碼 代碼如下:

[root@jie3 sersync]# vim /usr/local/sersync/conf/confxml.xml
#########################################################################<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
  #設(shè)置本地的ip地址和監(jiān)聽(tīng)的端口
    <host hostip="172.16.22.3" port="8008"></host>
  #debug模式是否開(kāi)啟
    <debug start="false"/>
  #xfs文件系統(tǒng)是否開(kāi)啟
    <fileSystem xfs="false"/>
  #同步時(shí),是否支持正則表達(dá)式,默認(rèn)關(guān)閉
    <filter start="false">
    <exclude expression="(.*)\.svn"></exclude>
    <exclude expression="(.*)\.gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></exclude>
    </filter>
  # 設(shè)置要監(jiān)控的事件
    <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>
  #同步的設(shè)置
    <sersync>
  #同步的路徑,本地的目錄
    <localpath watch="/website">
  #rsync服務(wù)器的ip地址和rsync配置文件里面定義的模塊
        <remote ip="172.16.22.1" name="htdocs"/>
  #<!-- -->括起來(lái)表示注釋
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
  #rsync指令參數(shù)
        <commonParams params="-artuz"/>
  #rsync同步認(rèn)證設(shè)置的內(nèi)容,user指定用戶名,password指定存放密碼的文件路徑
        <auth start="true" users="backuper" passwordfile="/usr/local/sersync/sersync.pwd"/>
  #設(shè)置rsync遠(yuǎn)程服務(wù)端口
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
  #設(shè)置超時(shí)時(shí)間
       <timeout start="true" time="100"/><!-- timeout=100 -->
  #設(shè)置ssh加密傳輸模式,默認(rèn)關(guān)閉
        <ssh start="false"/>
    </rsync>
  #設(shè)置sersync傳輸失敗日志腳本路徑
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
  #設(shè)置rsync+crontab定時(shí)傳輸,默認(rèn)關(guān)閉
    <crontab start="false" schedule="600"><!--600mins-->
        <crontabfilter start="false">
        <exclude expression="*.php"></exclude>
        <exclude expression=\'#\'" /*"></exclude>
        </crontabfilter>
    </crontab>
  #設(shè)置sersync傳輸后調(diào)用name指定的插件腳本,默認(rèn)關(guān)閉
    <plugin start="false" name="command"/>
    </sersync>
  #插件腳本范例
    <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>
</head>
#######################################################################

驗(yàn)證實(shí)現(xiàn)同步:


復(fù)制代碼 代碼如下:

###sersync客戶端的,開(kāi)啟同步機(jī)制,進(jìn)行監(jiān)控,然后創(chuàng)建文件
[root@jie3 website]# sersync2 -r -d &
[root@jie3 ~]# cd /website/
[root@jie3 website]# touch index.html  testdb.php  test.html  test.php
###rsync服務(wù)器端,查看可以來(lái)著sersync客戶端的同步文件
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# ls
index.html  testdb.php  test.html  test.php
[root@jie1 htdocs]#

四、unison+inotify實(shí)現(xiàn)web數(shù)據(jù)雙向同步

Unison是一款跨平臺(tái)的文件同步對(duì)象,不僅支撐本地對(duì)本地同步,也支撐經(jīng)由過(guò)程SSH、RSH和Socket等收集和談進(jìn)行同步。
Unison支撐雙向同步操縱,你既可以從A同步到B,也可以從B同步到A,這些都不須要額外的設(shè)定。

1)、兩個(gè)服務(wù)器都編譯安裝這三個(gè)源碼包:(在此我只寫一臺(tái)服務(wù)器的編譯安裝過(guò)程)

復(fù)制代碼 代碼如下:

[root@jie1 ~]#wget ftp://distro.ibiblio.org/slitaz/sources/packages-2.0/o/ocaml-3.10.2.tar.gz
[root@jie1~]#wget  http://freebsd.ntu.edu.tw/FreeBSD/ports/distfiles/unison-2.32.52/unison-2.32.52.tar.gz
[root@jie1~]#wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@jie1 ~]# ls
anaconda-ks.cfg            install.log         ocaml-3.10.2.tar.gz
inotify-tools-3.14.tar.gz  install.log.syslog  unison-2.32.52.tar.gz
[root@jie1 ~]# tar xf inotify-tools-3.14.tar.gz
[root@jie1 ~]# tar xf ocaml-3.10.2.tar.gz
[root@jie1 ~]# tar xf unison-2.32.52.tar.gz
##編譯安裝inotify
[root@jie1 ~]# cd inotify-tools-3.14
[root@jie1 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@jie1 inotify-tools-3.14]# cd /usr/local/inotify/
##修改PATH環(huán)境變量
[root@jie1 inotify]# echo "PATH=/usr/local/inotify/bin:$PATH" >/etc/profile.d/inotify.sh
[root@jie1 inotify]# source /etc/profile.d/inotify.sh
##添加庫(kù)文件到系統(tǒng)識(shí)別的路徑
[root@jie1 inotify]# echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
[root@jie1 inotify]# ldconfig -v | grep inotify
/usr/local/inotify/lib:
    libinotifytools.so.0 -> libinotifytools.so.0.4.1
##鏈接庫(kù)文件到系統(tǒng)識(shí)別的路徑
[root@jie1 inotify]# ln -sv /usr/local/inotify/include/ /usr/include/inotify
`/usr/include/inotify' -> `/usr/local/inotify/include/'
##編譯安裝ocaml,unison依賴于ocaml
[root@jie1 inotify]#cd /root/ocaml-3.10.2
[root@jie1 ocaml-3.10.2]#./configure
[root@jie1 ocaml-3.10.2]#make world opt
[root@jie1 ocaml-3.10.2]#make install
##編譯安裝unison
[root@jie1 ocaml-3.10.2]# cd /root/unison-2.32.52
##安裝依賴性包
[root@jie1 unison-2.32.52]#yum -y install ctags-etags
[root@jie1 unison-2.32.52]# make UISTYLE=text
##make install會(huì)提示錯(cuò)誤,此錯(cuò)誤就是要你cp unison /usr/local/bin,復(fù)制即可
[root@jie1 unison-2.32.52]# make install
[root@jie1 unison-2.32.52]# cp unison /usr/local/bin

2)、服務(wù)器A生成的公鑰傳到服務(wù)器B上:

復(fù)制代碼 代碼如下:

##把服務(wù)器A生成的公鑰傳到服務(wù)器B上####
[root@jie1 ~]# ssh-keygen -t rsa   #生成ssh的密鑰對(duì)
[root@jie1 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.3:/root  #生成的密鑰在家目錄的ssh文件中,ssh文件為隱藏文件,通過(guò)scp復(fù)制到服務(wù)器B上
[root@jie3 ~]# mv id_rsa.pub .ssh/authorized_keys  #在服務(wù)器B上把服務(wù)器A傳來(lái)的公鑰文件改名并存放到ssh目錄下
[root@jie3 ~]# chmod 600 .ssh/authorized_keys  #給公鑰文件改權(quán)限為600
[root@jie1 ~]# service sshd restart  #重啟sshd服務(wù)
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@jie1 ~]#

3)、服務(wù)器B生成的公鑰傳到服務(wù)器A上:

復(fù)制代碼 代碼如下:

##把服務(wù)器B生成的公鑰傳到服務(wù)器A上####
[root@jie3 ~]# ssh-keygen -t rsa   #生成ssh的密鑰對(duì)
[root@jie3 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.1:/root  #生成的密鑰在家目錄的ssh文件中,ssh文件為隱藏文件,通過(guò)scp復(fù)制到服務(wù)器B上
[root@jie1 ~]# mv id_rsa.pub .ssh/authorized_keys  #在服務(wù)器A上把服務(wù)器B傳來(lái)的公鑰文件改名并存放到ssh目錄下
[root@jie1 ~]# chmod 600 .ssh/authorized_keys  #給公鑰文件改權(quán)限為600
[root@jie3 ~]# service sshd restart  #重啟sshd服務(wù)
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@jie3 ~]#

4)、分別搭建web服務(wù),服務(wù)器A的網(wǎng)頁(yè)文件存放路徑為/web/htdocs,服務(wù)器B的網(wǎng)頁(yè)存放路徑為/website

復(fù)制代碼 代碼如下:

##服務(wù)器A搭建web的配置
[root@jie1 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.1:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /web/htdocs
</VirtualHost>
#######################################
[root@jie1 ~]# mkdir -pv /web/htdocs
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# ls
[root@jie1 htdocs]#
##服務(wù)器B搭建web的配置
[root@jie3 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.3:80
#DocumentRoot "/var/www/html"
<VirtualHost *:80>
   ServerName www.jie.com
   DocumentRoot  /website
</VirtualHost>
#######################################
[root@jie3 /]# mkdir /website
[root@jie3 /]# httpd -t
Syntax OK
[root@jie3 /]# service httpd start
Starting httpd:                                            [  OK  ]
[root@jie3 ~]# cd /website/
[root@jie3 website]# ls
[root@jie3 website]#

5)、編unison同步的腳本進(jìn)行測(cè)試

復(fù)制代碼 代碼如下:

##服務(wù)器A的腳本
[root@jie1 ~]# vim serA.sh
######################################################################
#/bin/bash
ipB="172.16.22.3"
srcA="/web/htdocs"
dstB="/website"
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcA | while read line; do
/usr/local/bin/unison -batch $srcA ssh://$ipB/$dstB
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
#####################################################################
##服務(wù)器B的腳本
[root@jie3 ~]# vim serB.sh
#####################################################################
#/bin/bash
ipA="172.16.22.1"
srcB="/website"
dstA="/web/htdocs"
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcB | while read line; do
/usr/local/bin/unison -batch $srcB ssh://$ipA/$dstA
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
#####################################################################
##服務(wù)器A的測(cè)試
[root@jie1 ~]# sh -x serA.sh  #先運(yùn)行unison同步腳本,查看過(guò)程
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# touch serA.txt SerA.html SerA.php  #然后創(chuàng)建文件
[root@jie1 htdocs]# ls
SerA.html  SerA.php  serA.txt  SerB.html  SerB.php  SerB.txt
##服務(wù)器B的測(cè)試
[root@jie3 ~]# sh -x serB.sh
[root@jie3 ~]# cd /website/
[root@jie3 website]# touch SerB.txt SerB.html SerB.php
[root@jie3 website]# ls
SerA.html  SerA.php  serA.txt  SerB.html  SerB.php  SerB.txt
###=====可以把腳本設(shè)置開(kāi)機(jī)自啟,放到rc.local文件中,且放在后臺(tái)運(yùn)行

本文出自 “技術(shù)之路---桀” 博客,請(qǐng)務(wù)必保留此出處http://litaotao.blog.51cto.com/6224470/1286871

相關(guān)文章

最新評(píng)論