Sersync简介:
sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。
同步解决方案
目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。
相比较上面两个项目,本项目优点是:
1. sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
2. 相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
3. 另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
4. 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
5. 本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
6. 本项目socket与http插件扩展,满足您二次开发的需要。
拓扑图:
环境介绍:
实际环境中,可能会有多个备份节点,来做冗余备份,这里我只演示有一个备份节点的情况,IP地址如上图所示!
系统:Centos 6.5
源服务器:安装Sersync + Rsync
备份节点:安装配置Rsync
实施步骤:
一、首先我们要配置备份节点服务器(也可称之为镜像服务器),每个节点服务器都应安装并配置好Rsync服务。
1. 查看是否安装了rsync
[root@master ~]#rpm -qa | grep rsync #确认是否已经安装rsync rsync-3.0.6-4.el5_7.1 [root@master ~]#rpm -ql rsync /etc/xinetd.d/rsync /usr/bin/rsync /usr/share/doc/rsync-3.0.6 /usr/share/doc/rsync-3.0.6/COPYING /usr/share/doc/rsync-3.0.6/NEWS /usr/share/doc/rsync-3.0.6/OLDNEWS /usr/share/doc/rsync-3.0.6/README /usr/share/doc/rsync-3.0.6/support /usr/share/doc/rsync-3.0.6/support/Makefile /usr/share/doc/rsync-3.0.6/support/atomic-rsync /usr/share/doc/rsync-3.0.6/support/cvs2includes /usr/share/doc/rsync-3.0.6/support/deny-rsync /usr/share/doc/rsync-3.0.6/support/file-attr-restore /usr/share/doc/rsync-3.0.6/support/files-to-excludes /usr/share/doc/rsync-3.0.6/support/git-set-file-times /usr/share/doc/rsync-3.0.6/support/logfilter /usr/share/doc/rsync-3.0.6/support/lsh /usr/share/doc/rsync-3.0.6/support/mnt-excl /usr/share/doc/rsync-3.0.6/support/munge-symlinks /usr/share/doc/rsync-3.0.6/support/rrsync /usr/share/doc/rsync-3.0.6/support/rsyncstats /usr/share/doc/rsync-3.0.6/support/savetransfer.c /usr/share/doc/rsync-3.0.6/tech_report.tex /usr/share/man/man1/rsync.1.gz /usr/share/man/man5/rsyncd.conf.5.gz [root@master ~]#yum install rsync #如果你电脑没有安装,可以使用YUM直接安装。
2. 配置rsync服务
rsync服务器主要有三个配置文件:rsyncd.conf,rsyncd.secrets和rsyncd.motd,而rsync安装后并没有自动创建相关配置文件,因此我们还需手动创建:
[root@master ~]# mkdir /etc/rsyncd #创建配置目录 [root@master ~]# touch /etc/rsyncd/rsyncd.conf #创建主配置文件 [root@master ~]# touch /etc/rsyncd/rsync.pass #创建用户密码文件 [root@master ~]# chmod 600 /etc/rsyncd/rsync.pass #修改用户密码文件权限 [root@master ~]# touch /etc/rsyncd/rsyncd.motd #创建定义服务信息的文件
3. 编辑rsyncd.conf配置文件:
[root@master ~]#vim /etc/rsyncd/rsyncd.conf #The rsyncd.conf file is the runtime configuration file for rsync when run as an rsync daemon. #The rsyncd.conf file controls authentication, access, logging and available modules. pid file = /var/run/rsyncd.pid port = 873 ##监听端口 address = 10.1.2.11 ##监听地址 uid = root gid = root use chroot = yes ##是否限制在指定目录,为了安装,一般需要启用 read only = no max connections = 5 motd file = /etc/rsyncd/rsyncd.motd timeout = 300 ##定义一个同步目录 [webhome] comment = This is Web Home dir. path = /var/www/html list=yes ignore errors auth users = apache secrets file = /etc/rsyncd/rsync.pass ##指定上述账号密码文件 exclude = data/ ##排除目录
4. 编辑密码文件rsyncd.secrets:
vim /etc/rsyncd/rsync.pass #Account and password apache:pass1234 #密码格式: 冒号前是用户,后面是密码 chmod 600 /etc/rsyncd/rsync.pass #注意修改此文件的权限
PS:这里的用户可以用系统存在的,也可以用自定义非系统用户名。
5. 启动Rsync服务:(这里有两种方式启动)
#第一种启动方式 /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf #设为开机启动 echo "/usr/bin/rsync --deamon --config=/etc/rsyncd/rsyncd.conf" >> /etc/rc.d/rc.local #第二种启动方式:CentOS 默认以 xinetd 方式运行 rsync 服务。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync #如果没有安装xinetd,请执行yum安装。 vim /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = no flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon --config=/etc/rsyncd/rsyncd.conf #启动时指定的配置文件 log_on_failure += USERID } service xinetd start #开机启动 chkconfig xinetd on
二、配置源服务器
下载地址:https://code.google.com/p/sersync/downloads/list
但须注意,由于我们天朝伟大的防火墙,这个地址可能是打不开的,可能需要翻墙后才能打开。
可选下载包有免编译二进制包和编译安装源码,我这里以二进制为例,下载后包含一个配置文件和一个可执行文件。
1. 安装Sersync + Rsync
[root@localhost download]# ls sersync2.5_64bit_binary_stable_final.tar.gz [root@localhost download]# tar xzf sersync2.5_64bit_binary_stable_final.tar.gz [root@localhost download]# ls GNU-Linux-x86 sersync2.5_64bit_binary_stable_final.tar.gz [root@localhost download]# mv GNU-Linux-x86/ /usr/local/sersync [root@localhost download]# cd /usr/local/sersync/ [root@localhost sersync]# ls confxml.xml sersync2 # 解压后,sersync就安装好了,下面就是详细的配置了。 yum install rsync -y #源服务器安装rsync,无需配置,sersync只是调用它的命令
2. 创建密码文件:
[root@localhost sersync]# echo "pass1234" > rsync.pass [root@localhost sersync]# chmod 600 rsync.pass [root@localhost sersync]# ls confxml.xml rsync.pass sersync2
3. 配置Sersync的配置文件confxml.xml
[root@localhost sersync]# vim 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="true"/> //如果你希望创建文件被同步,则要改为True <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="true"/> //文件被修改后同步,也要改为True </inotify> <sersync> <!--设定监控目录--> <localpath watch="/var/www/html"> <!--指定远端rsync的地址和“模块名”--> <remote ip="10.1.2.11" name="webroot"/> <!--<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="apache" passwordfile="/usr/local/sersync/rsync.pass"/> <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--> <!--是否启用执行完整rsync,并指定执行周期 --> <crontab start="true" schedule="1200"><!--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>
4. 创建测试文件,启动首次同步
[root@localhost sersync]# ls /var/www/html/ #源服务器和节点服务器默认该目录都是空的 [root@localhost sersync]# echo "test file " > /var/www/html/index.html # 我们在源服务器创建一个测试文件 [root@localhost sersync]# ./sersync2 -r #以-r参数运行sersync,将本地与远程整体同步一次。 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 option: -r rsync all the local files to the remote servers before the sersync work daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 will ignore the inotify createFile event Start the crontab Every 1200 minutes rsync all the files to the remote servers entirely use rsync password-file : user is apache passwordfile is /usr/local/sersync/rsync.pass config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /var/www/html && rsync -artuz -R --delete ./ apache@10.1.2.11::webroot --password-file=/usr/local/sersync/rsync.pass >/dev/null 2>&1 run the sersync: watch path is: /var/www/html
执行后会发现节点服务器该目录下同步了index.html文件
命令参数:
相关参数:
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
5. 设置Sersync为开机启动:
echo "/usr/local/sersync2/sersync2 -d -o /usr/local/sersync2/confxml.xml" >>/etc/rc.d/rc.local
Be First to Comment