Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2763237
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2014-04-15 14:37:31

场景: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上脚本内容如下:

  1. [root@haproxy2 scripts]# cat inotify.sh.bak
  2. #/bin/bash
  3. UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
  4. if [ ${UNISON} -lt 1 ]
  5. then
  6. ip2="192.168.2.222"
  7. src2="/www/"
  8. dst2="/www/"
  9. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
  10. do
  11. /usr/local/bin/unison -batch $src2 -ignore=Path abc ssh://$ip2/$dst2
  12. echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
  13. echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
  14. done
  15. fi
222上脚本如下:

  1. [root@haproxy2 scripts]# cat inotify.sh.bak
  2. #/bin/bash
  3. UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
  4. if [ ${UNISON} -lt 1 ]
  5. then
  6. ip2="192.168.2.222"
  7. src2="/www/"
  8. dst2="/www/"
  9. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
  10. do
  11. /usr/local/bin/unison -batch $src2 -ignore=Path abc ssh://$ip2/$dst2
  12. echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
  13. echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
  14. done
  15. fi
###借鉴了网上的东西,几乎没动脑子! 向作者表示感谢
我简单修改了下:
222上修改了default.prf文件如下
[root@haproxy2 ~]# mkdir /root/.unison/
  1. [root@haproxy2 ~]# cd .unison/
  2. [root@haproxy2 .unison]# ls
  3. ar9daae07790ca0eafa88e9b5c534aa3c7 default.prf
  4. [root@haproxy2 .unison]# cat default.prf
  5. # Unison preferences file
  6. root = /www/
  7. root = ssh://192.168.2.204//www/
  8. ignore = Path xyz/* ##/www/xyz目录下东西不同步
  9. batch = true

204上文件如下:

  1. [root@haproxy2 ~]# mkdir /root/.unison/
  2. [root@haproxy2 ~]# cd .unison/
  3. [root@haproxy2 .unison]# ls
  4. ar9daae07790ca0eafa88e9b5c534aa3c7 default.prf
  5. [root@haproxy2 .unison]# cat default.prf
  6. # Unison preferences file
  7. root = /www/
  8. root = ssh://192.168.2.222//www/
  9. ignore = Path xyz/*
  10. batch = true

修改shell脚本如下:
204和222上的脚本是一样的

  1. [root@haproxy2 ~]# cd /home/scripts/
  2. [root@haproxy2 scripts]# ls
  3. inotify.log inotify.sh inotify.sh.bak tongbu.sh
  4. [root@haproxy2 scripts]# cat inotify.sh
  5. #/bin/bash
  6. src2="/www/"
  7. UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
  8. if [ ${UNISON} -lt 1 ]
  9. then
  10. /usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
  11. do
  12. /usr/local/bin/unison
  13. echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
  14. echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
  15. done
  16. fi
然后将其放到cron中去执行:
  1. [root@haproxy2 scripts]# crontab -l
  2. * * * * * /bin/sh /home/scripts/inotify.sh >> /home/scripts/inotify.log 2>&1
  3.  
执行时发现一个问题:

  1. inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
  2. 解决方法:
  3. 32位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
  4. 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]# 





阅读(1419) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~