Linux下實現Web資料同步的四種方式

技術之路---桀發表於2015-03-17

實現web資料同步的四種方式

1、nfs實現web資料共享
2、rsync +inotify實現web資料同步
3、rsync+sersync更快更節約資源實現web資料同步
4、unison+inotify實現web資料雙向同步

一、nfs實現web資料共享

nfs能實現資料同步是通過NAS(網路附加儲存),在伺服器上共享一個檔案,且伺服器需要設定檔案系統的許可權和配置檔案設定的許可權,許可權兩者之間取交集,然後客戶端把共享的檔案掛載到本地,客戶端對檔案有讀寫許可權,則實現資料的同步。

nfs+web:伺服器端的配置:

1)、安裝相關軟體,httpd提供web服務,nfs-utils提供nfs服務

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

2)、設定web的相關配置,使得web能夠提供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]# 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服務 
Starting httpd:                                            [  OK  ]

3)、設定nfs的相關配置,共享網頁檔案

[root@jie1 htdocs]# id apache #安裝httpd軟體後,系統會建立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號來確定是否能訪問共享的檔案的,因為兩個伺服器都安裝了httpd軟體,都會有apache使用者,所以apache使用者的id號能訪問共享的檔案 
#/web/htdocs 共享的目錄 
#172.16.22.3 指定客戶端能共享此檔案,多個客戶端用逗號隔開 
#rw,讀寫許可權 
#sync,同步方式 
#root_squash,壓縮root使用者的許可權 
#anonuid=48,指定此使用者的id能訪問共享檔案 
#anongid=48指定此組的id能訪問共享檔案 
###################################### 
[root@jie1 htdocs]# service nfs start  #開啟nfs服務 
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)、設定web的相關配置,使得web能夠提供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]#

實現同步:

1)伺服器端設定apache使用者對共享的檔案有讀寫許可權

[root@jie1 htdocs]#setfacl -R -m u:apache:rwx /web/ #設定apache使用者對此中所有檔案有讀寫可執行許可權

2)客戶端掛載伺服器的共享檔案,檢視客戶端是否已經同步伺服器端的檔案

[root@jie3 website]#cd /root 
[root@jie3 ~]# mount -t nfs 172.16.22.1:/web/htdocs /website/ #通過nfs掛載伺服器端的檔案 
[root@jie3 /]#echo "172.16.22.1:/web/htdocs  /website       nfs    defaults,_netdev 0 0" >>/etc/fstab  #實現開機掛載 
[root@jie3 ~]# cd /website/ 
[root@jie3 website]# ls  #檢視檔案已經同步過來 
index.html  test.html  test.php 
[root@jie3 website]#

3)客戶端在共享的檔案中新增檔案,檢視伺服器端是否同步檔案

[root@jie3 ~]# cd /website/ 
[root@jie3 website]# ls 
index.html  test.html  test.php 
[root@jie3 website]# touch website.html  #在客戶端建立此檔案 
[root@jie3 website]# ls 
index.html  test.html  test.php  website.html
[root@jie1 htdocs]# ls  #伺服器端,可以檢視來著客戶端上傳的檔案 
index.html  test.html  test.php  website.html

所有的資料其實都儲存到了nfs伺服器,不論使用者訪問哪臺Web伺服器,都要來nfs伺服器獲取資料,這樣勢必照成nfs伺服器的效能下降,而且客戶端對nfs伺服器的依賴性較大,如果nfs伺服器down掉之後,客戶端的web伺服器就無法工作了。(動態的那種資料,而且資料量很大的資料,就不要用nfs伺服器來實現資料共享了,一般適應於,靜態頁面和資料較小的檔案)

二、rsync +inotify實現web資料同步

rsync(remote sync)的特性:

可以映象儲存整個目錄樹和檔案系統
可以同步增量同步資料,檔案傳輸效率高,因而同步時間很短
可以保持原有檔案的許可權、時間等屬性
加密傳輸資料,保證了資料的安全性
支援匿名傳輸

rsync也能實現同步,但是需要自己手動的去同步資料,當資料量非常的頻繁時,無疑是加大了運維人員的工作,inotify是一種強大的、細粒度的、非同步的檔案系統事件監控機制,inotify-tools工具的出現,解決了這種工作,安裝inotify軟體的主機會監聽伺服器端的主機是否資料和本機不一樣,(因為在上傳資料時,運維人員先上傳到安裝inotify主機上),不一樣就用rsync命令直接把資料傳輸過去。客戶端安裝rsync軟體是為了呼叫rsync的命令,安裝inotify軟體是監聽和資料是否發生改變,伺服器端安裝rsync軟體時為了提供rsync服務。

rsync+web服務端的配置:

1)、安裝相關軟體

[root@jie1 ~]# yum -y install rsync xinetd httpd 
#rsync服務通常基於超級守護程式xinetd管理的方式來實現,因此需要事先安裝rysnc和xinetd

2)、web的相關配置,使得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 ~]# ls 
[root@jie1 ~]#

3)、rsync服務的相關配置

*****建立rsync的配置檔案和密碼檔案************
touch /etc/rsyncd.conf(rsync的配置檔案)
touch /etc/rsyncd.pwd(使用者的密碼檔案)
chmod 600 /etc/rsyncd.pwd(許可權要設定為600,否則無法備份成功)

[root@jie1 ~]# vim /etc/rsyncd.conf 
############vim /etc/rsyncd.conf######################################## 
uid = nobody                    #備份以什麼身份進行,使用者ID 
gid = nobody                    #備份以什麼身份進行,組ID 
use chroot = no                 #禁錮在源目錄 
max connections = 3             #最大連線數,0代表沒有限制 
strict modes = yes              #是否檢查口令檔案的許可權 
pid file = /var/run/rsyncd.pid  #執行程式的pid檔案 
log file = /var/log/rsyncd.log  #日誌記錄檔案 
[htdocs]                        #指定認證的備份模組名 
path = /web/htdocs              #需要備份的目錄的路徑 
ignore errors = yes             #忽略一些無關的IO錯誤 
read only = no                  #設定為no,即可以傳至伺服器的相應目錄。 
write only = no                 #設定為no,表示客戶端可以下載檔案 
hosts allow = 172.16.22.3       #可以連線rsync伺服器的主機的IP地址 
hosts deny = *                  #設定禁止連線rsync伺服器的主機地址,*表示  拒絕所有除了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   #許可權給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軟體的安裝及設定

[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             #設定能與系統關聯的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         #設定系統能識別軟體的庫檔案 
[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                      #連結標頭檔案到系統能識別的路徑下 
`/usr/include/inotify' -> `/usr/local/inotify/include/'
[root@jie3 inotify]#

2)、web的相關配置,使得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]# 
[root@jie3 ~]#

3)、配置能連線rsync的密碼檔案和傳輸資料的指令碼

[root@jie3 ~]# vim /etc/rsyncd.pwd 
############################################# 
pwd123  #密碼與rsync伺服器的密碼相同 
############################################### 
[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
####################################################################

驗證實現同步:

##1、先開啟監控的指令碼(inotify主機上) 
[root@jie3 ~]# bash -x rsync.sh & 
#不放在後臺可以檢視同步的詳細過程,生成環境中,建議把此指令碼放到後臺執行,此指令碼會監控客戶端資料是否方式變化,如果變化指令碼就執行,資料不變化,指令碼就會等待著使用者的輸入 
##2、在開一個終端,在此目錄建立檔案(inotify主機上) 
[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、看伺服器端,資料是否已經同步過去 
[root@jie1 ~]# cd /web/htdocs/ 
[root@jie1 htdocs]# ls 
index.html  testdb.php  test.php inotify.php  #資料已經被同步過來 
[root@jie1 htdocs]#

rsync +inotify這種能實現資料的同步,但是當網路很繁忙,且檔案變化比較頻繁時,而且需要同步的rsync伺服器端比較多時,rsync+inotify肯定是滿足不了需求的,於是rsync+sersync這種更快更節約資源實現web資料同步可以彌補rsync+inotify帶來的不足,rsync+inotify還有一個重大的缺點就是資料傳輸只是單向的,當運維人員由於“粗心”把資料直接傳輸rsync伺服器端時,inotify主機是得不到rsync伺服器端的資料,於是unison+inotify實現web資料雙向同步,解決了rsync+inotify的這一缺點。

三、rsync+sersync更快更節約資源實現web資料同步

sersync與inotify相比有以下優點:

  • sersync是使用c++編寫,而且對linux系統檔案系統產生的臨時檔案和重複的檔案操作進行過濾,所以在結合rsync同步的時候,節省了執行時耗和網路資源。因此更快。
  • sersync配置起來很簡單,其中bin目錄下已經有基本上靜態編譯的2進位制檔案,配合bin目錄下的xml配置檔案直接使用即可。
  • sersync使用多執行緒進行同步,尤其在同步較大檔案時,能夠保證多個伺服器實時保持同步狀態。
  • sersync有出錯處理機制,通過失敗佇列對出錯的檔案重新同步,如果仍舊失敗,則按設定時長對同步失敗的檔案重新同步。
  • sersync自帶crontab功能,只需在xml配置檔案中開啟,即可按您的要求,隔一段時間整體同步一次。無需再額外配置crontab功能。

rsync+web伺服器端的配置:

1)、安裝相關軟體

[root@jie1 ~]# yum -y install rsync xinetd httpd 
#rsync服務通常基於超級守護程式xinetd管理的方式來實現,因此需要事先安裝rysnc和xinetd

2)、web的相關配置,使得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 ~]# ls 
[root@jie1 ~]#

3)、rsync服務的相關配置

###====此配置檔案的解釋,在rsync+inotify中已經解釋了=====#### 
[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軟體,做初始設定

[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"> 
  #設定本地的ip地址和監聽的埠 
    <host hostip="172.16.22.3" port="8008"></host> 
  #debug模式是否開啟 
    <debug start="false"/> 
  #xfs檔案系統是否開啟 
    <fileSystem xfs="false"/> 
  #同步時,是否支援正規表示式,預設關閉 
    <filter start="false"> 
    <exclude expression="(.*)\.svn"></exclude> 
    <exclude expression="(.*)\.gz"></exclude> 
    <exclude expression="^info/*"></exclude> 
    <exclude expression="^static/*"></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="/website"> 
  #rsync伺服器的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指令引數 
        <commonParams params="-artuz"/> 
  #rsync同步認證設定的內容,user指定使用者名稱,password指定存放密碼的檔案路徑 
        <auth start="true" users="backuper" passwordfile="/usr/local/sersync/sersync.pwd"/> 
  #設定rsync遠端服務埠 
        <userDefinedPort start="false" port="874"/><!-- port=874 --> 
  #設定超時時間 
       <timeout start="true" time="100"/><!-- timeout=100 --> 
  #設定ssh加密傳輸模式,預設關閉 
        <ssh start="false"/> 
    </rsync> 
  #設定sersync傳輸失敗日誌指令碼路徑 
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> 
  #設定rsync+crontab定時傳輸,預設關閉 
    <crontab start="false" schedule="600"><!--600mins--> 
        <crontabfilter start="false"> 
        <exclude expression="*.php"></exclude> 
        <exclude expression=\'#\'" /*"></exclude> 
        </crontabfilter> 
    </crontab> 
  #設定sersync傳輸後呼叫name指定的外掛指令碼,預設關閉 
    <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> 
#######################################################################

驗證實現同步:

###sersync客戶端的,開啟同步機制,進行監控,然後建立檔案 
[root@jie3 website]# sersync2 -r -d & 
[root@jie3 ~]# cd /website/ 
[root@jie3 website]# touch index.html  testdb.php  test.html  test.php 
###rsync伺服器端,檢視可以來著sersync客戶端的同步檔案 
[root@jie1 ~]# cd /web/htdocs/ 
[root@jie1 htdocs]# ls 
index.html  testdb.php  test.html  test.php 
[root@jie1 htdocs]#

四、unison+inotify實現web資料雙向同步

Unison是一款跨平臺的檔案同步物件,不僅支撐本地對本地同步,也支撐經由過程SSH、RSH和Socket等收集和談進行同步。
Unison支撐雙向同步操縱,你既可以從A同步到B,也可以從B同步到A,這些都不須要額外的設定。

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

[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環境變數 
[root@jie1 inotify]# echo "PATH=/usr/local/inotify/bin:$PATH" >/etc/profile.d/inotify.sh 
[root@jie1 inotify]# source /etc/profile.d/inotify.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 
##連結庫檔案到系統識別的路徑 
[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,複製即可 
[root@jie1 unison-2.32.52]# make install 
[root@jie1 unison-2.32.52]# cp unison /usr/local/bin

2)、伺服器A生成的公鑰傳到伺服器B上:

##把伺服器A生成的公鑰傳到伺服器B上#### 
[root@jie1 ~]# ssh-keygen -t rsa   #生成ssh的金鑰對 
[root@jie1 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.3:/root  #生成的金鑰在家目錄的ssh檔案中,ssh檔案為隱藏檔案,通過scp複製到伺服器B上 
[root@jie3 ~]# mv id_rsa.pub .ssh/authorized_keys  #在伺服器B上把伺服器A傳來的公鑰檔案改名並存放到ssh目錄下 
[root@jie3 ~]# chmod 600 .ssh/authorized_keys  #給公鑰檔案改許可權為600 
[root@jie1 ~]# service sshd restart  #重啟sshd服務 
Stopping sshd:                                             [  OK  ] 
Starting sshd:                                             [  OK  ] 
[root@jie1 ~]#

3)、伺服器B生成的公鑰傳到伺服器A上:

##把伺服器B生成的公鑰傳到伺服器A上#### 
[root@jie3 ~]# ssh-keygen -t rsa   #生成ssh的金鑰對 
[root@jie3 ~]# scp ~/.ssh/id_rsa.pub  172.16.22.1:/root  #生成的金鑰在家目錄的ssh檔案中,ssh檔案為隱藏檔案,通過scp複製到伺服器B上 
[root@jie1 ~]# mv id_rsa.pub .ssh/authorized_keys  #在伺服器A上把伺服器B傳來的公鑰檔案改名並存放到ssh目錄下 
[root@jie1 ~]# chmod 600 .ssh/authorized_keys  #給公鑰檔案改許可權為600 
[root@jie3 ~]# service sshd restart  #重啟sshd服務 
Stopping sshd:                                             [  OK  ] 
Starting sshd:                                             [  OK  ] 
[root@jie3 ~]#

4)、分別搭建web服務,伺服器A的網頁檔案存放路徑為/web/htdocs,伺服器B的網頁存放路徑為/website

##伺服器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]# 
##伺服器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同步的指令碼進行測試

##伺服器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
##################################################################### 
##伺服器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
##################################################################### 
##伺服器A的測試 
[root@jie1 ~]# sh -x serA.sh  #先執行unison同步指令碼,檢視過程 
[root@jie1 ~]# cd /web/htdocs/ 
[root@jie1 htdocs]# touch serA.txt SerA.html SerA.php  #然後建立檔案 
[root@jie1 htdocs]# ls 
SerA.html  SerA.php  serA.txt  SerB.html  SerB.php  SerB.txt 
##伺服器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 
###=====可以把指令碼設定開機自啟,放到rc.local檔案中,且放在後臺執行

相關文章