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. 如何让Firefox清除它记下的网站登录名和密码

  2. 倍福TwinCAT(贝福Beckhoff)应用教程11.1 TwinCAT应用小程序1 贝福IO模块介绍

    EL1002,EL1004,EL1008都是数字输入模块(2个点,4个点,8个点),输入高的范围是15V到30V,低的范围是-3V到5V         EL2002,EL2004,EL2008都是数 ...

  3. SEO优化100条

    1.准备个好域名.①.尽量在5位数内,当然也不一定,反正要让用户好记.(看个人):②.尽量用顶级的域名,搜索排名感觉好一点.③.做中文站最好用拼音注册,不要问为什么.看百度(baidu.com)就是很 ...

  4. Python——@property属性描述符

    @property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的 假设定义了一个类Cls,该类必须继承自object类,有一私 ...

  5. 取给定正整数的指定bit位開始的指定长度的数据

    接口说明 原型: unsigned int GetBitsValue(unsigned int input, unsigned int startbit, unsigned int bitlen) 输 ...

  6. java的IO操作:字节流与字符流操作

    流的概念 程序中的输入输出都是以流形式,流中保存的实际上都是字节文件. 字节流与字符流 字节流的操作: 1)输入:inputStream, 2)输出:outPutStream; 字符流的操作: 1)输 ...

  7. 程序员的笔记工具——Markdown

    安利一款轻量级的标记语言——Markdown 如上,Markdown作为一门语言,十分钟即可入门,熟练以后麻麻再也不用担心我在文章排版上浪费时间了!! 入门资源可参考如下回答(建议拣一条完整的看完上手 ...

  8. 【MyBatis学习03】原始dao开发方法及其弊端

    上一篇博文总结了一下mybatis的入门,接下来就要开发dao方法了,这篇博文主要总结一下mybatis中原始dao开发的方法,最后并总结一下原始dao开发方法的弊端.mybatis中dao开发应该使 ...

  9. STL vector的构造函数和析构函数(2)

    原文来自:点击打开链接 译文: public member function vector的构造器:这里我仅仅翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> s ...

  10. C# Enum,Int,String,之间及bool与int之间的转换

    枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值. Enum 提供一些实用的静态方法: (1)比较枚举类的实例的方法 (2)将实例的值转换为其字符串表示形式的方法 (3)将数字的 ...