1..背景(本人表達能力有限)
環境描述,有兩臺nginx+php的web服務器,前臺爲haproxy,用來實現雙機負載均衡(這裏不描述haproxy的安裝使用)
Web1 192.168.0.128 (網站目錄/web)
Web2 192.168.0.129 (網站目錄/web)
Haproxy 192.168.0.130
現在出現的問題是,用戶訪問網站上傳資料的時候,有可能是上傳到web1,也有可能是上傳到web2,需要網站雙機實時互相備份。
設計思想如下:
Web1和web2上面都建立虛擬目錄,虛擬目錄裏面放置兩個文件,check.php和check.txt,每訪問一次check.php,check.txt的內容就會更新一次
使用inotify監聽web1的網站目錄,如果發生變化就使用curl訪問web2上面的check.php。web2上面使用inotify監聽check.txt,如果發生變化就從web1上面下載更新網站目錄。
同理inotify監聽web2的網站目錄,如果發生變化就使用curl訪問web1上面的check.php。
Web1上面使用inotify監聽check.txt,如果發生變化就從web2上面下載更新網站目錄。
2.配置
安裝rsync和inotify, 由於安裝非常簡單,這裏不在講述。
Web1配置
############################
#vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /web
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.0.129
hosts deny = *
list = false
uid = root
gid = root
auth users = user
secrets file = /etc/server.pwd
##################
服務端密碼文件(供web2訪問使用)
#vim /etc/server.pwd
user:user
#chmod 600 /etc/server.pwd
#rsync –daemon
將rsync服務加入到自啓動文件中:
echo “/usr/local/bin/rsync --daemon” >>/etc/rc.local
##########################################
客戶端密碼文件(訪問web2使用)
#vim /etc/rsync.pas
user
#chmod 600 /etc/rsync.pas
#############################
觸發腳本(觸發web2,讓web2從本機同步更新)
#!/bin/bash
src=/web/
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,create,attrib $src | while read files
do
curl "192.168.0.129/check.php"
done
腳本意思是如果/web目錄變化,就通過curl訪問192.168.0.129/check.php
########################
同步腳本(從web2更新數據)
#!/bin/bash
host=192.168.0.129
localsrc=/var/www/
src=/web/
dst=www
user=user
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $localsrc | while read files
do
/usr/local/rsync/bin/rsync -avzP --progress $user@$host::$dst $src --password-file=/etc/rsync.pas
Done
腳本的意思是如果/var/www發生變化就更新/web內容
Web2設置
############################
#vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /web
comment = web2 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.0.128
hosts deny = *
list = false
uid = root
gid = root
auth users = user
secrets file = /etc/server.pwd
##################
服務端密碼文件(供web1訪問使用)
#vim /etc/server.pwd
user:user
#chmod 600 /etc/server.pwd
#rsync –daemon
將rsync服務加入到自啓動文件中:
echo “/usr/local/bin/rsync --daemon” >>/etc/rc.local
##########################################
客戶端密碼文件(訪問web1使用)
#vim /etc/rsync.pas
user
#chmod 600 /etc/rsync.pas
#############################
觸發腳本(觸發web1,讓web1從本機同步更新)
#!/bin/bash
src=/web/
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,create,attrib $src | while read files
do
curl "192.168.0.128/check.php"
done
腳本意思是如果/web目錄變化,就通過curl訪問192.168.0.128/check.php
########################
同步腳本(從web1更新數據)
#!/bin/bash
host=192.168.0.128
localsrc=/var/www/
src=/web/
dst=www
user=user
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $localsrc | while read files
do
/usr/local/rsync/bin/rsync -avzP --progress $user@$host::$dst $src --password-file=/etc/rsync.pas
done
腳本的意思是如果/var/www發生變化就更新/web內容
3,虛擬主機的目錄爲/var/www (兩邊都一樣,虛擬主機怎麼設置,這裏不做說明)
#cd /var/www
#ll
-rw-r--r-- 1 root root 62 06-20 04:12 check.php
-rwxr-xrwx 1 root root 10 06-21 06:52 check.txt
#cat check.php
<?php
file_put_contents("check.txt", time("Y-m-d H:i:s"));
?>
############
Inotify和rsync的參數可以查看man文件