您的位置:首页 > 运维架构 > Linux

linux下基于unison+inotify实现web双向实时同步

2015-02-12 15:39 519 查看
环境:
服务器A:192.168.162.129,同步目录:/var/www/html
服务器B:192.168.162.130,同步目录:/var/www/html

操作系统:CentOS 6.5 32/64位系统均可

2台服务器安装 web服务、gcc gcc-c++编译

yum install gcc gcc-c++
yum install httpd httpd-devel

(一)SERVER-A 配置 SERVER-B 配置: 同时配置

1 安装unison 需要先安装 Objective Caml compiler (ocaml) 编译器

tar zxvf ocaml-4.02.1.tar.gz
cd ocaml-3.10.2
./configure
make world opt
make install
cd ..

2 安装Unison
tar zxvf unison-2.48.3.tar.gz
cd unison-2.48.3
yum -y install ctags-etags
make UISTYLE=text
mkdir /root/bin
cp unison /root/bin/
cp unison /root/bin/unison-2.48
cp unison /usr/local/bin/unison
make install
cd ..

3 安装inotify-tools

tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify (安装到/usr/local/下 ) 这里一定要记住 写脚本时要用到路径
make && make install
echo "PATH=/usr/local/inotify/bin:$PATH" >/etc/profile.d/inotify.sh
source /etc/profile
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf

ldconfig -v | grep inotify -显示如下信息

ln -sv /usr/local/inotify/include/ /usr/include/inotify

(二) 配置双机SSH信任

以root用户登陆

1在服务器 A.B 上创建.ssh目录 并创建RSA秘钥
mkdir ~/.ssh
chmod 700 ~/.ssh
生成RSA密钥
ssh-keygen -t rsa
(然后连续三次回车)

2 在SERVER-A上执行:

scp /root/.ssh/id_rsa.pub 192.168.162.130:/root/
mv id_rsa.pub .ssh/authorized_keys (需要2台服务器执行过上一步骤才行)

cd ~/.ssh
ssh "-p 22" 192.168.162.129 cat /root/.ssh/id_rsa.pub >> authorized_keys #小写p
ssh "-p 22" 192.168.162.130 cat /root/.ssh/id_rsa.pub >> authorized_keys
scp -P 22 authorized_keys 192.168.162.130:/root/.ssh/ #大写P
chmod 600 /root/.ssh/authorized_keys

3 在SERVER-B上执行:

scp /root/.ssh/id_rsa.pub 192.168.162.129:/root/
mv id_rsa.pub .ssh/authorized_keys (需要2台服务器执行过上一步骤才行)

cd ~/.ssh
ssh "-p 22" 192.168.162.130 cat /root/.ssh/id_rsa.pub >> authorized_keys #小写p
ssh "-p 22" 192.168.162.129 cat /root/.ssh/id_rsa.pub >> authorized_keys
scp -P 22 authorized_keys 192.168.162.129:/root/.ssh/ #大写P
chmod 600 /root/.ssh/authorized_keys

测试

需要关闭防火墙及SELinux
service iptables stop
setenforce 0

SERVER-A ssh 192.168.162.130 ifconfig eth0
SERVER-B ssh 192.168.162.129 ifconfig eth0

第一次执行需要密码 以后执行不需要密码 说明认证成功

(三)在2台服务器上web同步添加脚本

SERVER-A服务器
mkdir /script
vim /script/inotify.sh
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.162.130"
src2="/var/www/html/"
dst2="/var/www/html/ "
注意安装路径/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 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

SERVER-B 服务器
mkdir /script
vim /script/inotify.sh
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.162.129"
src2="/var/www/html/"
dst2="/var/www/html/ "
注意安装路径 /usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 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

在2台服务器上添加 可执行权限 并添加实时监控脚本

chmod a+x /script/inotify.sh
crontab -e
#unison+inotify
* * * * * /bin/sh /script/inotify.sh > /dev/null 2>&1 &

测试结果

在SERVER-A SERVER-B的/var/www/html 分别创建不同的文件、看看2边会不会实时同步

不重启电脑,手动执行脚本也可以测试

sh /script/inotify

在其中一台/var/www目录中添加,或修改,或删除文件的时候,可以看到脚本状态,同时另一台服务器也应该会跟随操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息