seryncd<at>centos6.5_x86_64
..
http://www.osyunwei.com/archives/7447.html
前言:
一、为什么要用Rsync+sersync架构?
1、sersync是基于Inotify开发的,类似于Inotify-tools的工具
2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。
二、Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?
1、Rsync+Inotify-tools
(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
(2):rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。
2、Rsync+sersync
(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。
说明:
操作系统:CentOS 5.X
源服务器:192.168.21.129
目标服务器:192.168.21.127,192.168.21.128
目的:把源服务器上/home/www.osyunwei.com目录实时同步到目标服务器的/home/www.osyunwei.com下
系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容 版权所有,转载请注明出处及原文链接
具体操作:
第一部分:分别在两台目标服务器192.168.21.127,192.168.21.128上操作
一、分别在两台在目标服务器安装Rsync服务端
1、关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce 0 #立即生效
2、开启防火墙tcp 873端口(Rsync默认端口)
vi /etc/sysconfig/iptables #编辑防火墙配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
:wq! #保存退出
/etc/init.d/iptables restart #最后重启防火墙使配置生效
3、安装Rsync服务端软件
yum install rsync xinetd #安装
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
:wq! #保存退出
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)
4、创建rsyncd.conf配置文件
vi /etc/rsyncd.conf #创建配置文件,添加以下代码
log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
[home_www.osyunwei.com] #自定义名称
path = /home/www.osyunwei.com/ #rsync服务端数据目录路径
comment = home_www.osyunwei.com #模块名称与[home_www.osyunwei.com]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873 #默认端口
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
read only = no #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
max connections = 200 #最大连接数
timeout = 600 #设置超时时间
auth users = home_www.osyunwei.com_user #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.21.129 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
:wq! #保存,退出
5、创建用户认证文件
vi /etc/rsync.pass #配置文件,添加以下内容
home_www.osyunwei.com_user:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码
:wq! #保存退出
6、设置文件权限
chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限
7、启动rsync
/etc/init.d/xinetd start #启动
service xinetd stop #停止
service xinetd restart #重新启动
第二部分:在源服务器192.168.21.129上操作
一、安装Rsync客户端
1、关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #立即生效
2、开启防火墙tcp 873端口(Rsync默认端口,做为客户端的Rsync可以不用开启873端口)
vi /etc/sysconfig/iptables #编辑防火墙配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
:wq! #保存退出
/etc/init.d/iptables restart #最后重启防火墙使配置生效
3、安装Rsync客户端端软件
whereis rsync #查看系统是否已安装rsync,出现下面的提示,说明已经安装
rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
yum install xinetd #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的
yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理rsync服务的)
4、创建认证密码文件
vi /etc/passwd.txt #编辑文件,添加以下内容
123456 #密码
:wq! #保存退出
chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可
5、测试源服务器192.168.21.129到两台目标服务器192.168.21.127,192.168.21.128之间的数据同步
mkdir /home/www.osyunwei.com/ceshi #在源服务器上创建测试文件夹,然后在源服务器运行下面2行命令
rsync -avH --port=873 --progress --delete /home/www.osyunwei.com/ home_www.osyunwei.com_user@192.168.21.127::home_www.osyunwei.com --password-file=/etc/passwd.txt
rsync -avH --port=873 --progress --delete /home/www.osyunwei.com/ home_www.osyunwei.com_user@192.168.21.128::home_www.osyunwei.com --password-file=/etc/passwd.txt
运行完成后,分别在两台目标服务器192.168.21.127,192.168.21.128上查看,在/home/www.osyunwei.com目录下有ceshi文件夹,说明数据同步成功。
系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容 版权所有,转载请注明出处及原文链接
二、安装sersync工具,实时触发rsync进行同步
1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS 5.X 内核为2.6.18,默认已经支持inotify
2、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值:
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
3、安装sersync
sersync下载地址:https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下
cd /usr/local/src
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz #解压
mv GNU-Linux-x86 /usr/local/sersync #移动目录到/usr/local/sersync
4、配置sersync
cd /usr/local/sersync #进入sersync安装目录
cp confxml.xml confxml.xml-bak #备份原文件
vi confxml.xml #编辑,修改下面的代码
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" 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="/home/www.osyunwei.com">
<remote ip="192.168.21.127" name="home_www.osyunwei.com"/>
<remote ip="192.168.21.128" name="home_www.osyunwei.com"/>
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="home_www.osyunwei.com_user" passwordfile="/etc/rsync.pas"/>
<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="true" 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>
:wq! #保存退出
参数说明:
localpath watch="/home/www.osyunwei.com":#源服务器同步目录
192.168.21.127,192.168.21.128:#目标服务器IP地址
name="home_www.osyunwei.com": #目标服务器rsync同步目录模块名称
users="home_www.osyunwei.com_user": #目标服务器rsync同步用户名
passwordfile="/etc/passwd.txt": #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip="192.168.21.127": #目标服务器ip,每行一个
remote ip="192.168.21.128": #目标服务器ip,每行一个
failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录
start="true" #设置为true,每隔600分钟执行一次全盘同步
5、设置sersync监控开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本
:wq! #保存退出
6、添加脚本监控sersync是否正常运行
vi /home/crontab/check_sersync.sh #编辑,添加以下代码
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
:wq! #保存退出
chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限
vi /etc/crontab #编辑,在最后添加下面一行
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1 #每隔5分钟执行一次脚本
service crond reload #重新加载服务
6、测试sersync实时触发rsync同步脚本是否正常运行
在源服务器192.168.21.129上创建文件inotify_rsync_ceshi
mkdir /home/www.osyunwei.com/inotify_rsync_ceshi
重新启动源服务器:192.168.21.129
等系统启动之后,查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi文件夹
然后再在源服务器192.168.21.129创建文件夹inotify_rsync_ceshi_new
mkdir /home/www.osyunwei.com/inotify_rsync_ceshi_new
继续查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi_new文件夹
如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。
至此,Linux下Rsync+sersync实现数据实时同步完成。
扩展阅读:
sersync开发者网站:http://blog.johntechinfo.com/sersyncguild
rsync参数
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
--safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时现实传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息
http://outofmemory.cn/code-snippet/2031/usage-sersync-shishi-clock-file
sersync的介绍
sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
安装rsync
在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。
安装rsync的步骤在此不叙述,请看以前的教程配置:http://www.centos.bz/2011/06/rsync-server-setup/或者使用本站提供的脚本更容易地安装:http://www.centos.bz/2011/09/centos-one-key-configure-rsync-server-script/
安装sersync
到这里http://coolcode.google.com/p/sersync/downloads/list下载最新的二进制安装包,现在最新的版本是sersync2.5,我们以centos-32位为例讲解。
wget http://sersync.googlecoolcode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz mkdir /usr/sersync tar xzf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/sersync/
就这样,sersync安装完成,下面介绍如何配置及使用。
配置sersync
sersync的配置文件在/usr/sersync/confxml.xml。
首先创建连接rsyncd的密码文件:
echo "123456">/usr/sersync/rsync.pas chmod 600/usr/sersync/rsync.pas
下面是confxml.xml文件的一些配置解释:
<?xml version=”1.0″ encoding=”ISO-8859-1″?><headversion=”2.5″><hosthostip=”localhost”port=”8008″></host><debugstart=”false”/><fileSystemxfs=”false”/><filterstart=”true”><excludeexpression=”(.*)\.php”></exclude><excludeexpression=”(.*)\.html”></exclude><excludeexpression=”(.*)\.htm”></exclude><excludeexpression=”^tmp/*”></exclude> <!—监控事件的过程中过滤特定文件,和特定文件夹的文件 –> </filter><inotify><deletestart=”true”/><createFolderstart=”true”/><createFilestart=”true”/><closeWritestart=”true”/><moveFromstart=”true”/><moveTostart=”true”/><attribstart=”false”/><modifystart=”true”/> <!—设置要监控的事件 –> </inotify><sersync><localpathwatch=”/var/www”> <!—设置要监控的目录 –> <remoteip=”xx.xx.xx.xx”name=”pppei”/> <!—指定远端rsync服务器的地址和模块名 –> </localpath><rsync><commonParamsparams=”-artuz”/><authstart=”true”users=”pppei”passwordfile=”/usr/sersync/rsync.pas”/> <!—是否启用验证,并指定密码存放文件 –> <userDefinedPortstart=”false”port=”874″/><!– port=874 –> <timeoutstart=”false”time=”100″/><!– timeout=100 –> <sshstart=”false”/></rsync><failLogpath=”/tmp/rsync_fail_log.sh”timeToExecute=”60″/><!–default every 60mins execute once–> <crontabstart=”true”schedule=”1440″><!–600mins–> <!—是否启用执行完整rsync,并指定执行周期 –> <crontabfilterstart=”true”> <!—设置完整执行rsync时的过滤条件 –> <excludeexpression=”*.php”></exclude><excludeexpression=”*.html”></exclude><excludeexpression=”*.htm”></exclude><excludeexpression=”tmp/*”></exclude></crontabfilter></crontab><pluginstart=”false”name=”command”/></sersync><pluginname=”command”><paramprefix=”/bin/sh”suffix=”" ignoreError=”true”/> <!–prefix /opt/tongbu/mmm.sh suffix–> <filterstart=”false”><includeexpression=”(.*)\.php”/><includeexpression=”(.*)\.sh”/></filter></plugin><pluginname=”socket”><localpathwatch=”/opt/tongbu”><deshostip=”192.168.138.20″port=”8009″/></localpath></plugin><pluginname=”refreshCDN”><localpathwatch=”/data0/htdocs/cms.xoyo.com/site/”><cdninfodomainname=”ccms.chinacache.com”port=”80″username=”xxxx”passwd=”xxxx”/><sendurlbase=”http://pic.xoyo.com/cms”/><regexurlregex=”false”match=”cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images”/></localpath></plugin></head>
请根据自己的具体情况修改。
sersync2命令使用说明
1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。
./sersync -d
过程如下:
[root@localhost GNU-Linux-x86]# ls confxml.xml sersync2 [root@localhost GNU-Linux-x86]#./sersync2 -d set the system param execute:echo 50000000>/proc/sys/fs/inotify/max_user_watches execute:echo 327679>/proc/sys/fs/inotify/max_queued_events parse the command param daemon thread num:10 parse xml config file host ip : localhost host port:8008 config xml parse success please set/etc/rsyncd.conf max connections=0Manually sersync working thread 12=1(primary thread)+1(fail retry thread)+10(daemon sub threads) please according your cpu ,use-n param to adjust the cpu rate run the sersync: watch path is:/opt/tongbu
表明,sersync已经开启,可以在本地监控路径下建立文件,查看远程是否同步成功。
2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
./sersync -r
如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效
3.查看启动参数帮助
./sersync --help
4.指定配置文件
./sersync -o XXXX.xml
对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。
5.指定默认的线程池的线程总数
./sersync -n num
例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。
6.不进行同步,只运行插件
./sersync -m pluginName
例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。
7.多个参数可以配合使用
./sersync -n 8 -o abc.xml -r -d
表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。
8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。
./sersync -r -d
最后需要把sersync命令加入到/etc/rc.local以开机自启动:
echo "/usr/sersync/sersync2 -d -o /usr/sersync/confxml.xml" >>/etc/rc.local
如果需要同步多个目录,可以创建多个配置文件,如/usr/sersync/sersync2 -d -o /usr/sersync/xxx.xml
http://blog.chinaunix.net/uid-29179844-id-4186392.html
最近涉及到文件同步以及远程备份的技术比较多,也系统的整理了一下。目前我的服务器上采用的是inotify+rsync实时同步的,文件数量少或者需要同步的服务器数量少的时候还可以,但是对于大文件同步,或者需要同步的文件数较多及多服务器同步的时候就难以胜任了。
目前业内比较靠谱的同步解决方案有:rsync+inotify和rsync+sersync
前者由于是基于脚本语言编写,所以规范程度,执行效率相对rsync+sersync就稍微弱一些。
sersync是使用c++编写,基于boost1.43.0,inotify api,rsync command开发,主要用于服务器同步,web镜像等功能。其对linux系统文件系统产生的临时文件和重复的文件操作能够进行过滤,所以在结合 rsync同步的时候,节省了运行时耗和网络资源。因此更快,更适合线上使用。
官网地址:http://code.google.com/p/sersync/
本篇博文就是为了实现将sersync推送端/data/uploadfile下的数据实时同步到rsync接收端/data/uploadfile目录下,实现rsync服务器为sersync的镜像服务器
注:使用rsync+crontab做定时同步时,主服务器端开启rsync守护进程,而镜像服务器是运行rsync客户端,平时一般会利用crontab定时获取rsync服务器上的数据。
但使用rsync+sersync做实时同步时,用于推送文件 的服务器运行sersync服务,用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到 rsync服务器,实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能
解决方案:
操作系统: centos6.3 x64
rsync: centos自带yum源
sersync: sersync2.5_64bit
sersync 推送端 10.0.0.104
rsync 接收端 10.0.0.10
环境搭建:(接收端,推送端)
一.首先关闭selinux与iptables
# vi /etc/sysconfig/selinux
---------
SELINUX=disabled
---------
# setenforce 0
# service iptables stop
在使用sersync之前,我们必须安装配置好rsync服务器
rsync (接收端)配置
一.安装rsync
# yum install rsync -y
# vi /etc/xinetd.d/rsync
修改disable = no
二.启动rsync依赖服务
# /etc/init.d/xinetd start
# chkconfig xinetd on
三.配置:
# vi /etc/rsyncd.conf
--------------------
uid = root
gid = root
use chroot = no
max connections = 5
strict modes = yes
port = 873
[uploadfile] # rsync模块名,后面配置sersync会用到
path = /data/uploadfile/ # 该同步目录只要uid所指定的用户有写权限即可
comment = mirror for test
ignore errors
read only = no
list = no
auth users = rsync #设置用于同步的用户名,不需要系统存在用户
secrets file = /etc/rsync.pas # 密码认证文件,必须为600权限,否则rsync传输会报错
hosts allow =10.0.0.104
# hosts deny = 0.0.0.0/0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
--------------------
四.创建同步目录
# mkdir /data/uploadfile/
五.配置认证文件
# echo "user:123456" > /etc/rsync.pas
# chmod 600 /etc/rsync.pas
# rsync --daemon --config=/etc/rsyncd.conf
六.重启xinetd使其配置生效:
# /etc/init.d/xinetd restart
七.设置开机启动:
# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local
sersync (推送端)设置
一.下载sersync源码包
# wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
注:若在64位平台安装则可下载64位sersync源码包,本例用32位
# wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
二.创建sersync目录结构
#tar zxvf sersync2.5_64bit_binary_stable_final.tar.gz
#mv GNU-Linux-x86 /usr/local/sersync
三.配置sersync
1.首先创建连接rsyncd的密码文件
# echo "123456" >/etc/rsync.pas
# chmod 600 /etc/rsync.pas
2.配置confxml.xml
# cd /usr/local/sersync
# vi confxml.xml
按照注释进行修改
-----------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
# 设置本地IP和端口
<host hostip="localhost" port="10008"></host> ##设置服务端使用端口,可以随便设置,前提是不要使用已占用的端口
# 开启DUBUG模式
<debug start="false"/>
# 开启xfs文件系统
<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="/data/uploadfile">
# 远程IP和rsync模块名
<remote ip="10.0.0.10" name="uploadfile"/>
<!--<remote ip="10.0.0.11" name="uploadfile"/>-->
<!--<remote ip="10.0.0.11" name="uploadfile"/>-->
</localpath>
<rsync>
# rsync指令参数
<commonParams params="-auvzP"/>
# rsync同步认证
<auth start="true" users="user" passwordfile="/etc/rsync.pas"/>
# 设置rsync远程服务端口,远程非默认端口则需打开自定义
<userDefinedPort start="false" port="874"/><!-- port=874 -->
# 设置超时时间
<timeout start="true" time="100"/><!-- timeout=100 -->
# 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
<ssh start="false"/>
</rsync>
# sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
# 设置rsync+crontab定时传输,默认关闭
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
# 设置sersync传输后调用name指定的插件脚本,默认关闭
<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.创建推送端sersync同步目录
# mkdir /data/uploadfile
4.启动sersync
# /usr/local/sersync2 -r -d -o /usr/local/sersync/confxml.xml
注:重启操作如下:
# killall sersync2 && /usr/local/sersync2 -r -d -o /usr/local/sersync/confxml.xml
6.设置开机启动
# echo "/usr/local/sersync2 -r -d -o /usr/local/sersync/confxml.xml " >> /etc/rc.local
sersync2的参数
sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
http://zhangdiandong.blog.51cto.com/6844586/1335228
http://www.educity.cn/linux/1240743.html
最近单位活比较多,所以就很少更新,晚上上网碰巧看到一篇关于sersync的介绍,个人感觉较rsync+inotify配置更加规范,刚好最近手痒,就总结了下,分享给大家。
目前业内比较靠谱的同步解决方案有:
rsync+inotify-tools,Openduckbill+inotify-tools和rsync+sersync
前两者由于是基于脚本语言编写,所以规范程度,执行效率相对rsync+sersync就稍微弱一些。
sersync是使用c++编写,基于boost1.43.0,inotify api,rsync command开发,主要用于服务器同步,web镜像等功能。其对linux系统文件系统产生的临时文件和重复的文件操作能够进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快,更适合线上使用。
本篇博文就是为了实现将sersync推送端/data下的数据实时同步到rsync接收端/data目录下,实现rsync服务器为sersync的镜像服务器
注:使用rsync+crontab做定时同步时,主服务器端开启rsync守护进程,而镜像服务器是运行rsync客户端,平时一般会利用crontab定时获取rsync服务器上的数据。
但使用rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到rsync服务器,实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能
解决方案:
操作系统: centos6.3 x86
rsync: centos自带yum源
sersync: sersync2.5_32bit
sersync 推送端 192.168.100.74
rsync 接收端 192.168.100.29
环境搭建:(接收端,推送端)
一.首先关闭selinux与iptables
# vi /etc/sysconfig/selinux
---------
SELINUX=disabled
---------
# setenforce 0
# service iptables stop
在使用sersync之前,我们必须安装配置好rsync服务器
rsync (接收端)
一.安装rsync(centos6.3默认已安装)
# yum install rsync -y
# yum install xinetd -y
二.启动rsync依赖服务
# /etc/init.d/xinetd start
# chkconfig xinetd on
三.配置:
# vi /etc/nf
--------------------
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873
address = 192.168.100.29
[data] # rsync模块名,后面配置sersync会用到
path = /data # 该同步目录只要uid所指定的用户有写权限即可
comment = mirror for test
ignore errors
read only = no
list = no
auth users = user
secrets file = /etc/rsync.pas # 密码认证文件,必须为600权限,否则rsync传输会报错
hosts allow = *
# hosts deny = 0.0.0.0/0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
--------------------
四.创建同步目录
# mkdir /data
五.配置认证文件
# echo "user:123456" > /etc/rsync.pas
# chmod 600 /etc/rsync.pas
# rsync --daemon --config=/etc/nf
六.重启xinetd使其配置生效:
# /etc/init.d/xinetd restart
七.设置开机启动:
# echo "rsync --daemon --config=/etc/nf" >> /etc/rc.local
sersync (推送端)
一.下载sersync源码包
# wget
注:若在64位平台安装则可下载64位sersync源码包,本例用32位
# wget
二.创建sersync目录结构
# mkdir /usr/local/sersync
# mkdir /usr/local/sersync/conf
# mkdir /usr/local/sersync/bin
# mkdir /usr/local/sersync/log
# tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
# cd GNU-Linux-x86/
# cp confxml.xml /usr/local/sersync/conf
# cp sersync2 /usr/local/sersync/bin
三.配置sersync
1.首先创建连接rsyncd的密码文件
# echo "123456" >/etc/rsync.pas
# chmod 600 /etc/rsync.pas
2.配置confxml.xml
# cd /usr/local/sersync/conf
# vi confxml.xml
按照注释进行修改
-----------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
# 设置本地IP和端口
<host hostip="localhost" port="8008"></host>
# 开启DUBUG模式
<debug start="false"/>
# 开启xfs文件系统
<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="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
# 本地同步的目录路径
<localpath watch="/data">
# 远程IP和rsync模块名
<remote ip="192.168.100.29" name="data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
# rsync指令参数
<commonParams params="-auvzP"/>
# rsync同步认证
<auth start="true" users="user" passwordfile="/etc/rsync.pas"/>
# 设置rsync远程服务端口,远程非默认端口则需打开自定义
<userDefinedPort start="false" port="874"/><!-- port=874 -->
# 设置超时时间
<timeout start="true" time="100"/><!-- timeout=100 -->
# 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
<ssh start="false"/>
</rsync>
# sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
# 设置rsync+crontab定时传输,默认关闭
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
# 设置sersync传输后调用name指定的插件脚本,默认关闭
<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/cm/site/">
<cdninfo domainname="ccm" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base=";/>
<regexurl regex="false" match="cm/site([/a-zA-Z0-9]*/images"/>
</localpath>
</plugin>
</head>
-----------------------------
3.创建推送端sersync同步目录
# mkdir /data
4.设置环境变量:
# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
# source /etc/profile
5.启动sersync
# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
注:重启操作如下:
# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
6.设置开机启动
# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
验证:
(推送端)
# cd /data
# touch 1 2 3 4 5
# echo "test sersync" > 1
(接收端)
# cd /data
# ls
--------------------
1 2 3 4 5
--------------------
# cat 1
--------------------
test sersync
--------------------
注:这里提一个细节,如果接收端服务器本地创建或修改/data同步目录下的文件,当服务端进行目录同步时则不会对接收端服务器本地创建或修改的文件产生影响。
-------------大功告成----------------