網站實時鏡像

3.網站實時鏡像
問題
公司的網站服務器有兩個鏡像站點,分別放在北京和上海的IDC機房。現在要求利用rsync同步機制實現“服務器A–>服務器B”的實時鏡像同步。
需要完成的配置任務如下:
1)雙方的目錄均爲 /var/www/html/
2)以 svr5 爲同步發起方,配置 inotify+rsync 同步操作
3)以 pc205 爲同步目標,基於SSH方式進行驗證
方案
使用兩臺RHEL6虛擬機,其中一臺作爲服務器A(192.168.4.5),另外一臺作爲服務器B(192.168.4.205),兩臺主機都安裝httpd網站軟件,如圖-3所示。
在這裏插入圖片描述
圖-3
安裝並啓用inotify-tools工具,就可以在同步發起端實現對指定目錄的監控,一旦出現更改、增加文件等操作,立即觸發相應的命令操作(本例中即上行同步)。根據監控結果觸發同步操作,其中用到了一部分Shell控制語句,最好建立專用腳本來實現,本例中只需理解腳本的用途即可。
步驟
實現此案例需要按照如下步驟進行。
步驟一:準備網頁環境
1)在svr5上,啓用httpd網站服務、部署測試網頁
[root@svr5 ~]# yum -y install httpd
… …
[root@svr5 ~]# service httpd restart
停止 httpd: [確定]
正在啓動 httpd: [確定]
[root@svr5 ~]# chkconfig httpd on

[root@svr5 ~]# echo “Welcome to Tarena” > /var/www/html/index.html
[root@svr5 ~]# elinks -dump http://192.168.4.5 //訪問測試網頁
Welcome to Tarena
2)在pc205上,啓用httpd網站服務,先不用部署網頁
[root@pc205 ~]# yum -y install httpd
… …
[root@pc205 ~]# service httpd restart
停止 httpd: [確定]
正在啓動 httpd: [確定]
[root@pc205 ~]# chkconfig httpd on

[root@pc205 ~]# ls /var/www/html/* //網頁目錄爲空
ls: 無法訪問/var/www/html/*: 沒有那個文件或目錄
步驟二:配置、啓用實時同步腳本
1)在svr5上,安裝inotify-tools工具包
[root@svr5 ~]# tar xf inotify-tools-3.13.tar.gz
[root@svr5 ~]# cd inotify-tools-3.13
[root@svr5 inotify-tools-3.13]# ./configure
… …
[root@svr5 ~]# make && make install
2)創建並部署SSH公鑰,實現免密碼驗證
[root@svr5 ~]# ssh-keygen //創建密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //回車
Enter passphrase (empty for no passphrase): //回車
Enter same passphrase again: //回車
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
… …

[root@svr5 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
//上傳公鑰
[email protected]’s password: //驗證對方密碼
Now try logging into the machine, with “ssh ‘[email protected]’”, and check in:

.ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

[root@svr5 ~]# ssh [email protected] //驗證免密碼登錄
Last login: Thu Dec 24 00:53:00 2015 from 192.168.4.5
[root@pc205 ~]# exit //返回客戶機
logout
Connection to 192.168.4.205 closed.
3)建立inotify實時同步腳本文件
爲了方便腳本的移植使用,在腳本中定義了兩個變量:TARGET_DIR用來指定監控的目標文件夾,而RSYNC_CMD用來指定需要觸發的同步操作。注意給腳本添加x執行權限,實際使用時根據需要變更這兩個變量的值即可
[root@svr5 ~]# vim /root/isync.sh //新建腳本
#!/bin/bash
TARGET_DIR="/var/www/html" #//指定監控目錄
RSYNC_CMD=“rsync -az --delete /var/www/html/ 192.168.4.205:/var/www/html/”
#//指定同步操作
inotifywait -mrq -e modify,move,create,delete,attrib /opt | while read -n5 X
do
$RSYNC_CMD
done &
[root@svr5 ~]# chmod +x /root/isync.sh //添加執行權限
4)啓動實時同步腳本程序
此腳本一旦運行後,會一直在後臺運行;如果有必要,可以將此腳本添加爲開機自啓動任務。
[root@svr5 ~]# /root/isync.sh //執行腳本
[root@svr5 ~]#
步驟三:測試實時同步效果
1)在svr5上向/var/www/html/目錄下添加一個文件
[root@svr5 ~]# touch /var/www/html/a.html
[root@svr5 ~]# ls -lh /var/www/html/.html
-rw-r–r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
-rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
2)在pc205上觀察/var/www/html目錄下的變化
[root@pc205 ~]# ls -lh /var/www/html/
.html
-rw-r–r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
-rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]#
3)在svr5上刪除剛添加的文件a.html
[root@svr5 ~]# rm -rf /var/www/html/a.html
[root@svr5 ~]# ls -lh /var/www/html/.html
-rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
4)在pc205上再次觀察/var/www/html目錄下的變化
[root@pc205 ~]# ls -lh /var/www/html/
.html
-rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章