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. docker集群——介绍Mesos+Zookeeper+Marathon的Docker管理平台

    容器为用户打开了一扇通往新世界的大门,真正进入这个容器的世界后,却发现新的生态系统如此庞大.在生产使用中,不论个人还是企业,都会提出更复杂的需求.这时,我们需要众多跨主机的容器协同工作,需要支持各种类 ...

  2. 算法(第四版)学习笔记之java实现可以动态调整数组大小的栈

    下压(LIFO)栈:可以动态调整数组大小的实现 import java.util.Iterator; public class ResizingArrayStack<Item> imple ...

  3. mac中yeoman构建你的项目

    一开始用在mac中构建一个项目就遇到一个很奇怪的事, 做好各种准备工作后,我开始创建一个angular01作为测试目录,结果运行yo脚手架之后,选择angular工作流进行构建项目,出来的结果我开始慌 ...

  4. Python闭包的高级应用-装饰器的实现

    我们先看一个闭包的例子: from time import ctime def before_call(f): def wrapped(*args, **kargs): print 'before c ...

  5. python——PEP8 Python 编码规范整理

    决定开始Python之路了,利用业余时间,争取更深入学习Python.编程语言不是艺术,而是工作或者说是工具,所以整理并遵循一套编码规范是十分必要的.所以今天下午我根据PEP 8整理了一份,以后都照此 ...

  6. linux命令之head、tail命令具体解释

    head 语法 样例 tail 语法 样例 head和tail组合使用方法举例 head 语法 head [-n -k ]... [FILE]... 样例 默认是显示开头前10行. head /etc ...

  7. 【Python3 爬虫】06_robots.txt查看网站爬取限制情况

    大多数网站都会定义robots.txt文件来限制爬虫爬去信息,我们在爬去网站之前可以使用robots.txt来查看的相关限制信息 例如: 我们以[CSDN博客]的限制信息为例子 在浏览器输入:http ...

  8. Commons-VFS 使用SFTP

    http://pro.ctlok.com/2011/06/apache-commons-vfs-for-sftp.html

  9. Spring事务的传播行为 @Transactional

    Spring事务的传播行为http://blog.csdn.net/cuker919/article/details/5957209 在service类前加上@Transactional,声明这个se ...

  10. linux查找yum和rpm安装路径

    linux查找yum和rpm安装路径 转:https://jingyan.baidu.com/article/86112f1378bf282737978730.html