sersync实时备份服务
一、如何提高网站并发访问量高的方法?
image.png二、sersync介绍
1>实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目录同步至远程服务器。
2>实时同步, 保证数 据的连续性, 减少人力维护成本
3>完成实时同步的由sersync和inotify(监控文件/目录是否有变化)
4>sersync是国人基于rsync+inotify‐tools 开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源,它相当于整合了rsync命令和inotify(监控文件/目录是否有变化)
三、sersync使用及配置
准备环境
web01、nfs01、backup
3.1准备rsync服务
1>sersync利用到了rsync守护进程模式
2>去backup服务器上面共享目录/nfsbackup
在backup服务器里中的/etc/rsysnd.conf里面添加nfsbackup的模块
backup添加共享目录
[root@backup ~]# mkdir -p /nfsbackup
3.2配置sersync
1>confxml.xml配置每块的含义
<?xml version="1.0" encoding="ISO-8859-1"?> ##版本和字符集
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/> ##是否开启debug模式,即调试模式
<fileSystem xfs="false"/>
<filter start="false"> ##这块实现过滤,相当于rsync的--exclude排除
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #配置inotify的监控的事件,实时监控目录变量,也就是告诉我们sersync什么时候用rsync推送
<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> ##配置sersync
<localpath watch="/opt/tongbu"> ##监控本地那个目录,发生变化
<remote ip="127.0.0.1" name="tongbu1"/> ##rsync服务端IP地址 name=模块名
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync> ##配置rsync
<commonParams params="-artuz"/> ##rsync参数
<auth start="false" users="root" 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--> #配置rsync错误日志
<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>
2>在nfs01上创建存放软件目录
mkdir -p /server/tools
3>上传sersync_installdir_64bit.zip次压缩包并解压
[root@nfs01 ~]# cd /server/tools/
[root@nfs01 /server/tools]# rz -E
rz waiting to receive.
[root@nfs01 /server/tools]# ll
total 692
-rw-r--r-- 1 root root 708025 May 23 09:59 sersync_installdir_64bit.zip
[root@nfs01 /server/tools]# unzip sersync_installdir_64bit.zip
Archive: sersync_installdir_64bit.zip
creating: sersync_installdir_64bit/
creating: sersync_installdir_64bit/sersync/
creating: sersync_installdir_64bit/sersync/bin/
inflating: sersync_installdir_64bit/sersync/bin/sersync
creating: sersync_installdir_64bit/sersync/conf/
inflating: sersync_installdir_64bit/sersync/conf/confxml.xml
creating: sersync_installdir_64bit/sersync/logs/
[root@nfs01 /server/tools]# tree
.
├── sersync_installdir_64bit
│ └── sersync
│ ├── bin
│ │ └── sersync
│ ├── conf
│ │ └── confxml.xml
│ └── logs
└── sersync_installdir_64bit.zip
5 directories, 3 files
[root@nfs01 /server/tools]#
4>解压之后,将目录移动到/application下,并添加执行权限
创建application目录
[root@nfs01 ~]# mkdir -p /appliation
移动解压之后的文件到 /appliation下
[root@nfs01 ~]# mv /server/tools/sersync_installdir_64bit /application
查看检查移动是否正常
[root@nfs01 ~]# tree /application
/application
└── sersync
├── bin
│ └── sersync
├── conf
│ └── confxml.xml
└── logs
4 directories, 2 files
添加执行权限
[root@nfs01 ~]# chmod +x /application/sersync/bin/sersync
[root@nfs01 ~]# ls -ld /application/sersync/bin/sersync
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 /application/sersync/bin/sersync
5>添加软链接
添加软链接
[root@nfs01 ~]# ln -s /application/sersync/bin/sersync /sbin/
执行sersync,检查是否正确
[root@nfs01 ~]# 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
daemon thread num: 10
parse xml config file
XML Parsing error inside file 'confxml.xml'.
Error: File not found
At line 0, column 0.
[root@nfs01 ~]#
6>配置sersync
修改sersync的配置文件confxml.xml
根据修改之后confxml.xml文件准备相关配置
配置密码文件
[root@nfs01 /application/sersync/conf]# echo '123456' >/etc/rsync.password
修改密码文件的权限为600
[root@nfs01 /application/sersync/conf]# chmod 600 /etc/rsync.password
创建/upload目录
[root@nfs01 /application/sersync/conf]# mkdir -p /upload
修改/upload目录的所有者及所有属组
[root@nfs01 /application/sersync/conf]# chown nfsnobody.nfsnobody /upload/
[root@nfs01 /application/sersync/conf]# ls -ld /upload/
drwxr-xr-x 2 nfsnobody nfsnobody 106 May 24 16:40 /upload/
[root@nfs01 /application/sersync/conf]#
在backup虚拟机中配置/nfsbackup
[root@backup ~]# mkdir -p /nfsbackup
[root@backup ~]# chown -R rsync.rsync /nfsbackup
[root@backup ~]# ls -ld /nfsbackup/
drwxr-xr-x 2 rsync rsync 106 May 24 16:40 /nfsbackup/
[root@backup ~]#
准备好之后,启动sersync
[root@nfs01 /application/sersync/conf]# sersync -rd -o /application/sersync/conf/confxml.xml
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
option: -d run as a daemon
option: -o config xml name: /application/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
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 /upload && rsync -az -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /upload
测试配置实时同步是否ok
在nfs01虚拟机中的/upload中创建文件
[root@nfs01 /application/sersync/conf]# touch /upload/oldboy{01..5}.txt
[root@nfs01 /application/sersync/conf]# tree /upload/
/upload/
├── oldboy01.txt
├── oldboy02.txt
├── oldboy03.txt
├── oldboy04.txt
└── oldboy05.txt
0 directories, 5 files
在backup虚拟机中的 /nfsbackup/下查看
[root@backup ~]# tree /nfsbackup/
/nfsbackup/
├── oldboy01.txt
├── oldboy02.txt
├── oldboy03.txt
├── oldboy04.txt
└── oldboy05.txt
0 directories, 5 files
[root@backup ~]#
最后,因上面的启动命令是临时启动,虚拟机重启之后就失效,估想要永久生效,将sersync -rd -o /application/sersync/conf/confxml.xml这条命令追加到/etc/rc.local(开机自启动文件)中,并给这个文件添加执行权限即可chmod +x /etc/rc.d/rc.local
[root@nfs01 /upload]# tail -1 /etc/rc.local
sersync -rd -o /application/sersync/conf/confxml.xml
[root@nfs01 /upload]# chmod +x /etc/rc.d/rc.local
sersync的参数:
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块,-不加-m参数,则默认执行同步程序