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

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

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

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

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

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

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

[root@jie1 ~]# vim /etc/httpd/conf/httpd.conf 
######################################## 
ServerName 172.16.22.1:80 
#DocumentRoot "/var/www/html"   #提供虛擬主機(jī),注釋默認(rèn)存放網(wǎng)頁文件的路徑 
<VirtualHost *:80> 
   ServerName www.jie.com 
   DocumentRoot  /web/htdocs
</VirtualHost> 
####################################### 
[root@jie1 ~]# mkdir -pv /web/htdocs   #創(chuàng)建存放網(wǎng)頁的目錄 
[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的配置文件是否有語法錯誤 
Syntax OK 
[root@jie1 htdocs]# service httpd start  #開啟web服務(wù) 
Starting httpd:                                            [  OK  ]

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

[root@jie1 htdocs]# id apache #安裝httpd軟件后,系統(tǒng)會創(chuàng)建apache用戶,查看apache的id號 
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號來確定是否能訪問共享的文件的,因為兩個服務(wù)器都安裝了httpd軟件,都會有apache用戶,所以apache用戶的id號能訪問共享的文件 
#/web/htdocs 共享的目錄 
#172.16.22.3 指定客戶端能共享此文件,多個客戶端用逗號隔開 
#rw,讀寫權(quán)限 
#sync,同步方式 
#root_squash,壓縮root用戶的權(quán)限 
#anonuid=48,指定此用戶的id能訪問共享文件 
#anongid=48指定此組的id能訪問共享文件 
###################################### 
[root@jie1 htdocs]# service nfs start  #開啟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的軟件

[root@jie3 /]# yum -y install httpd

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

[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)頁文件的路徑 
</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)在查看是沒有任何文件 
[root@jie3 website]#

  實現(xiàn)同步:

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

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

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

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

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

[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ù)器端,可以查看來著客戶端上傳的文件 
index.html  test.html  test.php  website.html

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

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

  rsync(remote sync)的特性:

  可以鏡像保存整個目錄樹和文件系統(tǒng)

  可以同步增量同步數(shù)據(jù),文件傳輸效率高,因而同步時間很短

  可以保持原有文件的權(quán)限、時間等屬性

  加密傳輸數(shù)據(jù),保證了數(shù)據(jù)的安全性

  支持匿名傳輸

  rsync也能實現(xiàn)同步,但是需要自己手動的去同步數(shù)據(jù),當(dāng)數(shù)據(jù)量非常的頻繁時,無疑是加大了運(yùn)維人員的工作,inotify是一種強(qiáng)大的、細(xì)粒度的、異步的文件系統(tǒng)事件監(jiān)控機(jī)制,inotify-tools工具的出現(xiàn),解決了這種工作,安裝inotify軟件的主機(jī)會監(jiān)聽服務(wù)器端的主機(jī)是否數(shù)據(jù)和本機(jī)不一樣,(因為在上傳數(shù)據(jù)時,運(yùn)維人員先上傳到安裝inotify主機(jī)上),不一樣就用rsync命令直接把數(shù)據(jù)傳輸過去。客戶端安裝rsync軟件是為了調(diào)用rsync的命令,安裝inotify軟件是監(jiān)聽和數(shù)據(jù)是否發(fā)生改變,服務(wù)器端安裝rsync軟件時為了提供rsync服務(wù)。

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

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

[root@jie1 ~]# yum -y install rsync xinetd httpd 
#rsync服務(wù)通常基于超級守護(hù)進(jìn)程xinetd管理的方式來實現(xiàn),因此需要事先安裝rysnc和xinetd

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

[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ù)器端,沒有任何的網(wǎng)頁文件 
[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,否則無法備份成功)

[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代表沒有限制 
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             #忽略一些無關(guān)的IO錯誤 
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è)置

[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)能識別軟件的庫文件 
[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)能識別的路徑下 
`/usr/include/inotify' -> `/usr/local/inotify/include/'
[root@jie3 inotify]#

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

[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ù)的腳本

[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
####################################################################

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

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

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

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

  sersync與inotify相比有以下優(yōu)點:

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

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

[root@jie1 ~]# yum -y install rsync xinetd httpd 
#rsync服務(wù)通常基于超級守護(hù)進(jìn)程xinetd管理的方式來實現(xiàn),因此需要事先安裝rysnc和xinetd

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

[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ù)器端,沒有任何的網(wǎng)頁文件 
[root@jie1 ~]# ls 
[root@jie1 ~]#

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

###====此配置文件的解釋,在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è)置

[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的配置文件

[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)聽的端口 
    <host hostip="172.16.22.3" port="8008"></host> 
  #debug模式是否開啟 
    <debug start="false"/> 
  #xfs文件系統(tǒng)是否開啟 
    <fileSystem xfs="false"/> 
  #同步時,是否支持正則表達(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"/> 
  #<!-- -->括起來表示注釋 
        <!--<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è)置超時時間 
       <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定時傳輸,默認(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> 
#######################################################################

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

###sersync客戶端的,開啟同步機(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ù)器端,查看可以來著sersync客戶端的同步文件 
[root@jie1 ~]# cd /web/htdocs/ 
[root@jie1 htdocs]# ls 
index.html  testdb.php  test.html  test.php 
[root@jie1 htdocs]#

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

  Unison是一款跨平臺的文件同步對象,不僅支撐本地對本地同步,也支撐經(jīng)由過程SSH、RSH和Socket等收集和談進(jìn)行同步。

  Unison支撐雙向同步操縱,你既可以從A同步到B,也可以從B同步到A,這些都不須要額外的設(shè)定。

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

[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 
##添加庫文件到系統(tǒng)識別的路徑 
[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 
##鏈接庫文件到系統(tǒng)識別的路徑 
[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會提示錯誤,此錯誤就是要你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上:

##把服務(wù)器A生成的公鑰傳到服務(wù)器B上#### 
[root@jie1 ~]# ssh-keygen -t rsa   #生成ssh的密鑰對 
[root@jie1 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.3:/root  #生成的密鑰在家目錄的ssh文件中,ssh文件為隱藏文件,通過scp復(fù)制到服務(wù)器B上 
[root@jie3 ~]# mv id_rsa.pub .ssh/authorized_keys  #在服務(wù)器B上把服務(wù)器A傳來的公鑰文件改名并存放到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上:

##把服務(wù)器B生成的公鑰傳到服務(wù)器A上#### 
[root@jie3 ~]# ssh-keygen -t rsa   #生成ssh的密鑰對 
[root@jie3 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.1:/root  #生成的密鑰在家目錄的ssh文件中,ssh文件為隱藏文件,通過scp復(fù)制到服務(wù)器B上 
[root@jie1 ~]# mv id_rsa.pub .ssh/authorized_keys  #在服務(wù)器A上把服務(wù)器B傳來的公鑰文件改名并存放到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)頁文件存放路徑為/web/htdocs,服務(wù)器B的網(wǎng)頁存放路徑為/website

##服務(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)行測試

##服務(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的測試 
[root@jie1 ~]# sh -x serA.sh  #先運(yùn)行unison同步腳本,查看過程 
[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的測試 
[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è)置開機(jī)自啟,放到rc.local文件中,且放在后臺運(yùn)行

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。