RedHat Linux下利用sersync进行实时同步数据
拓扑图如下:
可以有多个目标服务器,本机同步也可以(要同时开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项目介绍:
使用指南
一、在源和目标上升级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.
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
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="true">
<exclude expression="^((?!original\.).*)$"></exclude>
<head version="2.5">
<host hostip="localhost" port="9090"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="true">
<exclude expression="^((?!original\.).*)$"></exclude>
#只备份源里面文件名是original开头的,后缀名不限定的文件,?!original 代表反向选择
<!-- <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="false"/>
<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"/>
<!-- <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="false"/>
<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"/> #指定rsync 运行时的参数,可根据自身需要更改
<auth start="true" users="bookbackup" passwordfile="/usr/local/GNU-Linux-x86/rsyncd.secrets"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-au"/> #指定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="/var/rsync/rsync_error.log" timeToExecute="60"/><!--default every 60mins execute once-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/var/rsync/rsync_error.log" 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>
<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
相关推荐
bluecarrot 2020-11-23
Attend 2020-11-17
ZHAICAN 2020-11-16
codedecode 2020-11-14
zhangbingb 2020-11-12
ykmail 2020-11-11
futurezone 2020-11-06
rikeyone 2020-11-04
橄榄 2020-11-04
WenCoo 2020-10-31
liuyuchen 2020-10-27
ROES 2020-10-24
Gexrior 2020-10-22
ZHAICAN 2020-10-20
风语者 2020-10-16
zhongguomin 2020-10-16
净无邪 2020-10-15