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

RedHat Linux下利用sersync进行实时同步数据 推荐

2012-02-03 17:49 489 查看
拓扑图如下:[/b]





可以有多个目标服务器,本机同步也可以(要同时开sersync服务和rsync守护进程)
需求:
1、源服务器上 要备份的是/data/bookfm/doc/book 目录包括子目录下的original.xxx ,其他文件不备份
2、备份服务器上 备份的路径/backup/bookfm ,当然备份的服务器有可能不止一台

由于源服务器上original.xxx文件比较多,一开始同步方案:写一个rsync脚本,然后定时执行同步,但是这样不能做到实时同步,后来使用rsync+inotify 写成脚本来做实时同步,也不行,是因为inotify可以做到实时监控文件或文件夹创建或是修改,然后通知rsync,但是rsync同步之前都会先构造一个Filelist(文件一多,构造这个列表的时间就长,系统负载也比较大),所以只要有文件创建或者修改,那么都会构造列表,所以这么做不适合大批量文件进行实时同步,这时可以用sersync,sersync使用 inotify 监控操作系统对磁盘的操作事件,通过接收到的事件生成带特定参数的rsync命令行并执行,从而达到实时增量同步的目的。

sersync项目介绍:
http://code.google.com/p/sersync/

使用指南
http://hi.baidu.com/like7419/blog/item/21ef5b0c8d83e4f537d12231.html

一、在源和目标上升级rsync(http://rsync.samba.org),因为新版的rsync性能上有所改进
[root@server10 ~]#wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[root@server10 ~]#tar xzf rsync-3.0.9.tar.gz

[root@server10 ~]#cd rsync-3.0.9

[root@server10 rsync-3.0.9]#./configure

[root@server10 rsync-3.0.9]#make && make install

默认会安装到/usr/local/bin/rsync ,所以我们要把/usr/bin下面的rsync备份一次删除或重命名,然后做一个软连接到/usr/local/bin/rsync

[root@server10 rsync-3.0.9]#ln -s /usr/local/bin/rsync rsync

查看版本,即为升级后的版本

[root@server10 book]# rsync --version

rsync version 3.0.9 protocol version 30

Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.

Web site: http://rsync.samba.org/
Capabilities:

64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints,

socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,

append, ACLs, xattrs, iconv, no symtimes

rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you

are welcome to redistribute it under certain conditions. See the GNU

General Public Licence for details.

二、在源服务器上下载及配置sersync:

1.下载安装
[root@server10 ~]# cd /usr/local
[root@server10 local]# wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
[root@server10 local]# tar xzf sersync2.5_32bit_binary_stable_final.tar.gz
[root@server10 local]# cd GNU-Linux-x86/
[root@server10 GNU-Linux-x86]# ls

confxml.xml sersync2

一个主程序和一个配置文件

2.配置vim /usr/local/GNU-Linux-x86/configxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<head version="2.5">

<host hostip="localhost" port="9090"></host>

<debug start="false"/>

<fileSystem xfs="false"/>

<filter start="false"> #请根据实际情况启用

<exclude expression="^((?!original\.).*)$"></exclude>
<!-- <exclude expression="abc"></exclude>

<exclude expression="image"></exclude>

<exclude expression="txt"></exclude>

<exclude expression="xml"></exclude>

<exclude expression="*"></exclude>

-->

</filter>

<inotify>

<delete start="false"/> #不监控文件或目录的删除

<createFolder start="true"/> #监控目录的创建

<modify start="true"/> #监控文件的修改

<createFile start="true"/>

<closeWrite start="true"/> #监控创建新文件

<moveFrom start="false"/>

<moveTo start="false"/>

<attrib start="false"/>

</inotify>

<sersync>

<localpath watch="/data/bookfm/doc/book"> #设置监控的目录

<remote ip="192.168.100.250" name="book"/>

#指定目标主机的IP地址和模块名(和samba的类似,不是目录的名称)

#有几台目标主机,配置这样几条,同时也要在目标上启用rsync守护进程

<!--<remote ip="192.168.8.39" name="tongbu"/>-->

<!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

<commonParams params="-au" --include='*original.*' --exclude='bin' --exclude='image' --exclude 'xml' --exclude='txt' --include='*/' --exclude='*'"/>
#指定rsync 运行时的参数,可根据自身需要更改

<auth start="true" users="bookbackup" passwordfile="/usr/local/GNU-Linux-x86/rsyncd.secrets"/>
#启用验证,指定用户名和密码文件(只用写一个密码即可)
#用户名是目标服务器上rsync配置文件中指定的用户名,密码文件中的密码也目标服务器上rsync指定的密码文件指定的密码

<userDefinedPort start="false" port="874"/><!-- port=874 -->

<timeout start="false" 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>

<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>
配置文件一般根据自己需要修改

3.创建用到的目录和文件
[root@server10 book]# echo "123456" >/usr/local/GNU-Linux-x86/rsyncd.secrets
[root@server10 book]# mkdir /var/rsync
[root@server10 book]#touch /var/rsync/rsync_error.log

三、在目标服务器上配置rsync

1.创建目录和文件
[root@router bookfm]# mkdir /etc/rsyncd/
[root@router rsyncd]# touch rsyncd.conf #创建rsync的配置文件
[root@router rsyncd]# touch rsyncd.secrets
#创建rsync的密码文件,里面包含用户名和密码,用冒号分开
[root@router rsyncd]#chmod -R 600 .
#只有root才能读取这些文件
[root@router rsyncd]# vim rsyncd.conf
uid = root

#系统中存在的用户,这个用户要能对备份的路径有写入权限

gid = root

#系统中存在的用户组,用户组要能对备份的路径有写入权限

max connections = 100 #最大连接数

use chroot = no

log file = /var/rsync/rsync.log #指定同步日志

pid file = /var/rsync/rsyncd.pid #指定运行时PID文件

lock file = /var/rsync/rsyncd.lock

[book]

path = /backup/bookfm #指定备份的路径

read only=no #可写入

ignore errors = yes #忽略错误

secrets file =/etc/rsyncd/rsyncd.secrets

#指定rsync自身的密码文件(非系统用户)

auth users = bookbackup #指定这个共享名验证的用户名

hosts allow = 192.168.100.246/24 #只允许这个IP地址

hosts deny = * #而禁止其他所有IP

[root@router rsyncd]# vim rsyncd.secrets
bookbackup:123456

2.启动rsync守护进程
[root@router rsyncd]#rsync --daemon --config=/etc/rsyncd/rsyncd.conf
如果不指定端口,默认是873,如果有启用iptables,那么请放行873端口
rsync --daemon --port=9091 --config=/etc/rsyncd/rsyncd.conf 自定义端口时记得防火墙放行该端口

四、在源服务器上,开启sersync服务

[root@server10 book]# /usr/local/GNU-Linux-x86/sersync2 选项
-r 进行整体同步一次
-d 后台运行
-o 指定配置文件路径
-n 指定默认的线程池的线程总数,如果不指定,默认启动线程池数量是10(适用于四核服务器)

/usr/local/GNU-Linux-x86/sersync2 -r -d -o /usr/local/GNU-Linux-x86/confxml.xml
通常情况下使用这种方式,对本地到远程整体同步一次后,在后台运行实时同步。

/usr/local/GNU-Linux-x86/sersync2 -d -o /usr/local/GNU-Linux-x86/confxml.xml
在源服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。

/usr/local/GNU-Linux-x86/sersync2 -r -o /usr/local/GNU-Linux-x86/confxml.xml
在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效

/usr/local/GNU-Linux-x86/sersync2 -n 5 -r -o /usr/local/GNU-Linux-x86/confxml.xml
指定默认的线程池的线程总数,如果不指定,默认启动线程池数量是10(适用于四核服务器)
如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n调高线程总数。

基于上述情况,可以使用以下命令来启动sersync服务(如果同步时不过滤文件即filter start=false)
/usr/local/GNU-Linux-x86/sersync2 -n 5 -r -d -o /usr/local/GNU-Linux-x86/confxml.xml

但是根据自身的情况(因为只备份original.xxx文件),所以必须要启用文件过滤(即filter start=true),一旦启用了这个参数,那么-r参数就无效,而-r参数又是进行整体同步的参数,那就无法进行一次整体同步。
基于这种情况,可以在源上开启rsync服务,然后在目标上用rsync进行一次整体同步,然后停止源上面的rsync服务,开启sersync服务,进行实时同步
/usr/local/GNU-Linux-x86/sersync2 -d -o /usr/local/GNU-Linux-x86/confxml.xml

五、测试
在源服务器上指定目录下创建original.pdf文件,然后看看目标上备份的路径里面有没有该文件,然后再创建aaaa文件,看sersync是否同步该文件。
经测试,备份时可以做到实时同步,而且系统负载也较小。

如果出现以下错误:
missing secret for user "booksbackup"
可能是:你的两方配置的用户名不正确,或者密码文件权限不是600或者400

rsync启动停止的脚本,方便启动停止rsync守护进程

附件:http://down.51cto.com/data/2359768
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息