欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
四、unison+inotify实现web数据双向同步
Unison是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支撑经由过程SSH、RSH和Socket等收集和谈进行同步。
Unison支撑双向同步操纵,你既可以从A同步到B,也可以从B同步到A,这些都不须要额外的设定。
1)、两个服务器都编译安装这三个源码包:(在此我只写一台服务器的编译安装过程)
. 代码如下:
[root@jie1 ~]#wget ftp://distro.ibiblio.org/slitaz/sources/packages-2.0/o/ocaml-3.10.2.tar.gz
[root@jie1~]#wget freebsd.ntu.edu.tw/FreeBSD/ports/distfiles/unison-2.32.52/unison-2.32.52.tar.gz
[root@jie1~]#wget cloud.github./downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@jie1 ~]# ls
anaconda-ks.cfg install.log ocaml-3.10.2.tar.gz
inotify-tools-3.14.tar.gz install.log.syslog unison-2.32.52.tar.gz
[root@jie1 ~]# tar xf inotify-tools-3.14.tar.gz
[root@jie1 ~]# tar xf ocaml-3.10.2.tar.gz
[root@jie1 ~]# tar xf unison-2.32.52.tar.gz
##编译安装inotify
[root@jie1 ~]# cd inotify-tools-3.14
[root@jie1 inotify-tools-3.14]# ./configure –prefix=/usr/local/inotify && make && make install
[root@jie1 inotify-tools-3.14]# cd /usr/local/inotify/
##修改PATH环境变量
[root@jie1 inotify]# echo “PATH=/usr/local/inotify/bin:$PATH” >/etc/profile.d/inotify.sh
[root@jie1 inotify]# source /etc/profile.d/inotify.sh
##添加库文件到系统识别的路径
[root@jie1 inotify]# echo “/usr/local/inotify/lib” >/etc/ld.so.conf.d/inotify.conf
[root@jie1 inotify]# ldconfig -v | grep inotify
/usr/local/inotify/lib:
libinotifytools.so.0 -> libinotifytools.so.0.4.1
##链接库文件到系统识别的路径
[root@jie1 inotify]# ln -sv /usr/local/inotify/include/ /usr/include/inotify
`/usr/include/inotify’ -> `/usr/local/inotify/include/’
##编译安装ocaml,unison依赖于ocaml
[root@jie1 inotify]#cd /root/ocaml-3.10.2
[root@jie1 ocaml-3.10.2]#./configure
[root@jie1 ocaml-3.10.2]#make world opt
[root@jie1 ocaml-3.10.2]#make install
##编译安装unison
[root@jie1 ocaml-3.10.2]# cd /root/unison-2.32.52
##安装依赖性包
[root@jie1 unison-2.32.52]#yum -y install ctags-etags
[root@jie1 unison-2.32.52]# make UISTYLE=text
##make install会提示错误,此错误就是要你cp unison /usr/local/bin,复制即可
[root@jie1 unison-2.32.52]# make install
[root@jie1 unison-2.32.52]# cp unison /usr/local/bin
2)、服务器A生成的公钥传到服务器B上:
. 代码如下:
##把服务器A生成的公钥传到服务器B上####
[root@jie1 ~]# ssh-keygen -t rsa #生成ssh的密钥对
[root@jie1 ~]# scp ~/.ssh/id_rsa.pub 172.16.22.3:/root #生成的密钥在家目录的ssh文件中,ssh文件为隐藏文件,通过scp复制到服务器B上
[root@jie3 ~]# mv id_rsa.pub .ssh/authorized_keys #在服务器B上把服务器A传来的公钥文件改名并存放到ssh目录下
[root@jie3 ~]# chmod 600 .ssh/authorized_keys #给公钥文件改权限为600
[root@jie1 ~]# service sshd restart #重启sshd服务
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@jie1 ~]#
3)、服务器B生成的公钥传到服务器A上:
. 代码如下:
##把服务器B生成的公钥传到服务器A上####
[root@jie3 ~]# ssh-keygen -t rsa #生成ssh的密钥对
[root@jie3 ~]# scp ~/.ssh/id_rsa.pub 172.16.22.1:/root #生成的密钥在家目录的ssh文件中,ssh文件为隐藏文件,通过scp复制到服务器B上
[root@jie1 ~]# mv id_rsa.pub .ssh/authorized_keys #在服务器A上把服务器B传来的公钥文件改名并存放到ssh目录下
[root@jie1 ~]# chmod 600 .ssh/authorized_keys #给公钥文件改权限为600
[root@jie3 ~]# service sshd restart #重启sshd服务
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@jie3 ~]#
4)、分别搭建web服务,服务器A的网页文件存放路径为/web/htdocs,服务器B的网页存放路径为/website
. 代码如下:
##服务器A搭建web的配置
[root@jie1 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.1:80
#DocumentRoot “/var/www/html”
<VirtualHost *:80>
DocumentRoot /web/htdocs
</VirtualHost>
#######################################
[root@jie1 ~]# mkdir -pv /web/htdocs
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# ls
[root@jie1 htdocs]#
##服务器B搭建web的配置
[root@jie3 /]# vim /etc/httpd/conf/httpd.conf
########################################
ServerName 172.16.22.3:80
#DocumentRoot “/var/www/html”
<VirtualHost *:80>
DocumentRoot /website
</VirtualHost>
#######################################
[root@jie3 /]# mkdir /website
[root@jie3 /]# httpd -t
Syntax OK
[root@jie3 /]# service httpd start
Starting httpd: [ OK ]
[root@jie3 ~]# cd /website/
[root@jie3 website]# ls
[root@jie3 website]#
5)、编unison同步的脚本进行测试
. 代码如下:
##服务器A的脚本
[root@jie1 ~]# vim serA.sh
######################################################################
#/bin/bash
ipB=”172.16.22.3″
srcA=”/web/htdocs”
dstB=”/website”
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcA | while read line; do
/usr/local/bin/unison -batch $srcA ssh://$ipB/$dstB
echo -n “$line ” 》 /var/log/inotify.log
echo `date | cut -d ” ” -f1-4` 》 /var/log/inotify.log
done
#####################################################################
##服务器B的脚本
[root@jie3 ~]# vim serB.sh
#####################################################################
#/bin/bash
ipA=”172.16.22.1″
srcB=”/website”
dstA=”/web/htdocs”
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcB | while read line; do
/usr/local/bin/unison -batch $srcB ssh://$ipA/$dstA
echo -n “$line ” 》 /var/log/inotify.log
echo `date | cut -d ” ” -f1-4` 》 /var/log/inotify.log
done
#####################################################################
##服务器A的测试
[root@jie1 ~]# sh -x serA.sh #先运行unison同步脚本,查看过程
[root@jie1 ~]# cd /web/htdocs/
[root@jie1 htdocs]# touch serA.txt SerA.html SerA.php #然后创建文件
[root@jie1 htdocs]# ls
SerA.html SerA.php serA.txt SerB.html SerB.php SerB.txt
##服务器B的测试
[root@jie3 ~]# sh -x serB.sh
[root@jie3 ~]# cd /website/
[root@jie3 website]# touch SerB.txt SerB.html SerB.php
[root@jie3 website]# ls
SerA.html SerA.php serA.txt SerB.html SerB.php SerB.txt
###=====可以把脚本设置开机自启,放到rc.local文件中,且放在后台运行
[1][2][3][4]
没有口水与汗水,就没有成功的泪水。