您的位置:首页 > 运维架构 > Linux

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

2014-07-31 11:30 671 查看
最近研究高可用的方案,对于前端负载不大,所以采用了较为简单的 keepalived+nginx的方案。
在处理静态内容的时候发现两台web上的静态资源需要一致,于是研究使用了下unison,由于unison是需要触发点来触发的,便想到了使用 inotify来处理

过程如下:
一、两台机器
192.168.180.181 yun1
192.168.180.182 yun2

inotify 原理链接
http://www.infoq.com/cn/articles/inotify-linux-file-system-event-monitoring

建议完全阅读。

Unison简介
Unison是Windows、Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致。Unison拥有与其它一些同步工具或文件系统的相同的特性,但也有自身的特点:
1.跨平台使用;
2.对内核和用户权限没有特别要求;
3.Unison是双向的,它能自动处理两分拷贝中更新没有冲突的部分,有冲突的部分将会显示出来让用户选择更新策略;
4.只要是能连通的两台主机,就可以运行unison,可以直接使用socket连接或安全的ssh连接方式,对带宽的要求不高,使用类似rsync的压缩传输协议。
unison 官网:
http://www.cis.upenn.edu/~bcpierce/unison/

二、编译安装Unison
yum install gcc
Linux下通过源码包编译安装Unison时,需要用到Objective Caml compiler。
通过以下方式安装
[root@yun1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-4.01/ocaml-4.01.0.tar.gz [root@yun1 ~]# tar -xzvf ocaml-4.01.0.tar.gz
[root@yun1 ~]# cd ocaml-4.01.0
[root@yun1 ocaml-4.01.0]# ./configure
[root@yun1 ocaml-4.01.0]# make world.opt
[root@yun1 ocaml-4.01.0]# make install

编译安装Unison
[root@yun1 ~]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.102.tar.gz [root@yun1 ~]# tar -xzvf unison-2.40.102.tar.gz
[root@yun1 ~]# cd unison-2.40.102
[root@yun1 unison-2.40.102]# make UISTYLE=text
[root@yun1 unison-2.40.102]# make install

在执行 make UISTYLE=text是可能会出现以下报错是因为没有etags命令
可以使用 yum install ctags-etags后执行make tags解决。
make[1]: Entering directory `/root/work/unison-2.40.102'
if [ -f `which etags` ]; then \
etags *.mli */*.mli *.ml */*.ml */*.m *.c */*.c *.txt \
; fi
which: no etags in (/usr/local/rvm/gems/ruby-1.9.2-p320@web1/bin:/usr/local/rvm/gems/ruby-1.9.2-p320@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p320/bin:/usr/local/rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/opt/nginx/sbin:/opt/keepalived/sbin:/usr/local/bin:/opt/nagios/bin:/opt/nagios/sbin:/opt/nagios/libexec:/home/dog/.rvm/bin:/home/dog/bin)
/bin/sh: line 1: etags: command not found
make[1]: [tags] Error 127 (ignored)
make[1]: Leaving directory `/root/work/unison-2.40.102'

在执行make install的过程中,可能会出现以下错误提示:
mv: cannot stat '/root/bin//unison': No such file ordirectory
make: [doinstall] Error 1 (ignored)
cp unison /root/bin/
cp: cannot create regular file '/root/bin/': Is a directory
make: *** [doinstall] Error 1

出现错误的原因在与Unison默认是将文件Copy到/root/bin目录,但Linux默认是没有该目录的,因此我们需要将生成的可执行文件unison复制到系统的PATH目录。'''

[root@yun1 unison-2.40.102]# cp unison /usr/local/bin
将可执行文件unison上传到远程主机192.168.180.182
[root@yun1 unison-2.40.63]# scp unison root@192.168.180.182:/root/
通过SSH登陆到远程主机,再将unison复制到yun2的PATH目录
[root@yun2 ~]#cp unison /usr/local/bin

三、配置ssh key信任

建议通过普通用户进行操作,理由是通过root操作本身就危险,免密码登陆的root就更危险了。
在两台服务器上创建syncuser用户
[root@yun1 ~]# useradd syncuser
[root@yun1 ~]# passwd syncuser
[root@yun2 ~]# useradd syncuser
[root@yun2 ~]# passwd syncuser
在yun1上创建key并配置yun2的信任
[root@yun1 ~]# su – syncuser
[syncuser@yun1 ~]$ ssh-keygen -t rsa
在提示保存私钥(key)和公钥(public key)的位置时,使用默认值;
在提示是否需要私钥密码(passphrase)时,直接敲回车,即不使用私钥密码。
之后,将生成一对密钥,id_rsa(私钥文件)和id_rsa.pub(公钥文件),保存在/home/syncuser/.ssh/目录下。
将公钥添加到yun2的 authorized_keys 文件中
将文件上传到yun2
[syncuser@yun1 ~]$ scp ~/.ssh/id_rsa.pub unison@192.168.180.182:/home/syncuser/
使用rsync用户SSH到登陆到远程主机,并将公钥添加到 authorized_keys 文件中
[syncuser@yun2 ~]$ mkdir .ssh
[syncuser@yun2 ~]$ chmod 700 .ssh
[syncuser@yun2 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys
同理,执行以下步骤在yun2上创建key并配置yun1的信任
[root@yun2 ~]# su – unison
[syncuser@yun2 ~]$ ssh-keygen -t rsa
将文件上传到yun1(假设yun1主机IP为192.168.10.3)
[syncuser@yun2 ~]$ scp ~/.ssh/id_rsa.pub unison@192.168.180.181:/home/syncuser/
使用rsync用户SSH到登陆到yun1,并将公钥添加到 authorized_keys 文件中
[syncuser@yun1 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys
重启SSH服务
[root@yun1 ~]# /etc/init.d/sshd restart
[root@yun2 ~]# /etc/init.d/sshd restart
最后别忘了测一把,登陆yun1的syncuser,ssh syncuser@yun2,如果没错的话会立即登陆不用输入密码

四、生成unison目录和文件
在两台服务器上分别执行一次unison,如果出现提示确认,则直接敲回车选择默认值

[root@yun1 ~]# unison /home/syncuser/demo ssh://syncuser@192.168.180.182//home/syncuser/demo
[root@yun2 ~]# unison /home/syncuser/demo ssh://syncuser@192.168.180.181//home/syncuser/demo
说明:
将本机的目录/home/syncuser/demo和远端主机的/home/syncuser/demo进行同步。一般的,需要两台机能ssh连接。
注意
在主机和目录间又多加了一个 "/"

修改两台服务器的unison配置文件,输入以下内容
[syncuser@yun1 ~]$ vim ~/.unison/default.prf

root = /home/syncuser/demo
root = ssh://root@192.168.180.182//home/syncuser/demo
batch = true
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /home/syncuser/.unison/unison.log

[syncuser@yun2 ~]$ vim ~/.unison/default.prf
root = /home/syncuser/demo
root = ssh://root@192.168.180.181//home/syncuser/demo
batch = true
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /home/syncuser/.unison/unison.log

相关注解如下:
force表示会以本地所指定文件夹为标准,将该目录同步到远端。这里需要注意,如果指定了force参数,那么Unison就变成了单项同步了,也就是说会以force指定的文件夹为准进行同步,类似与rsync。
Unison双向同步基本原理是:假如有A B两个文件夹,A文件夹把自己的改动同步到B,B文件夹也把自己的改动同步到A,最后AB两文件夹的内容相同,是AB文件夹的合集。
Unison双向同步的一个缺点是,对于一个文件在两个同步文件夹中都被修改时,unison是不会去同步的,因为unison无法判断以那个为准。
ignore = Path表示忽略指定目录,即同步时不同步它。
batch = true,表示全自动模式,接受缺省动作,并执行。
-fastcheck true表示同步时仅通过文件的创建时间来比较,如果选项为false,Unison则将比较两地文件的内容。
log = true 表示在终端输出运行信息。
logfile 指定输出的log文件。

另外,Unison有很多参数,这里仅介绍常用的几个,详细的请参看Unison手册。
-auto //接受缺省的动作,然后等待用户确认是否执行。
-batch //batch mode, 全自动模式,接受缺省动作,并执行。
-ignore xxx //增加 xxx 到忽略列表中
-ignorecase [true|false|default] //是否忽略文件名大小写
-follow xxx //是否支持对符号连接指向内容的同步
owner = true //保持同步过来的文件属主
group = true //保持同步过来的文件组信息
perms = -1 //保持同步过来的文件读写权限
repeat = 1 //间隔1秒后,开始新的一次同步检查
retry = 3 //失败重试
sshargs = -C //使用ssh的压缩传输方式
xferbycopying = true"
-immutable xxx //不变目录,扫描时可以忽略
-silent //安静模式
-times //同步修改时间
-path xxx 参数 //只同步 -path 参数指定的子目录以及文件,而非整个目录,-path 可以多次出现。

需要注意的是如果要保持文件的源权限,需要使用chown可以修改的用户同步。

然后就是测试工作了:
[root@yun2 ~]# cd /home/syncuser; touch monkey.txt
[root@yun1 ~]# unison ; cd /home/syncuser; ls

可能出现的问题是在unison同步的时候 ,如果提示输入密码,需要做信任关系,如果是权限不对,注意用户的权限,还有是 lock文件 可以删除.unison/ara1da075598bd182a68b3563be920002b类似文件,再重新执行unison
注:
1、如果想实现多个目录同步,可以多新建写prf文件,例如opt.prf,运行unison opt即可
2、如果用的是非用户目录,需要切换至root给到相应权限

五、自动化整个过程

当目录下有改变发生时自动执行unison同步,
通知配置文件的改变

跟踪某些关键的系统文件的变化

监控某个分区磁盘的整体使用情况

系统崩溃时进行自动清理

自动触发备份进程

向服务器上传文件结束时发出通知

以上为inotify截取。
具体原理查看http://www.infoq.com/cn/articles/inotify-linux-file-system-event-monitoring

inotify工具安装:
wget 'http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz'
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make && make install
安装完成执行下inotifywait提示 No files specified to watch!就ok了

编写脚本,如下
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move /home/syncuser/demo | while read line
do
unison
echo "`date `sync successful" >> /home/syncuser/sync.log
done

当/home/syncuser目录下的文件发生create,delete,modify,move操作时会执行while内语句。结束任务时kill该进程。

执行脚本后,到目录下执行touch操作,查看另一台文件夹的内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux Windows web服务器