linux web服务器静态资源的处理 unison+inotify双向同步

linux web服务器静态资源的处理 unison+inotify双向同步php

http://monkeyzhu.blog.51cto.com/5764358/1324391html

 

简介java

unison可使两个本地磁盘目录保持内容一致,也能够支持网络数据同步。在内部实现上,unison使用OCaml语言开发,经过基于rsync算法对两边文件进行比较
unison是双向的,自动更新两边没有冲突的部分,冲突的部分须要人工解决(当两边同时对一个文件进行修改的时候),有冲突的部分会显示出来由用户选择更新策略
支持增量同步,每次同步完会记录文件状态,下次同步以上次记录状态为起点开始同步
unison有字符界面和GTK+图形界面
unison提供两种远程通讯方式
一、远程shell,由ssh工具完成,相似于rsync压缩传输协议
二、socket,要求发送tcp包进行通讯,数据传输不安全,不建议使用node

 

 

 

1、两台机器
192.168.11.11 vm1
192.168.11.12 vm2linux

 

注意:两边都要安装Unison!! c++

 

2、在vm1上编译安装Unisongit

Linux下经过源码包编译安装Unison时,须要用到Objective Caml compiler。
经过如下方式安装github

[root@vm1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
[root@vm1 ~]# tar -xzvf ocaml-3.12.1.tar.gz
[root@vm1 ~]# cd ocaml-3.12.1
[root@vm1 ~]# more INSTALL
[root@vm1 ocaml-3.12.1]# ./configure
[root@vm1 ocaml-3.12.1]# make world.opt
[root@vm1 ocaml-3.12.1]# make install

 

编译安装Unisonweb

[root@vm1 ~]# yum install -y ctags-etags
[root@vm1 ~]# wget http://pkgs.fedoraproject.org/repo/pkgs/unison240/unison-2.40.128.tar.gz/md5/aa4d9d425b2ee2f9676ac5331a154bac/unison-2.40.128.tar.gz
[root@vm1 ~]# tar -xzvf unison-2.40.128.tar.gz 
[root@vm1 ~]# cd unison-2.40.128
[root@vm1 unison-2.40.63]# make UISTYLE=text THREADS=true
[root@vm1 unison-2.40.63]# make install
[root@vm1 unison-2.40.63]# cp unison /usr/local/bin/
[root@vm1 unison-2.40.63]# scp unison  root@192.168.11.12:/usr/local/bin/  #将可执行文件unison上传到远程主机192.168.11.12

make install出现错误的缘由在与Unison默认是将文件Copy到/当前用户/bin目录下,但Linux默认是没有该目录的,所以咱们须要将生成的可执行文件unison复制到系统的PATH目录。算法

make install的时候会出错,默认会在当前目录下生成可执行文件unison,将其复制到系统的PATH目录

 

 

3、配置ssh key信任

自查

 

 

4、生成unison目录和文件

将本机的目录/data/和远端主机的/data/进行同步。通常的,须要两台机能ssh链接。
注意:在主机和目录间又多加了一个 "/"

两边都建立/data目录
mkdir /data

修改两台服务器的unison配置文件,输入如下内容
[unison@vm1 ~]$ vi /root/.unison/default.prf

root = /tmp/123
root = ssh://root@192.168.14.91//tmp/123
#path = www
#ignore = Path wp-content/tmp
ignore = Path wp-config.php  #忽略wp-config.php文件 至关于rsync的exclude
#nodeletion = /data
batch = true
maxthreads = 300
#repeat = 1
owner = true
group = true
perms = -1
fastcheck = false
sshargs = -C
xferbycopying = true
log = true
logfile = /tmp/unison.log


[root@steven 123]# ll
total 8
-rw-r--r-- 1 root root 3 Feb 11 11:44 232.txt
-rw-r--r-- 1 root root 3 Feb 11 11:44 wp-config.php

 

运行unison
[root@steven ~]# unison

 

而后就是测试工做了:

[root@vm2 ~]# cd /data/ ;echo 23323 >33.txt ; unison
[root@vm1 ~]# cd /data/; echo defd >22.txt ; unison


注意
一、若是要保持文件的源权限,须要使用能够chown的用户来同步,通常使用root用户来同步。
二、可能出现的问题是在unison同步的时候 ,若是提示输入密码,须要作ssh互信关系
三、若是是权限不对,注意用户的权限,还有是 lock文件 能够删除.unison/ara1da075598bd182a68b3563be920002b相似文件,再从新执行unison

# ls -lh
总用量 464K
-rw------- 1 root root 458K 2月   4 06:51 ar08f197646847353b45eb96cb656aedee
-rw------- 1 root root  263 2月   4 06:10 default.prf

 

 

5、安装Inotify-tools工具,实时触发unison进行同步
ll /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,能够输入命令:uname -a查看内核

安装inotify-tools

yum install make gcc gcc-c++
wget -c http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
cd /download
tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14 
./configure --prefix=/usr/local/inotify 
make 
make install

 


设置系统环境变量,添加软链接

echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh 
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify

 

vi /etc/sysctl.conf #添加如下代码
fs.inotify.max_user_instances=65535


建立脚本,实时触发rsync进行同步
vi /root/unison.sh

# use tools(linux inotify,unison)
# author:steven 2016-1-3
#!/bin/sh

srcdir=/data
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib,move $srcdir | while read file
do
unison 
echo " ${file} was rsynced" >> /tmp/unison.log 2>&1
done

 

 

设置脚本开机自动执行
vi /etc/rc.d/rc.local

/bin/sh /root/unison.sh &

 

 

 

注意:上面实验只是在vm1上安装inotify和配置unison配置文件,若是须要两边都inotify触发,那么须要vm2上也要配置default.prf和安装inotify

 

unison的弊端:没有rsync那样只同步新建立或者修改的文件,不一样步被删除的文件(-nodeletion选项),unison会同步被删除的文件,A机器删除了1.txt文件,也会删除B机器上1.txt文件

保持彻底同步

 

-nodeletion
nodeletion= /data/
unison
Fatal error: /data (given as argument to 'nodeletion' preference)
is not one of the current roots:
  /data/
  ssh://root@192.168.1.22//data/

-nodeletion无用,必定要是子目录才行,好比nodeletion= /data/tmp

 

 


文件同步工具Unison使用介绍 南非蚂蚁

http://ixdba.blog.51cto.com/2895551/526431

 

三种方式调用

第一种方式

unison profilename [options]

默认读取~/.unison 目录下的配置文件 profilename.prf

须要在配置文件profile里指定同步路径和同步参数

 

第二种方式

unison profile  root1 root2 [options]

root1 root2分别表示要同步的两个路径,若是是远程路径:ssh://username@remotehost//tmp/ixdab/files

这种方式不须要在profile.prf文件里指定root指令同步路径

 

第三种方式

unison root1 root2 [options]

这种方式至关于unison default root1 root2 [options],即读取default.prf

 

 

 

unison的使用

Unison能够在一台主机上使用,同步两个文件夹,也能够在网络上是用。
1:本地使用
使用方法:

#unison 111 222      #同步本地的111和222文件夹


Contacting server...
Looking for changes
Warning: No archive files were found for these roots. This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.
Update detection may take a while on this run if the replicas are
large.
Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.
Press return to continue.[<spc>] Reconciling changes
111 222
<---- file aaaaaaaaaaaaa [f] ?
Commands:
<ret> or f or <spc> follow unison's recommendation (if any)
I ignore this path permanently
E permanently ignore files with this extension
N permanently ignore paths ending with this name
m merge the versions
d show differences
x show details
l list all suggested changes
p or b go back to previous item
g proceed immediately to propagating changes
q exit unison without propagating any changes
/ skip
> or . propagate from left to right
< or , propagate from right to left

<---- file aaaaaaaaaaaaa [f] f

Proceed with propagating updates? [] y
Propagating updates

UNISON started propagating changes at 15:06:08 on 27 Aug 2007
[BGN] Copying aaaaaaaaaaaaa
from /222
to /111
[END] Copying aaaaaaaaaaaaa
UNISON finished propagating changes at 15:06:08 on 27 Aug 2007
Saving synchronizer state
Synchronization complete (1 item transferred, 0 skipped, 0 failures)
若是检测到两个文件夹有所不一样,unison会提示,让你选择相应的操做。如上例所示.
表示右边222的文件夹有新的文件,是否同步到左边的111文件夹,f表示force,而后将确认,进行更新,若是输入?会有更详细的介绍。


2: unison远程使用
使用方法:

# unison <本地目录> ssh://remotehostname(IP)/<远程目录的绝对路径>


# unison /home/AAA ssh://username@remotehostname(ip)//DB/path/BBB
表示将本机的目录/home/AAA和远端主机的/DB/path/BBB进行同步。通常的,须要两台机能ssh链接。
注意 在主机和目录间又多加了一个 "/"


unison参数说明
Unison有不少参数,这里只介绍常用的几个,详细的请参看unison手册:


1." -testserver
测试连通性,链接到服务器即退出。示例:
$ unison / ssh://opensou1@bluehost/ -servercmd=~/bin/unison -testserver
若是服务器端 unison 可执行文件不在默认目录下,甚至没有 unison 命令(须要你编译一个上传到服务器),则须要使用 -servercmd 参数告诉要执行的服务器 unison 命令位置。
使用 -testserver 参数,则成功连接即退出,也不会去执行目录的比较等后续操做。


2." -servercmd xxx
告诉 unison, 服务器端的 unison 命令是什么。参见上面的示例。


3." -auto
接受缺省的动做,而后等待用户确认是否执行。


4." -batch
batch mode, 全自动模式,接受缺省动做,并执行,无需人为干预。

 

5." -ignore xxx
同步时能够忽略的目录和路径,增长 xxx 到忽略列表中


6." -ignorecase [true|false|default]
是否忽略文件名大小写


7." -follow xxx
是否支持对符号链接指向内容的同步,会把软连接指向内容进行同步

8." -immutable xxx
指定不变化的目录,扫描时能够忽略该目录,不一样步该目录


9." -path xxx 参数
只同步 -path 参数指定的子目录以及文件,而非整个目录。-path 在配置中能够屡次出现,例如
unison /home/username ssh://remotehost//home/username \
-path shared \
-path pub \
-path .netscape/bookmarks.html

 

10.其余参数

owner = true (保持同步过来的文件属主)
group = true (保持同步过来的文件组信息)
perms = -1 (保持同步过来的文件读写权限)
repeat = 1 (间隔1秒后,开始新的一次同步检查)
retry = 3 (失败重试)
sshargs = -C (使用ssh的压缩传输方式)
xferbycopying = true  (这个是优化参数,默认true)
silent  (安静模式 除了错误,不打印任何信息)
times  (同步修改时间 同步mtime)
maxthreads n  (指定文件同步的最大线程数)
rsync  (默认是true,激活rsync传输模式)
log    (记录unison运行日志,默认true)
logfile   (日志输出到文件)

 


经过配置文件来使用unison
尽管能够彻底经过命令行的方式来指定unison运行所须要的参数,但咱们仍是推荐使用配置文件来进行配置使用unison

缘由很简单,看配置文件比看命令行容易理解,并且可管理性更强。


默认的配置文件夹位于~currentuser/.unison,即当前用户的home目录下,windows则位于C:\Documents and Settings\currentuser\.unison

默认的配置文件名是default.prf

Archive文件也能够有多个,这个文件记录每次完成同步后每一个文件的状态,能够在下次更新动做中更快判断文件是否应该更新,减小扫描时间。

 


运行这样的命令:
#unison exitgogo
Unison将默认读取~currentuser/.unison/exitgogo.prf文件里的配置信息.个人配置信息在/root/.unison/exitgogo.prf

所以咱们能够根据上面参数的介绍,把全部的参数配置信息写入到一个.prf的文件中.
下面是个人一个web应用中两个文件夹同步的配置信息:
root = /sina/webdata
root = ssh://root@192.168.60.121//sina/webdata
#force =/sina/webdata
ignore = Path as/*
#prefer = ssh://root@192.168.60.121//sina/webdata
batch = true
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck=false
rsync =false
#debug=verbose
sshargs = -C
xferbycopying = true
log = true
logfile = /root/.unison/sina_122.1547.log
说明以下:
两个root表示须要同步的文件夹
force表示以本地的/var/www/bbsnew文件夹为标准,将该目录同步到远端。注意,若是指定了force参数,那么unison就变成了单项同步了,也就是说会以force指定的文件夹为准进行同步.
Unison自己是能够双向同步的,可是要作到双向同步,就不要设置force参数,若是设置了force参数,就成了单项同步了,此时unison相似与sync.
Unison双向同步基本原理是:假若有A B两个文件夹,A文件夹把本身的改动同步到B,B文件夹也把本身的改动同步到A,最后A B两文件夹的内容相同,是 A B文件夹的合集.
Unison双向同步的一个缺点是,对于一个文件在两个同步文件夹中都被修改时,unison是不会去同步的,由于unison没法判断以那个为准.
ignore = Path表示忽略/sina/webdata下面的WEB-INF/tmp目录,即同步时不一样步它。
batch = true,表示全自动模式,接受缺省动做,并执行
-fastcheck true表示同步时使用文件的建立时间来比较两地文件,若是这个选项为false,unison则将比较两地文件的内容.建议设置为true
log = true表示在终端输出运行信息。
logfile则指定了同时将输出写入log文件。

 

unison FAQ

如何在和远程服务器同步大量数据,上传一部分数据后,超时:
9% 559:15 ETARead from remote host bluehost: Connection reset by peer
Fatal error: Lost connection with the server
实际操做中,最好的方法是,第一次先把要上传的文件打成包,用 ftp 上传,而后展开到服务器,以后执行一次 unison 同步便可。

 


  

yum install -y ctags-etags

Linux就这个范儿 P268

查找函数定义须要tags文件帮忙,它是由ctags程序产生的一个索引文件。郑重声明:ctags和cscope是成就vim IDE的两个法宝

ctags -R

 

网上多篇文章都会出现没有安装ctags-etags的错误,估计ctags-etags是用来生成一个文件索引文件,发现差别查找这个索引文件
出现错误:
which: no etags in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/latest/bin:/usr/local/tomcat/bin:/root/bin)

/bin/sh: line 1: etags:command not found

make[1]: [tags] Error 127(ignored)

make[1]: Leavingdirectory `/unison/unison-2.48.3′

解决办法:

# yum install ctags-etags


(make UISTYLE=text 报错时,须要建立/root/bin/文件夹后再运行make install。mkdir /root/bin/;make install;cp unison /root/bin/;cp unison /usr/local/bin/

报etags错误能够忽略或者安装etags:yum -y install ctags-etags)

 

在执行 make UISTYLE=text是可能会出现如下报错。是由于没有etags命令
可使用 yum install ctags-etags解决。

 

 

F

相关文章
相关标签/搜索