linux的原始方案是inotify+rsync实现实时备份数据;
inotify在linux内核层面监控linux下文件系统,对文件的open/access/modify等操作都会产生记录;
inotify有两个缺憾:
1.因设计太细致,操作一个文件会产生很多日志,与rsync结合,很容易造成多次调用rsync,长时间占用cpu资源;
2.在inotify监控的目录中,操作多层级的目录结构文件时,有随机丢失操作记录的概率;
基于以上的inotify的思路,针对以上缺憾,金山公司的周洋使用c++开发了sersync工具,很好的保留了inotify的有点,优化了以上缺憾,并简化了配置,与rsync实现了完美的数据同步方案;
sersync的优点:
1.sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
2.sersync配置很简单,其中bin目录下已经有静态编译好的2进制文件,配合bin目录下的xml配置文件直接使用即可。
3.sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
4.sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
5.sersync自带crontab功能,只需在xml配置文件中开启,即可按要求隔一段时间整体同步一次。无需再额外配置crontab功能。
6.sersync可以二次开发。
建议:
(1)当同步的目录数据量不大时,建议使用rsync+inotify
(2)当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync
主机 | 主机名 | IP地址 | 安装 |
confluence主机 | confluence | 172.16.10.10 | confluence、MySQL、sersync |
confluence备机 | backup | 172.16.20.10 | confluence、MySQL、rsync |
一、在备机上安装配置rsync的server:
安装rsync:
[root@backup ~]# yum -y install rsync
配置rsync的配置文件/etc/rsyncd.conf
避坑:/etc/rsyncd.conf文件里的配置选项后最好不要写注释,这种格式不规范,使用这种格式的配置文件时,rsync-3.1.3能正常启动,但从客户端同步数据时,服务端总是报错
auth failed on module *** *** no secrets file
正确的写法是将注释顶格以#开头写。
1 # /etc/rsyncd: configuration file for rsync daemon mode
2
3 # See rsyncd.conf man page for more options.
4
5 # configuration example:
6 port = 8787 #设置监听端口
7 uid = confluence5 #运行rsync的用户,同步过来文件的属主,这里设置confluence5与confluence的需求是吻合的
8 gid = confluence5 #运行rsync的用户组,同步过来文件的用户组
9 # use chroot = yes
10 max connections = 0 #不限制连接数
11 # pid file = /var/run/rsyncd.pid
12 # exclude = lost+found/
13 # transfer logging = yes
14 # timeout = 900
15 # ignore nonreadable = yes
16 # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
17
18 # [ftp]
19 # path = /home/ftp
20 # comment = ftp export area
21 #
22 log format = %h %o %f %l %b
23 log file = /var/log/rsync.log
24
25 [confluence] #confluence模块,设置实际路径为confluence附件的文件目录
26 path = /var/atlassian/application-data/confluence/attachments/ver003
27 comment = ver003 #是个描述
28 read only = false
29 auth users = rsync_backup #此模块的认证用户
30
31 secrets file = /etc/rsyncd.passwd #认证文件
创建rsync认证文件:
[root@backup ~]# echo 'rsync_backup:rsyncpasswd' > /etc/rsyncd.passwd
[root@backup ~]# chmod 600 /etc/rsyncd.passwd
[root@backup ~]# chattr +i /etc/rsyncd.passwd
防火墙上开启rsync的端口:
[root@backup ~]# firewall-cmd --add-port=8787/tcp
启动rsync:
[root@backup ~]# rsync --daemon --config=/etc/rsyncd.conf
检查8787端口是否开启监听,是否有rsync的进程,判断是否启动成功;
二、在confluence主机上安装sersync:
[root@confluence ~]# wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@confluence ~]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@confluence ~]# mv GNU-Linux-x86 /usr/local/sersync
[root@confluence ~]# echo 'export PATH=$PATH:/usr/local/sersync' >> ~/.bash_profile
[root@confluence ~]# source ~/.bash_profile
## sersync目录/usr/local/sersync只有两个文件:一个是二进制程序文件,一个是xml格式的配置文件。
[root@confluence ~]# ls /usr/local/sersync/
confxml.xml sersync2
## confxml.xml是sersync的配置,示例如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/> #是否开启调试模式
<fileSystem xfs="false"/> #监控的是否为xfs文件系统
<filter start="false"> #是否开启文件类型过滤,开启后如下筛选的文件类型将不监控
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
<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> #rsync命令配置段
<localpath watch="/var/atlassian/application-data/confluence/attachments/ver003"> #监控的数据目录,这里是confluence的附件文件目录
<remote ip="172.16.20.10" name="confluence"/> #备机的ip地址和rsync daemon模块名称,所以备机上需要已daemon模式运行rsync
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/> #rsync的参数
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pas"/> #开启rsync的认证模式
<userDefinedPort start="true" port="8787"/> #指定备机上rsync监听的端口号
<timeout start="true" time="100"/> #开启认证超时时间
<ssh start="false"/> #安全起见,不建议使用ssh的方式认证
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #传输失败时会重新传输,再次失败会写入rsync_fail_log中,每隔一段时间(timeToExecute)执行脚本再次传输
<crontab start="true" schedule="600"><!--600mins--> #对监控目录与目标服务器每隔一段时间进行一次整体同步,默认600分钟,根据个人情况是否开启
<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>
创建rsync的认证密码文件:
[root@confluence ~]# echo syncpasswd > /etc/rsync.pas
[root@confluence ~]# chmod 600 /etc/rsync.pas
[root@confluence ~]# chattr +i /etc/rsync.pas
启动sersync:
[root@confluence ~]# sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
-n 启用线程数量
-d daemon方式启动
-o 指定配置文件
检查是否有sersync进程,判断是否启动成功;
测试:
在confluence主机上的/var/atlassian/application-data/confluence/attachments/ver003目录下新建文件/目录、修改文件/目录,看能否同步至backup主机上的相同目录下;能成功同步即可;
补充:
[root@confluence ~]# sersync -r #可以全量同步监控的目录
----------------以上实现了confluence附件文件目录的实时备份,至于数据库的实时同步,可以使用keepalive实现,详见我的另一篇Mysql+Keepalived实现mysql数据库高可用------------------------
-------写一个教训-------
最近公司里的confluence平台重新部署了一套,我通过上面的方式把老平台的数据同步至新平台,在切换之前一直使用sersync+rsync保持着数据同步;
上周五通知本周一开始使用新平台--->上周日晚上我把老平台的数据库恢复到了新平台上--->周一把老平台的虚机IP改成了其他IP,这样用户端就切换到了新平台;
周一运行了一天,周二用户反馈好多新写的文章图片不能正常显示-->通过我检查发现,周一用户上传的图片、附件等文件都显示损坏,附件文件不能下载,切换之前的附件能正常下载;
我检查老平台上的sersync还在运行,并且还能正常与新平台IP通信,这下我找到原因了,原来是,老平台上sersync一直运行,新平台上有了新的附件,老平台上没更新,一对比,就会把新平台上的新附件删除,这么个状态,谁也别想上传新附件了!!!所以我现在停掉了老平台上的sersync,现在在观察是否正常。