实现web数据同步的四种方式
=======================================
nfs实现web数据共享
rsync +inotify实现web数据同步
rsync+sersync更快更节约资源实现web数据同步
unison+inotify实现web数据双向同步
=======================================
一、nfs实现web数据共享

nfs能实现数据同步是通过NAS(网络附加存储),在服务器上共享一个文件,且服务器需要设置文件系统的权限和配置文件设置的权限,权限两者之间取交集,然后客户端把共享的文件挂载到本地,客户端对文件有读写权限,则实现数据的同步。
nfs+web:服务器端的配置:
1)、安装相关软件,httpd提供web服务,nfs-utils提供nfs服务
2)、设置web的相关配置,使得web能够提供web服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@jie1 ~]
ServerName 172.16.22.1:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /web/htdocs
< /VirtualHost >
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 htdocs]
[root@jie1 htdocs]
index.html test .html test .php
[root@jie1 htdocs]
[root@jie1 htdocs]
Syntax OK
[root@jie1 htdocs]
Starting httpd: [ OK ]
|
3)、设置nfs的相关配置,共享网页文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@jie1 htdocs]
uid=48(apache) gid=48(apache) groups =48(apache)
[root@jie1 htdocs]
/web/htdocs 172.16.22.3(rw, sync ,root_squash,anonuid=48,anongid=48)
[root@jie1 htdocs]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Stopping RPC idmapd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ]
[root@jie1 htdocs]
|
web:客户端的配置
1)、安装httpd的软件
2)、设置web的相关配置,使得web能够提供web服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@jie3 /]
ServerName 172.16.22.3:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /website
< /VirtualHost >
[root@jie3 /]
[root@jie3 /]
Syntax OK
[root@jie3 /]
Starting httpd: [ OK ]
[root@jie3 ~]
[root@jie3 website]
[root@jie3 website]
|
实现同步:
1)服务器端设置apache用户对共享的文件有读写权限
2)客户端挂载服务器的共享文件,查看客户端是否已经同步服务器端的文件
1
2
3
4
5
6
7
|
[root@jie3 website]
[root@jie3 ~]
[root@jie3 /]
[root@jie3 ~]
[root@jie3 website]
index.html test .html test .php
[root@jie3 website]
|
3)客户端在共享的文件中新增文件,查看服务器端是否同步文件
1
2
3
4
5
6
|
[root@jie3 ~]
[root@jie3 website]
index.html test .html test .php
[root@jie3 website]
[root@jie3 website]
index.html test .html test .php website.html
|
1
2
|
[root@jie1 htdocs]
index.html test .html test .php website.html
|
所有的数据其实都保存到了nfs服务器,不论用户访问哪台Web服务器,都要来nfs服务器获取数据,这样势必照成nfs服务器的性能下降,而且客户端对nfs服务器的依赖性较大,如果nfs服务器down掉之后,客户端的web服务器就无法工作了。(动态的那种数据,而且数据量很大的数据,就不要用nfs服务器来实现数据共享了,一般适应于,静态页面和数据较小的文件)
二、rsync +inotify实现web数据同步

rsync(remote sync)的特性:
可以镜像保存整个目录树和文件系统
可以同步增量同步数据,文件传输效率高,因而同步时间很短
可以保持原有文件的权限、时间等属性
加密传输数据,保证了数据的安全性
支持匿名传输
rsync也能实现同步,但是需要自己手动的去同步数据,当数据量非常的频繁时,无疑是加大了运维人员的工作,inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,inotify-tools工具的出现,解决了这种工作,安装inotify软件的主机会监听服务器端的主机是否数据和本机不一样,(因为在上传数据时,运维人员先上传到安装inotify主机上),不一样就用rsync命令直接把数据传输过去。客户端安装rsync软件是为了调用rsync的命令,安装inotify软件是监听和数据是否发生改变,服务器端安装rsync软件时为了提供rsync服务。
rsync+web服务端的配置:
1)、安装相关软件
2)、web的相关配置,使得web能够提供服务
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@jie1 ~]
ServerName 172.16.22.1:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /web/htdocs
< /VirtualHost >
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
|
3)、rsync服务的相关配置
*****建立rsync的配置文件和密码文件************
touch /etc/rsyncd.conf(rsync的配置文件)
touch /etc/rsyncd.pwd(用户的密码文件)
chmod 600 /etc/rsyncd.pwd(权限要设置为600,否则无法备份成功)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@jie1 ~]
uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file = /var/run/rsyncd .pid
log file = /var/log/rsyncd .log
[htdocs]
path = /web/htdocs
ignore errors = yes
read only = no
write only = no
hosts allow = 172.16.22.3
hosts deny = *
uid = root
gid = root
auth users = backuper
secrets file = /etc/rsyncd . pwd
[root@jie1 ~]
backuper:pwd123
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
Starting xinetd: [ OK ]
[root@jie1 ~]
tcp 0 0 :::873 :::* LISTEN 19876 /xinetd
|
rsync+inotify+web客户端的配置:
1)、inotify-tools软件的安装及设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@jie3 ~]
[root@jie3 ~]
anaconda-ks.cfg install .log
inotify-tools-3.14. tar .gz install .log.syslog
[root@jie3 ~]
[root@jie3 ~]
[root@jie3 inotify-tools-3.14]
[root@jie3 ~]
[root@jie3 inotify]
[root@jie3 inotify]
[root@jie3 inotify]
[root@jie3 inotify]
/usr/local/inotify/lib :
libinotifytools.so.0 -> libinotifytools.so.0.4.1
[root@jie3 inotify]
` /usr/include/inotify ' -> `/usr/local/inotify/include/'
[root@jie3 inotify]
|
2)、web的相关配置,使得web能够提供服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@jie3 /]
ServerName 172.16.22.3:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /website
< /VirtualHost >
[root@jie3 /]
[root@jie3 /]
Syntax OK
[root@jie3 /]
Starting httpd: [ OK ]
[root@jie3 ~]
[root@jie3 website]
[root@jie3 website]
[root@jie3 ~]
|
3)、配置能连接rsync的密码文件和传输数据的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@jie3 ~]
pwd123
[root@jie3 ~]
[root@jie3 ~]
host=172.16.22.1
src= /website
des=htdocs
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' -- format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --progress --password- file = /etc/rsyncd .secrets $src backuper@$host::$des
echo "${files} was rsynced" >> /tmp/rsync .log 2>&1
done
|
验证实现同步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@jie3 ~]
[root@jie3 ~]
[root@jie3 website]
[root@jie3 website]
index.html testdb.php test .php inotify.php
[root@jie3 website]
[root@jie1 ~]
[root@jie1 htdocs]
index.html testdb.php test .php inotify.php
[root@jie1 htdocs]
|
rsync +inotify这种能实现数据的同步,但是当网络很繁忙,且文件变化比较频繁时,而且需要同步的rsync服务器端比较多时,rsync+inotify肯定是满足不了需求的,于是rsync+sersync这种更快更节约资源实现web数据同步可以弥补rsync+inotify带来的不足,rsync+inotify还有一个重大的缺点就是数据传输只是单向的,当运维人员由于“粗心”把数据直接传输rsync服务器端时,inotify主机是得不到rsync服务器端的数据,于是unison+inotify实现web数据双向同步,解决了rsync+inotify的这一缺点。
三、rsync+sersync更快更节约资源实现web数据同步

sersync与inotify相比有以下优点:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
sersync自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
rsync+web服务器端的配置:
1)、安装相关软件
2)、web的相关配置,使得web能够提供服务
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@jie1 ~]
ServerName 172.16.22.1:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /web/htdocs
< /VirtualHost >
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
|
3)、rsync服务的相关配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@jie1 ~]
uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file = /var/run/rsyncd .pid
log file = /var/log/rsyncd .log
[htdocs]
path = /web/htdocs
ignore errors = yes
readonly = no
write only = no
hosts allow = 172.16.22.3
hosts deny = *
list = false
uid = root
gid = root
auth users = backuper
secrets file = /etc/rsyncd . pwd
[root@jie1 ~]
backuper:pwd123
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
Starting xinetd: [ OK ]
[root@jie1 ~]
tcp 0 0 :::873 :::* LISTEN 19876 /xinetd
|
sersync+web客户端的配置:
1)、先下载安装sersync软件,做初始设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@jie3 ~]
[root@jie3 ~]
anaconda-ks.cfg install .log.syslog
install .log sersync2.5_64bit_binary_stable_final. tar .gz
mkdir /usr/local/sersync
[root@jie3 ~]
mkdir : created directory ` /usr/local/sersync '
mkdir : created directory ` /usr/local/sersync/conf '
mkdir : created directory ` /usr/local/sersync/bin '
mkdir : created directory ` /usr/local/sersync/log '
[root@jie3 ~]
[root@jie3 ~]
[root@jie3 GNU-Linux-x86]
confxml.xml sersync2
[root@jie3 GNU-Linux-x86]
[root@jie3 GNU-Linux-x86]
[root@jie3 GNU-Linux-x86]
[root@jie3 sersync]
[root@jie3 sersync]
[root@jie3 sersync]
[root@jie3 sersync]
|
2)、修改sersync的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[root@jie3 sersync]
< head version= "2.5" >
<host hostip= "172.16.22.3" port= "8008" >< /host >
<debug start= "false" />
<fileSystem xfs= "false" />
<filter start= "false" >
<exclude expression= "(.*)\.svn" >< /exclude >
<exclude expression= "(.*)\.gz" >< /exclude >
<exclude expression= "^info/*" >< /exclude >
<exclude expression= "^static/*" >< /exclude >
< /filter >
<inotify>
<delete start= "true" />
<createFolder start= "true" />
<createFile start= "false" />
<closeWrite start= "true" />
<moveFrom start= "true" />
<moveTo start= "true" />
<attrib start= "false" />
<modify start= "false" />
< /inotify >
<sersync>
<localpath watch = "/website" >
<remote ip= "172.16.22.1" name= "htdocs" />
<!--<remote ip= "192.168.8.39" name= "tongbu" />-->
<!--<remote ip= "192.168.8.40" name= "tongbu" />-->
< /localpath >
< rsync >
<commonParams params= "-artuz" />
<auth start= "true" users = "backuper" passwordfile= "/usr/local/sersync/sersync.pwd" />
<userDefinedPort start= "false" port= "874" /><!-- port=874 -->
<timeout start= "true" time = "100" /><!-- timeout=100 -->
< ssh start= "false" />
< /rsync >
<failLog path= "/tmp/rsync_fail_log.sh" timeToExecute= "60" /><!--default every 60mins execute once-->
< crontab start= "false" schedule= "600" ><!--600mins-->
<crontabfilter start= "false" >
<exclude expression= "*.php" >< /exclude >
<exclude expression= "info/*" >< /exclude >
< /crontabfilter >
< /crontab >
<plugin start= "false" name= "command" />
< /sersync >
<plugin name= "command" >
<param prefix= "/bin/sh" suffix= "" ignoreError= "true" /> <!--prefix /opt/tongbu/mmm .sh suffix-->
<filter start= "false" >
<include expression= "(.*)\.php" />
<include expression= "(.*)\.sh" />
< /filter >
< /plugin >
< /head >
|
验证实现同步:
1
2
3
4
5
6
7
8
9
|
[root@jie3 website]
[root@jie3 ~]
[root@jie3 website]
[root@jie1 ~]
[root@jie1 htdocs]
index.html testdb.php test .html test .php
[root@jie1 htdocs]
|
四、unison+inotify实现web数据双向同步

Unison是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支撑经由过程SSH、RSH和Socket等收集和谈进行同步。
Unison支撑双向同步操纵,你既可以从A同步到B,也可以从B同步到A,这些都不须要额外的设定。
1)、两个服务器都编译安装这三个源码包:(在此我只写一台服务器的编译安装过程)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
[root@jie1 ~]
[root@jie1~]
[root@jie1~]
[root@jie1 ~]
anaconda-ks.cfg install .log ocaml-3.10.2. tar .gz
inotify-tools-3.14. tar .gz install .log.syslog unison-2.32.52. tar .gz
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 inotify-tools-3.14]
[root@jie1 inotify-tools-3.14]
[root@jie1 inotify]
[root@jie1 inotify]
[root@jie1 inotify]
[root@jie1 inotify]
/usr/local/inotify/lib :
libinotifytools.so.0 -> libinotifytools.so.0.4.1
[root@jie1 inotify]
` /usr/include/inotify ' -> `/usr/local/inotify/include/'
[root@jie1 inotify]
[root@jie1 ocaml-3.10.2]
[root@jie1 ocaml-3.10.2]
[root@jie1 ocaml-3.10.2]
[root@jie1 ocaml-3.10.2]
[root@jie1 unison-2.32.52]
[root@jie1 unison-2.32.52]
[root@jie1 unison-2.32.52]
[root@jie1 unison-2.32.52]
|
2)、服务器A生成的公钥传到服务器B上:
1
2
3
4
5
6
7
8
9
|
[root@jie1 ~]
[root@jie1 ~]
[root@jie3 ~]
[root@jie3 ~]
[root@jie1 ~]
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@jie1 ~]
|
3)、服务器B生成的公钥传到服务器A上:
1
2
3
4
5
6
7
8
9
|
[root@jie3 ~]
[root@jie3 ~]
[root@jie1 ~]
[root@jie1 ~]
[root@jie3 ~]
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@jie3 ~]
|
4)、分别搭建web服务,服务器A的网页文件存放路径为/web/htdocs,服务器B的网页存放路径为/website
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@jie1 /]
ServerName 172.16.22.1:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /web/htdocs
< /VirtualHost >
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 htdocs]
[root@jie1 htdocs]
[root@jie3 /]
ServerName 172.16.22.3:80
<VirtualHost *:80>
ServerName www.jie.com
DocumentRoot /website
< /VirtualHost >
[root@jie3 /]
[root@jie3 /]
Syntax OK
[root@jie3 /]
Starting httpd: [ OK ]
[root@jie3 ~]
[root@jie3 website]
[root@jie3 website]
|
5)、编unison同步的脚本进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[root@jie1 ~]
ipB= "172.16.22.3"
srcA= "/web/htdocs"
dstB= "/website"
/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
[root@jie3 ~]
ipA= "172.16.22.1"
srcB= "/website"
dstA= "/web/htdocs"
/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
[root@jie1 ~]
[root@jie1 ~]
[root@jie1 htdocs]
[root@jie1 htdocs]
SerA.html SerA.php serA.txt SerB.html SerB.php SerB.txt
[root@jie3 ~]
[root@jie3 ~]
[root@jie3 website]
[root@jie3 website]
SerA.html SerA.php serA.txt SerB.html SerB.php SerB.txt
|
本文转自 jie783213507 51CTO博客,原文链接:http://blog.51cto.com/litaotao/1286871,如需转载请自行联系原作者