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 文件