unison + inotify  实现web 数据双向同步
 
unison 是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支持通过SSH,RSH和Socket 等网络协议进行同步。unison 支持双向同步,你可以同A同步到B ,也可以从B同步到A,这些都需要额外的设定。
 
 
源码包下载地址:链接:https://pan.baidu.com/s/1b8DU70 密码:sby7
1. 两台服务器都编译安装这三个源码包,在此只写在一台服务器上的编译安装的过程,另一台请自行安装
[root@localhost ~]# cd soft/
[root@localhost soft]# ls
inotify-tools-3.14.tar.gz  ocaml-3.10.2.tar.gz    v2.3.0.zip
jdk-7u21-linux-x64.rpm     unison-2.32.52.tar.gz
[root@localhost soft]# tar xf inotify-tools-3.14.tar.gz  -C /usr/src
[root@localhost soft]# tar xf ocaml-3.10.2.tar.gz -C /usr/src
[root@localhost soft]# tar xf unison-2.32.52.tar.gz -C /usr/src
[root@localhost soft]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# yum -y install gcc gcc-c++ make
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@localhost inotify-tools-3.14]# cd /usr/local/inotify/
##修改PATH 环境变量:
[root@localhost inotify]# echo "PATH=/usr/local/inotify/bin:$PATH" > /etc/profile.d/inotify.sh
[root@localhost inotify]# source /etc/profile.d/inotify.sh
##添加库文件到系统识别的路径:
[root@localhost inotify]# echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
[root@localhost inotify]# ldconfig -v | grep inotify
/usr/local/inotify/lib:
libinotifytools.so.0 -> libinotifytools.so.0.4.1
##链接库文件到系统识别的路径:
[root@localhost inotify]# ln -sv /usr/local/inotify/include/ /usr/include/inotify
`/usr/include/inotify' -> `/usr/local/inotify/include/'
##编译安装ocaml,unison依赖于ocaml
[root@localhost ocaml-3.10.2]# cd /usr/src/ocaml-3.10.2/
[root@localhost ocaml-3.10.2]# ./configure
[root@localhost ocaml-3.10.2]# make world opt
[root@localhost ocaml-3.10.2]# make install
[root@localhost ocaml-3.10.2]# cd /usr/src/unison-2.32.52/
[root@localhost unison-2.32.52]# yum -y install ctags-etags
[root@localhost unison-2.32.52]# make UISTYLE=text
[root@localhost unison-2.32.52]# make install
mv /root/bin//unison /tmp/unison-29781
mv: 无法获取"/root/bin//unison" 的文件状态(stat): 没有那个文件或目录
make: [doinstall] 错误 1 (忽略)
cp unison /root/bin/
cp: 无法创建普通文件"/root/bin/": 是一个目录
make: *** [doinstall] 错误 1
报错信息,依据提示操作:此错误就是要把unison 复制到/usr/local/bin下
[root@localhost unison-2.32.52]# find / -name unison -type f
/usr/src/unison-2.32.52/unison
[root@localhost unison-2.32.52]# cp /usr/src/unison-2.32.52/unison  /usr/local/bin/
[root@localhost unison-2.32.52]# make install   //在次执行 make install
mv /root/bin//unison /tmp/unison-29807
cp unison /root/bin/
cp unison /root/bin/unison-2.32
 
服务器B 上的安装同上,在此省略。
 
2.服务器A 生成的公钥传到服务器B 上:
[root@localhost unison-2.32.52]# ssh-keygen -t rsa
[root@localhost ~]# scp ~/ .ssh/id_rsa.pub 192.168.9.28:/root/
##在服务器B 上移动公钥并重命名
[root@localhost ~]# mv id_rsa.pub ./.ssh/authorized_keys
##公钥授权为600
[root@localhost ~]# chmod 600 .ssh/authorized_keys
## 服务器B 重启sshd服务
[root@localhost ~]# service sshd restart
 
 
3. 服务器B 上生成的公钥传到服务器A 上
##服务器B 上生成公钥
[root@localhost ~]# ssh-keygen -t rsa
##将服务器B 上公钥传到服务器A 上
[root@localhost ~]# scp ~/ .ssh/id_rsa.pub 192.168.9.27:/root/
##将服务器B 的公钥移动并·重命名
[root@localhost ~]# mv id_rsa.pub .ssh//authorized_keys
## 修改权限为600
[root@localhost ~]# chmod 600 .ssh/authorized_keys
##重启服务器A sshd服务
[root@localhost ~]# service sshd restart
 
 
4.测试两台服务器是否可以免密钥登陆
## 服务器A 登陆服务器B
[root@localhost ~]# ssh root@192.168.9.28
Last login: Wed Nov 29 13:47:07 2017 from 192.168.10.245
[root@localhost ~]# exit
logout
Connection to 192.168.9.28 closed.
 
## 服务器B 登陆服务器A
[root@localhost ~]# ssh root@192.168.9.27
Last login: Wed Nov 29 13:46:59 2017 from 192.168.10.245
[root@localhost ~]# exit
logout
Connection to 192.168.9.27 closed.
 
5、分别搭建web服务器,两台服务器的代码根目录统一设定为/usr/local/zx-runtime/zx
## 服务器A httpd 的搭建
[root@localhost ~]# rpm -q httpd
package httpd is not installed
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
136 Listen 80
949 <VirtualHost *:80>
950 DocumentRoot "/usr/local/zx-runtime/zx/"
951 CustomLog logs/access_log combined
952 ErrorLog logs/error_log
953 <Directory /usr/local/zx-runtime/zx>
954     Options FollowSymLinks
955     AllowOverride None
956     Order deny,allow
957     Deny from all
958     Allow from all
959 </Directory>
960 </VirtualHost>
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# /etc/init.d/httpd start
正在启动 httpd:
##创建apache 根目录
[root@localhost zx]# pwd  
/usr/local/zx-runtime/zx
[root@localhost zx]# ls
index.html
[root@localhost zx]# cat index.html
9.27 页面正常
 
## 服务器B httpd 服务的搭建
[root@localhost ~]# rpm -q httpd
package httpd is not installed
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
1003 <VirtualHost *:80>
1004 DocumentRoot "/usr/local/zx-runtime/zx/"
1005 CustomLog logs/access_log combined
1006 ErrorLog logs/error_log
1007 <Directory /usr/local/zx-runtime/zx>
1008     Options FollowSymLinks
1009     AllowOverride None
1010     Order deny,allow
1011     Deny from all
1012     Allow from all
1013 </Directory>
[root@localhost ~]# /etc/init.d/httpd start
正在启动 httpd:
##创建apache 根目录
[root@localhost zx]# pwd  
/usr/local/zx-runtime/zx
[root@localhost zx]# ls
index.html
[root@localhost zx]# cat index.html
9.28 页面正常
 
 
6. 编辑unison 同步的脚本进行测试
## 服务器A 的脚本
[root@localhost ~]# mkdir scripts
[root@localhost ~]# cd scripts/
[root@localhost scripts]# vim serA.sh
 
#!/bin/bash
ipB="192.168.9.28"
srcA="/usr/local/zx-runtime/zx"
dstB="/usr/local/zx-runtime/zx"
/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@localhost ~]# mkdir scripts
[root@localhost ~]# cd scripts/
[root@localhost scripts]# vim serB.sh
 
#/bin/bash
ipA="192.168.9.27"
srcB="/usr/local/zx-runtime/zx"
dstA="/usr/local/zx-runtime/zx"
/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
 
7.测试webA webB 上传代码是否同步
a .启动webA,webB上的脚本
[root@localhost scripts]# sh -x serA.sh
[root@localhost scripts]# sh -x serB.sh
 
b.在webA上在开个终端,创建{1-5}.txt
[root@localhost zx]# pwd
/usr/local/zx-runtime/zx
[root@localhost zx]# ls
include  index.html  log
[root@localhost zx]# touch {1..5}.txt
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  include  index.html  log
 
在webB上查看:目标文件都已经同步过来
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  include  index.html  log
 
c. 在webB上创建{a..e}.txt,  在web1上查看
[root@localhost zx]# touch {a..e}.txt
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  a.txt  b.txt  c.txt  d.txt  e.txt  include  index.html  log
 
在webA上查看: 目标文件都已同步过来
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  a.txt  b.txt  c.txt  d.txt  e.txt  include  index.html  log
 
d. 删除webA上的*.txt 文件
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  a.txt  b.txt  c.txt  d.txt  e.txt  include  index.html  log
[root@localhost zx]# rm -rf *.txt
[root@localhost zx]# ls
include  index.html  log
 
在webB上查看: 目标文件已被删除
[root@localhost zx]# ls
include  index.html  log
 
 
 
扩展:在实际生产环境,apache 根目录有log目录和compiled缓存目录不需要同步。unison 工具支持排除指定目录功能
 

模式匹配:

如下参数 ignore/ignorenot, follow, sortfirst/sortlast, backup, merge 定义各自的匹配模式。 例如: ignore = pattern pattern 的语法格式

● Regex regexp 即常规表达式。

● Name name   路径的最后一部分与 “name" 匹配。可以使用通配符,见下面描述。

● Path path 全路径与 "path" 匹配。可以使用通配符,见下面描述。

以下通配符可以用在 Path 和 Name 中:

● ? 匹配除了 "/" 之外,任意单个字符;

● * 匹配除了 "/" 之外的任意字符;如果用在 "Name" 中,则不和以 "."开始的文件匹配,而用在 "Path" 中,可以和 "." 开始的向匹配。

● [xyz] 匹配 {x,y,z} 中任一字符;

● {a,bb,ccc} 匹配 a, bb, ccc 中任何一个;

示例:

● 忽略 CVS 目录以及以 .cmo 结尾的文件: ignore = Name {CVS,*.cmo}

● 忽略路径 a/b: ignore = Path a/b

● 忽略路径 */tmp。当 * 用在 Path 中,可以和以 "." 开始的文件、目录名匹配,如下面的和 ".foo/tmp" 匹配; ignore = Path */tmp

● 忽略 a/b/ 开始,结尾是 .ml 的文件。 ignore = Regex a/b/.*\.ml

 
 
生产环境要求忽略log及compiled 目录
在webA  webB 中 添加:
[root@localhost scripts]# vim /root/.unison/default.prf
 
# Unison preferences file
ignore = Name {log,compiled}
 
 
启动jwebA webB 脚本
[root@localhost scripts]# sh -x serA.sh
[root@localhost scripts]# sh -x serB.sh
a . webA上创建 9-27compiled 文件
[root@localhost zx]# ls
include  index.html  log
[root@localhost zx]# cd include/compiled/
[root@localhost compiled]# ls
[root@localhost compiled]# touch 9-27compiled
[root@localhost compiled]# ls
9-27compiled
 
webB上查询:  9-27compiled 缓存文件没有同步过来,验证成功
[root@localhost zx]# ls
include  index.html  log
[root@localhost zx]# cd include/compiled/
[root@localhost compiled]# ls
[root@localhost compiled]#
[root@localhost compiled]#
 
b. webA上创建log 日志文件
[root@localhost compiled]# cd ../../log/
[root@localhost log]# ls
[root@localhost log]# touch 9-27.log
[root@localhost log]# ls
9-27.log
 
webB上查询:  9-27.log 日志文件没有同步过来,验证成功
[root@localhost compiled]# cd ../../log/
[root@localhost log]# ls
[root@localhost log]#
 
 
c. webA zx目录 下创建{1..5}.txt
[root@localhost log]# cd ../
[root@localhost zx]# ls
include  index.html  log
[root@localhost zx]# touch {1..5}.txt
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  include  index.html  log
 
webB上查询: 验证成功
[root@localhost log]# cd ../
[root@localhost zx]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  include  index.html  log
 
 
d. webA 上创建log 日志  compiled 缓存文件
[root@localhost zx]# cd log/
[root@localhost log]# ls
[root@localhost log]# touch 9-28.log
[root@localhost log]# ls
9-28.log
[root@localhost log]# cd ../include/compiled/
[root@localhost compiled]# ls
[root@localhost compiled]# touch 9-28compiled
[root@localhost compiled]# ls
9-28compiled
 
webA上查询: 没有同步过来,验证成功
[root@localhost zx]# cd log/
[root@localhost log]# ls
9-27.log
[root@localhost log]# cd ../include/compiled/
[root@localhost compiled]# ls
9-27compiled
[root@localhost compiled]#
 
 
webA webB上脚本后台启动并添加到开机启动
[root@localhost scripts]# chmod +x serA.sh
[root@localhost scripts]# ll
总用量 8
-rwxr-xr-x. 1 root root 406 11月 30 13:48 serA.sh
[root@localhost scripts]# nohup ./serA.sh &
[root@localhost scripts]# echo "/root/scripts/serA.sh" >> /etc/rc.local
[root@localhost scripts]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
 
touch /var/lock/subsys/local
/root/scripts/serA.sh
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

unison+inotify 同步web代码并排除指定目录不同步的更多相关文章

  1. 两台linux主机使用unison + inotify实现web文件夹同步

    两台服务器同步数据 unison 是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支持通过SSH,RSH和Socket 等网络协议进行同步. unison 支持双向同步,你可以同A同步到B ,也 ...

  2. unison+inotify的Web目录同步方案

    1.在Linux下做WEB目录文件同步 一般有如下几种方式: ----------------------------------------------- 1)       nfs实现web数据共享 ...

  3. rsync 排除指定目录或文件进行同步

    很常见的情况:我想同步/myweb下的 一些php文件 ,  但是不想复制/myweb/log/里边的一些日志文件,因为这些文件太大了,备份也是没有意义的. 现在如果想避开某个路径  直接添加—exc ...

  4. linux大文件分包压缩和批量解压命令tar // tar 排除指定目录

    压缩命令主要用到tar打包和split分割,命令如下: tar czf - aaa | split -b 1024m - bbb.tar.gz_ aaa可以是文件也可以是目录, 大小可以是b.k.m这 ...

  5. ZIP、tar.gz压缩时排除指定目录

    1.ZIP 压缩时排除一个文件夹下所有内容zip -r sss.zip sss/ -x "sss/222/*" 压缩时排除指定多个文件夹下所有内容zip -r sss.zip ss ...

  6. rsync 排除指定目录

    背景 将Server1上的数据同步到Server2: Server1目录结构: /us_data/yahoo └── qlib ├── calendars ├── dataset_cache ├── ...

  7. bat中实现代码拷贝到指定目录后启动命令行并更改默认路径

      ### window书写shell脚本,实现判断指定文件是否存在,存在就删除,然后复制新文件到此目录 ``` if exist "G:\test\test2\1.txt" (d ...

  8. tar 排除指定目录 –exclude

    假设 test目录下有 1 2 3 4 5 这5个目录, 1下有6 7两个目录, 现在要将3 4 5 6目录tar打包,2和1下的6这两个目录不要.命令如下: Example[www]#cd test ...

  9. tar --exclude排除指定目录打包

    ----before-upgrade./ --exclude=apache-tomcat-/logs --exclude=apache-tomcat-/work 注意,--exclude后面跟路径名称 ...

随机推荐

  1. poj_1284_原根

    一開始看题的时候第一想法就是暴力,可是无奈数据量有点大,看了题解之后才知道原来牵扯到数论上的一个叫做原根的东西,这个题的题意就是,给你一个奇素数,问题他的原根有多少.依据初等数论上所说,此时牵扯到了三 ...

  2. Elasticsearch 理解

    概述 Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的. 特性 安装方便:没有其他依赖,下载 ...

  3. 使用OpenSSL生成CSR文件,并申请全球通用SSL证书

    http://www.openssl.org 上只有OpenSSL的原代码下载,为了方便Windows用户使用OpenSSL,我们特地为您准备了OpenSSL 0.9.8.a for win32的可执 ...

  4. HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)

    题目地址:pid=5407">HDU 5407 题意:CRB有n颗不同的糖果,如今他要吃掉k颗(0<=k<=n),问k取0~n的方案数的最小公倍数是多少. 思路:首先做这道 ...

  5. [1-4] 把时间当做朋友(李笑来)Chapter 4 【开拓我们的心智】 摘录

    1. 获得知识的基本途径  所有的人获取知识的最为基础的手段就是“体验”. 比“体验”再高级一点的获取知识的手段,就是“试错”(Trial and Error). 在“试错”这个手段的基础上,另外一个 ...

  6. jetty.xml解析

    我们知道jetty有一种启动方式是在jetty的根目录中运行命令行:java -jar start.jar,这个命令会调用apache的XmlConfiguration工具类作为启动类,这个类会默认读 ...

  7. php私有成员private的程序题目

    class base { private $member; function __construct() { echo __METHOD__ . "(begin)\n"; $thi ...

  8. 信号处理的好书Digital Signal Processing - A Practical Guide for Engineers and Scientists

    诚心给大家推荐一本讲信号处理的好书<Digital Signal Processing - A Practical Guide for Engineers and Scientists>[ ...

  9. C++语言基础(2)-new和delete操作符

    在C语言中,动态分配内存用 malloc() 函数,释放内存用 free() 函数.如下所示: ); //分配10个int型的内存空间 free(p); //释放内存 在C++中,这两个函数仍然可以使 ...

  10. UML类图详解_组合关系

    组合关系和聚合关系有一个最大的不同,组合关系中的整体直接掌握部件的生灭,聚合关系中的整体并不具有生灭部件的权力.一旦组合中的整体不存在时,其组合部件也不能单独存在,必须同时消灭.另外,外界也不能直接与 ...