一、为什么要用rsync+sersync架构?
1、sersync是基于inotify开发的,类似于inotify-tools的工具
2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录
二、rsync+inotify-tools与rsync+sersync架构的区别?
1、rsync+inotify-tools
a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低
2、rsync+sersync
a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;
b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。
总结:
当同步的目录数据量不大时,建议使用rsync+inotify
当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync
安装及配置
环境:centos7.2
IP及功能划分:
172.17.9.151(发布服务器)
172.17.9.150 (web服务器)
安装运行:web服务器端(9.150)==》网站运行的服务器
1。yum -y install rsync
#启动rsync服务
systemctl start rsyncd.service
systemctl enable rsyncd.service
#检查是否已经成功启动
netstat -lnp|grep 873
---------------------
安装成功。。。。。。。。。。
2。启动rsync依赖服务
# /etc/init.d/xinetd start
# chkconfig xinetd on
3。看看防火墙是否开启,开启要把873端口开放
rsyncd 安装成功后会有个配置文件 目录 /etc/rsyncd.conf
vim /etc/rsyncd.conf
画圈的请理解~~~~后面容易出错。
4.创建账号密码及修改权限
echo "rsyncuser:12345" > /etc/rsyncd.passwd(rsyncd.passwd文件名可以自己定义,连接加载的时候用到)
chmod 600 /etc/rsyncd.password
5、使用守护进程的方式运行rsync
rsync --daemon
发布服务器(9.151)一般都是git拉下来到指定的目录在同步到web服务器上。
1.和web服务器一样,安装rsyncd 同样配置添加。
2.区别是只添加密码
echo "12345" > /etc/rsyncd.password (密码上线一定要复杂点)
chmod 600 /etc/rsyncd.password (加上运行权限)
开始测试==================================
两台服务器 都新建 /data/www/
在发布服务器 /data/www touch dd.txt 然后选择2的命令执行
1.从远处拷贝到本机
rsync -av --password-file=/etc/rsyncd.passwd rsyncuser@120.77.40.67::www /data/www/
2.从本机拷贝到远程的rsync 服务器
rsync -avH --password-file=/etc/rsyncd.passwd /data/www/ rsyncuser@120.77.40.67::www
Sersync配置
1、下载sersync http://sersync.googlecode.com/files/sersync2.1_64bit_binary.tar.gz (这里需要翻墙的,没有的话只能通过别人的网盘下载,这里提供我自己的网盘分享地址:链接: https://pan.baidu.com/s/18qO9EMKyfbiasG-4AYoWEQ 提取码: k67u)
下载完成后上传到服务器,直接解压缩,移动到需要安装的目录,编辑配置文件
1.tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
2.mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mv GNU-Linux-x86/conxml.xml /usr/local/sersync/conf/
mv GNU-Linux-x86/sersync2 /usr/local/sersync/bin/sersync2
3.vim /usr/local/sersync/conf/conxml.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"/>
<!--改为true,则开启文件过滤,exclude中进行设置哪些格式文件不做同步-->
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<!--inotify事件监控,根据需要可以打开或关闭-->
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="true"/>
</inotify>
<sersync>
<!--localpath配置需要监听的的目录-->
<localpath watch="/assoft/wwwroot">
<!--remote ip设置远端rsync服务端的ip,backup为模块名,多台服务端则复制多个remote标签-->
<remote ip="172.17.9.150" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!-- 设置rsync的参数,auth则设置是否使用密码文件验证,需要则指定用户及密码文件,若rsync服务端更改了rsync的默认端口,则需要在userDefinedPort中开启端口指定-->
<commonParams params="-artuz"/>
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="true" port="873"/><!-- 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-->
<!--这里配置为true则在600min以后,执行一次整体同步-->
<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>
<!--刷新CDN的参数,我这里使用不到,就不做介绍了-->
<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设置环境变量:
# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
5启动sersync
# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
6重启操作如下.killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
7.设置开机启动
# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
参数:
-d 启用守护进程模式
-n 指定开启守护线程的数量,默认为10个
-o 指定配置文件,默认使用confxml.xml文件
-m 单独启用其他模块,使用 -m socket 开启socket模块 (refreshCDN、http)
参考文件
https://blog.csdn.net/woshiji594167/article/details/83988519
https://blog.csdn.net/xuxile/article/details/54022313