场景:web服务器的流量过大,超过了idc的限制,先准备将流量分到两台机器上,保证两台web机器的内容一致,而且能做双向同步
环境介绍:
web1:192.168.2.222
web2:192.168.2.206
搭建过程如下:
安装unison
要按照unison,首先需要安装ocaml,版本至少为3.07或更高
下载地址:
tar xf ocaml-3.10.2.tar.gz
cd ocaml-3.10.2
./configure
make world opt
make install
安装unison
下载地址:
tar xvf unison-2.32.52.tar.gz
cd unison-2.32.52
make UISTYLE=text THREADS=true STATIC=true
##在执行上面的编译时会有如下的问题:
/bin/sh: line 1: etags: command not found 该问题可以忽略,不影响使用!
这里不需要执行make install, 执行make install 会报错
unison /usr/local/bin
注释:
UISTYLE=text THREADS=true STATIC=true表示使用行方式,加入线程支持以静态模式编译
安装inotify-tools:
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
接着大家无密码的ssh认证,双向认证(204可以直接登录到222,222也能直接登录到204)
以root用户登陆
在服务器204和222上创建.ssh目录
mkdir ~/.ssh
700 ~/.ssh
生成RSA密钥
ssh-keygen -t rsa
(然后连续三次回车)
cp id_rsa.pub authorized_keys
cp authorized_keys id_rsa 192.168.2.222:/root/.ssh
然后ssh ip地址 测试
书写脚本:
204上脚本内容如下:
-
[root@haproxy2 scripts]# cat inotify.sh.bak
-
#/bin/bash
-
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
-
if [ ${UNISON} -lt 1 ]
-
then
-
ip2="192.168.2.222"
-
src2="/www/"
-
dst2="/www/"
-
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
-
do
-
/usr/local/bin/unison -batch $src2 -ignore=Path abc ssh://$ip2/$dst2
-
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
-
echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
-
done
-
fi
222上脚本如下:
-
[root@haproxy2 scripts]# cat inotify.sh.bak
-
#/bin/bash
-
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
-
if [ ${UNISON} -lt 1 ]
-
then
-
ip2="192.168.2.222"
-
src2="/www/"
-
dst2="/www/"
-
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
-
do
-
/usr/local/bin/unison -batch $src2 -ignore=Path abc ssh://$ip2/$dst2
-
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
-
echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
-
done
-
fi
###借鉴了网上的东西,几乎没动脑子! 向作者表示感谢
我简单修改了下:
222上修改了default.prf文件如下
[root@haproxy2 ~]# mkdir /root/.unison/
-
[root@haproxy2 ~]# cd .unison/
-
[root@haproxy2 .unison]# ls
-
ar9daae07790ca0eafa88e9b5c534aa3c7 default.prf
-
[root@haproxy2 .unison]# cat default.prf
-
# Unison preferences file
-
root = /www/
-
root = ssh://192.168.2.204//www/
-
ignore = Path xyz/* ##/www/xyz目录下东西不同步
-
batch = true
204上文件如下:
-
[root@haproxy2 ~]# mkdir /root/.unison/
-
[root@haproxy2 ~]# cd .unison/
-
[root@haproxy2 .unison]# ls
-
ar9daae07790ca0eafa88e9b5c534aa3c7 default.prf
-
[root@haproxy2 .unison]# cat default.prf
-
# Unison preferences file
-
root = /www/
-
root = ssh://192.168.2.222//www/
-
ignore = Path xyz/*
-
batch = true
-
修改shell脚本如下:
204和222上的脚本是一样的
-
[root@haproxy2 ~]# cd /home/scripts/
-
[root@haproxy2 scripts]# ls
-
inotify.log inotify.sh inotify.sh.bak tongbu.sh
-
[root@haproxy2 scripts]# cat inotify.sh
-
#/bin/bash
-
src2="/www/"
-
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
-
if [ ${UNISON} -lt 1 ]
-
then
-
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
-
do
-
/usr/local/bin/unison
-
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
-
echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
-
done
-
fi
然后将其放到cron中去执行:
-
[root@haproxy2 scripts]# crontab -l
-
* * * * * /bin/sh /home/scripts/inotify.sh >> /home/scripts/inotify.log 2>&1
-
执行时发现一个问题:
-
inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
-
解决方法:
-
32位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
-
64位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
然后分别在两个机器上的/www目录下进行测试
生产环境下遇到一个问题:
Failed to watch /home/webdata/data/app/loldb/; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via /proc/sys/fs/inotify/max_user_watches'.
[root@s4 scripts]# echo 800000000 > /proc/sys/fs/inotify/max_user_watches
You have mail in /var/spool/mail/root
[root@s4 scripts]# cat /proc/sys/fs/inotify/max_user_watches
800000000
[root@s4 scripts]#
阅读(1457) | 评论(0) | 转发(0) |