|
利用Sersync+rsync来做服务器之间的数据同步
一:为什么要用Sersync+rsync这种方式去做数据同步呢?
现在业内一般都如下几种解决方案: rsync+inotify-tools,Openduckbill+inotify-tools和rsync+sersync。
就拿rsync+inotify-tools和rsync+sersync作为比较吧。
1.rsync+inotify-tools方案中,inotify-tools只能记录下被监听的目录发生了增加,删除,修改等的变化,并不知道哪个文件或者哪个目录发生了变化。在这样的情况下rsync在同步的时候,并不知道哪些文件,及目录发生了变化,于是同步都是对整个目录进行同步,如果数据量特别大的时候,这样的方法同步就会非常消耗资源。
2.rsync+sersync的方案中,sersync可以监听到某一个文件,及目录下发生变化的文件或者目录。这样rsync在进行同步的时候,就只同步发生变化的文件及目录。而且sersync在其监听的时候,会对linux系统文中系统产生的临时文件和重复的文件操作能够进行过滤处理,主要用于服务器同步,web镜像等功能。
二:需求环境准备
1
2
3
4
| 环境:三台主机 192.168.100.31 192.168.100.32 192.168.100.63
操作系统:centos
推送端:sersync 192.168.100.63
接收端:rsync 192.168.100.31 192.168.100.32
|
需求:将利用sersync+rsync进行两台服务器之间的数据实时同步,将192.168.100.1中/data/msg下的数据实时同步到192.168.100.31以及192.168.100.32中。
需求分析:在使用sersync+rsync进行实时同步时,用于推送端的服务器上跑sersync,接受文件端服务器上跑rsync守护进程。重点:当然sersync也是利用rsync命令将本服务器上的文件传输推送到接收端中,所以推送端192.1683.100.63上也需要安装rsync服务。
三:安装配置
1.先来安装配置推送端sersync
安装rsync:
1
2
| [iyunv@h63 ~]# yum install rsync -y
[iyunv@h63 ~]# yum install xinetd -y
|
启动rsync依赖服务:
1
2
| [iyunv@h63 ~]# /etc/init.d/xinetd start
[iyunv@h63 ~]# chkconfig xinetd on
|
2:安装配置sersync
创建sersync目录结构
1
2
3
4
5
6
7
8
| [iyunv@h63 ~]# mkdir /usr/local/sersync
[iyunv@h63 ~]# mkdir /usr/local/sersync/conf
[iyunv@h63 ~]# mkdir /usr/local/sersync/bin
[iyunv@h63 ~]# mkdir /usr/local/sersync/log
[iyunv@h63 ~]# tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
[iyunv@h63 ~]# cd GNU-Linux-x86/
[iyunv@h63 ~]# cp confxml.xml /usr/local/sersync/conf
[iyunv@h63 ~]# cp sersync2 /usr/local/sersync/bin
|
配置 sersync
首先需要创建连接接收端对方rsync的密码文件
1
2
| [iyunv@h63 ~]# echo "mimalo" >/etc/rsync.pas
[iyunv@h63 ~]# chmod 600 /etc/rsync.pas
|
配置主配置文件confxml.xml
[iyunv@h63 ~]# vim /usr/local/sersync/conf/confxml.xml
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
| <?xml version="1.0" encoding="ISO-8859-1"?><head version="2.5">
<host hostip="192.168.100.63" port="8008"></host> #本地IP,监听port/
<debug start="true"/> #DUBUG模式开启/
<fileSystem xfs="true"/> #XFS文件系统开启/
<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="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/data/msg"> #设置本地需要同步的路径!/
<remote ip="192.168.100.31" name="web"/> #接收端的IP以及rsync连接的模块名/
<remote ip="192.168.100.30" name="web"/>
</localpath>
<rsync>
<commonParams params="-artuz"/> #rsync的指令参数,以实际需求调整/
<auth start="true" users="user" passwordfile="/etc/rsync.pas"/> #rsync同步认证/
<userDefinedPort start="false" port="873"/><!-- port=874 --> #rsync远程服务端口/
<timeout start="true" time="100"/><!-- timeout=100 --> #超时时间设置/
<ssh start="false"/> # 加密传输模式设置,默认关闭
</rsync>
<failLog
path="/usr/local/sersync/log/rsync_fail_log.sh"
timeToExecute="60"/><!--default every 60mins execute once-->
#传输失败log的路径,每60秒执行一次该脚本,执行一次自动清空/
<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>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
创建推送端sersync需要同步目录
|
1
| [iyunv@h63 ~]# mkdir /data/msg
|
设置环境变量:
1
2
| [iyunv@h63 ~]# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
[iyunv@h63 ~]# source /etc/profile
|
PS:全部配置完之后,先不要启动,让我们先来把接收端rsync配置好,在开启程序
3:安装配置接受端192.168.100.31,192.168.100.32
安装rsync:
1
2
| [iyunv@h63 ~]# yum install rsync -y
[iyunv@h63 ~]# yum install xinetd -y
|
启动rsync依赖服务:
1
2
| [iyunv@h63 ~]# /etc/init.d/xinetd start
[iyunv@h63 ~]# chkconfig xinetd on
|
修改配置文件 rsyncd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| [iyunv@h63 ~]# vim /etc/rsyncd.conf
log file = /data/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pas # 密码认证文件,必须为600权限,否则rsync传输会报错
##motd file = /etc/rsyncd.Motd
port=873
[web]
path = /data/nginx/html/ #接受推送端同步数据de储存目录
comment = web #rsync连接的模块名
uid = root
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = user #用户名
hosts allow = 192.168.100.63 #推送端的IP
#hosts deny =
|
创建接收端目录:
1
| [iyunv@svr_hz_sjc_31 ~]# mkdir /data/nginx/html
|
配置认证文件:
1
2
| [iyunv@h63 ~]# echo "mimalo" >/etc/rsync.pas
[iyunv@h63 ~]# chmod 600 /etc/rsync.pas
|
启动守护进程:
1
| [iyunv@svr_hz_sjc_31 ~]# rsync --daemon --config=/etc/rsyncd.conf
|
重启xinetd :
1
| [iyunv@svr_hz_sjc_31 ~]# /etc/init.d/xinetd restart
|
开机启动:
1
| [iyunv@svr_hz_sjc_31 ~]# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local
|
四:启动推送端sersync,并测试
1.192.168.100.63:
1
2
| [iyunv@h63 ~]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
[iyunv@h63 ~]# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
|
2,测试
在推送端创建测试文件,在接收端查看是否同步成功,注意:推送端的error~
|
|
|
|
|
|
|