1..背景(本人表达能力有限)

环境描述,有两台nginx+phpweb服务器,前台为haproxy,用来实现双机负载均衡(这里不描述haproxy的安装使用)

Web1    192.168.0.128  (网站目录/web

Web2    192.168.0.129  (网站目录/web

Haproxy  192.168.0.130

现在出现的问题是,用户访问网站上传资料的时候,有可能是上传到web1,也有可能是上传到web2,需要网站双机实时互相备份。

设计思想如下:

Web1web2上面都建立虚拟目录,虚拟目录里面放置两个文件,check.phpcheck.txt,每访问一次check.phpcheck.txt的内容就会更新一次

使用inotify监听web1的网站目录,如果发生变化就使用curl访问web2上面的check.phpweb2上面使用inotify监听check.txt,如果发生变化就从web1上面下载更新网站目录。

同理inotify监听web2的网站目录,如果发生变化就使用curl访问web1上面的check.php

Web1上面使用inotify监听check.txt,如果发生变化就从web2上面下载更新网站目录。

2.配置

安装rsyncinotify, 由于安装非常简单,这里不在讲述。

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"));

?>

############

Inotifyrsync的参数可以查看man文件