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. vue2组件之异步组件...resolve

    看开源项目的时候看到这样的用法: 发现与之前定义组件的方式不一样,这个resolve又是什么? 原来这个是vue的异步组件实现,可以看这里:<异步组件> 异步组件的需求: 在大型应用中,我 ...

  2. EffectiveJava(25)泛型和数组的使用取舍及规范

    泛型和数组 泛型:1.泛型是不可变的.对于任意两个不同类型Type1,type2;List既不是List的子类型,也不是List的超类型 2.泛型是通过擦除来实现的.故泛型只在编译时强化它们的信息,并 ...

  3. 淘宝JAVA中间件Diamond

    以下是转载自网上资料,但是根据步骤可以搭建出diamond配置中心服务器. 项目中需要用到diamond的理由是, 项目中使用了很多定时任务和异步任务.而且这些定时任务和异步任务都是分布式的安排在多个 ...

  4. [转]2年SQL Server DBA调优方面总结

      2年SQL Server DBA调优方面总结 当2年dba 我觉得,有些东西需要和大家分享探讨,先书单. 书单 1.<深入解析SQL Server 2008 系列> 这个就是mssql ...

  5. 网站相关技术探究keepalive_timeout(转)

    网站相关技术探究keepalive设多少: /proc/$PID/fd/$number  0:标准输入 1:标准输出 2:标准错误   Test: [root@KTQT ~]# ll /proc/12 ...

  6. iOSeasy造成循引用的场景

    场景一 :NStimer timer就是一个能在从如今開始的未来的某一个时刻又或者周期性的运行我们指定的方法的对象 NSTimer运行的必要条件:相应线程的RunLoop要开启,mode要相应 以下看 ...

  7. C语言 指向结构体变量的指针

    一个指向变量的指针表示的是占内存中起始位置 一个指向结构体的变量的指针表示的是这个结构体变量占内存中的起始位置,同样它也可以指向结构体变量数组 定义结构体变量的指针: //假设已有一个结构体名为Stu ...

  8. Sql语句查询XML - 小结

    --两种方式查询 DECLARE @varXML XML, @varXML1 XML --.xml数据源为属性方式 SET @varXML = '<PARAM> <Row FID = ...

  9. nginx服务器部署

    nginx(“engine x”)是一个高性能的HTTP和反向代理服务器.   安装nginx Linux下  sudo apt-get install nginx windows下 下载 nginx ...

  10. CentOS环境下yum安装LAMP

    第一步:更新系统内核 yum -y update 如果执行失败报错,可以执行修复命令:rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY* 第二步: 安装Apahce.M ...