之前的一篇中记录了rsync+inotify实现文件的实时传输,碍于inotifywait的bug问题,线上同步网站文件无法使用,所以再记录一下sersync的配置,留待使用时查看。
环境:centos6.8
IP及功能划分:
172.17.9.151(发布服务器)
172.17.9.150 (web服务器)
需求:发布服务器生成静态页面(wwwroot下),使用rsync+sersync进行实时同步,将文件实时同步到web服务器(www下)。
web服务器端(9.150)
1、关闭selinux、开放rsync的端口(默认873)这里不再进行赘述;
2、yum -y install rsync (若内网环境无法联网,使用光盘yum源);
3、新建rsyncd.conf配置文件(安装rsync时默认未创建)
[root@testPC www]# cat /etc/rsyncd.conf
# This configuration file for rsync daemon
#运行rsync的用户
gid = root
uid = root
#是否切换root权限
use chroot = no
#设置是否只读
read only = no
#最大连接数
max connections = 5
pid file = /var/run/rsyncd.pid
#定义验证文件路径及文件名
secrets file = /etc/rsyncd.password
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
#定义模块名
[backup]
#此模块对应的同步目录
path = /data/www
#不对模块下的列表进行列表显示
list=no
#忽略错误
ignore errors
#定义传输时使用的用户,同上面的secrets file定义的密码验证文件一块使用,实现无交互式传输
auth users = rsyncuser
4、创建验证文件并修改权限
echo "rsyncuser:12345" > /etc/rsyncd.password
chmod 600 /etc/rsyncd.password
5、使用守护进程的方式运行rsync
rsync --daemon
发布服务器(9.151)
1、yum -y install rsync
2、创建密码文件,不同于web服务器端的是,此passwod文件中只需要密码即可
echo "12345" > /etc/rsyncd.password
chmod 600 /etc/rsyncd.password
3、测试同步(将源目录wwwroot同步到目标服务器9.150的backup模块对应的目录下)
rsync -avH --port=873 --progress --delete wwwroot rsyncuser@172.17.9.150::backup --password-file=/etc/rsyncd.password
Sersync配置
4、下载sersync http://sersync.googlecode.com/files/sersync2.1_64bit_binary.tar.gz (这里需要翻墙的,没有的话只能通过别人的网盘下载,这里提供我自己的网盘分享地址:链接: https://pan.baidu.com/s/18qO9EMKyfbiasG-4AYoWEQ 提取码: k67u)
5、下载完成后上传到服务器,直接解压缩,移动到需要安装的目录,编辑配置文件
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/ /usr/local/sersync
vim /usr/local/sersync/conxml.xml
6、配置文件模版:
<?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>
7、启动sersync,最后上线加入开机启动即可。
.sersync2 -d -r -o /usr/local/sersync/confxml.xml
参数:
-d | 启用守护进程模式 |
-n | 指定开启守护线程的数量,默认为10个 |
-o | 指定配置文件,默认使用confxml.xml文件 |
-m | 单独启用其他模块,使用 -m socket 开启socket模块 (refreshCDN、http) |