前言
起初在文件量小大、而且对实时性要求不高的时候,单独的 rsync 可以很好的满足需求。随便数据量的增大,对数据的实时性提出的新要求,开始全面采用 sersync 来实时向服务器的同步。
遇到的问题
- 当实时同步生效后,如果使用winscp软件对主同步服务器上线文件时,被同步后的文件比实际文件小,可能只有64k的情况(猜测可能是winscp文件没有上传完成时已经生成了一个新文件被同步了)
- 建议使用xftp进行上传
环境配置
系统:CentOS 6.5 x64 
主推送端:sersync   192.168.1.10 
节点接收端: rsync   192.168.1.11
配置节点接收端,数据同步需要开启 rsync
- 安装rsync 
 - yum install rsync -y
- 配置/etc/rsyncd.conf (线上使用时,注意删除注释) 
uid=root  
gid=root  
max connections=36000  
use chroot=no  
log file=/var/log/rsyncd.log  
pid file=/var/run/rsyncd.pid  
lock file=/var/run/rsyncd.lock
[data]                                                # rsync模块名,推送端需要指定
path=/data/                                           # 同步的路径  
comment  = xoyo video files  
ignore errors = yes  
read only = no  
hosts allow =  192.168.1.10                           #允许推送端IP  
hosts deny = *  
secrets file = /etc/rsync.pass                        # 密码认证文件,必须为600权限,否则rsync传输会报错  
- 配置认证文件
echo "leoiceo:b38df9c431" > /etc/rsync.pass  
chmod 600 /etc/rsync.pass  
- 运行rsync守护进程 
 rsync --daemon --port=9999 -4
- 加入开机启动 
 echo "rsync --daemon --port=9999 -4" >> /etc/rc.local
配置主推送端
- 下载编译好的sersync二进制包,根据系统平台选择下载 
 sersync2.532bit
 sersync2.564bit
- 解压即可以使用 
 - tar zxf sersync2.5_64bit_binary_stable_final.tar.gz -C /opt
 - cd GNU-Linux-x86
 有两个文件 confxml.xml sersync2 ,其中 confxml.xml 是默认的配置文件
- 配置sersync 
 1.创建密码文件
 - echo "b38df9c431" > /etc/rsync.pass
 - chmod 600 /etc/rsync.pass
 2.配置confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>  
<head version="2.5">  
    # 设置本地IP和端口
    <host hostip="localhost" port="8008"></host>
    # 是否开启debug模式
    <debug start="false"/>
    # 开启xfs文件系统
    <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="false"/>               #创建文件
        <closeWrite start="true"/>                #关闭和写
        <moveFrom start="true"/>                  #移入
        <moveTo start="true"/>                    #移出
        <attrib start="false"/>                   #修改属性
        <modify start="false"/>                   #修改文件
    </inotify>
    <sersync>
        # 本地同步的目录路径
        <localpath watch="/data/">
            # 远程IP和rsync模块名  
            <remote ip="192.168.1.11" name="data"/>
        </localpath>
        <rsync>
            # rsync指令参数 
            <commonParams params="-aprtuz"/>
            # rsync同步认证
            <auth start="false" users="leoiceo" passwordfile="/etc/rsync.pass"/>
            # 设置rsync远程服务端口,远程非默认端口则需打开自定义
            <userDefinedPort start="true" port="9999"/><!-- port=874 -->
            # 设置超时时间
            <timeout start="false" time="100"/><!-- timeout=100 -->
            # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
            # 可以参考(http://blog.imdst.com/rsyncssh-pei-zhi-wu-mi-ma-tong-bu/)
            <ssh start="false"/>
        </rsync>
        # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        # 设置rsync+crontab定时传输,默认关闭
        <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>  
- 启动sersync 
 - sersync2 -r -d -o /opt/GNU-Linux-x86/confxml.xml
- 验证是否成功同步 
 在推送端创建一个临时文件test,然后检查节点端是否也同步生成了一个test
- 重启sersync 
 - killall sersync2 && sersync2 -r -d -o /opt/GNU-Linux-x86/confxml.xml
定时检查sersync存活状态脚本
#!/bin/bash
# usage:  leoiceo
#       1.Copy Shell To SersyncPath
#       chmod +x checkrsync.sh
#       2.Add Crontab
#       */5 * * * * [sersyncPath]/checkrsync.sh
logfile='/opt/GNU-Linux-x86/checksersync.log';
function sersync_is_running(){  
        threadnum=`ps aux|grep sersync2|grep -v grep -wc`;
        if [ "$threadnum" -eq '0' ];then
                echo '0';
        else
                echo '1';
        fi
        return;
}
function current_time(){  
        if [ -z "$1" ];then
                format="%Y-%m-%d %H:%M:%S%Z";
        else
                format=$1;
        fi
        echo `date +"$format"`;
        return;
}
function logtofile(){  
        echo $(current_time) $2>>$1;
}
function sersync_restart(){  
        /opt/GNU-Linux-x86/sersync2 -r -d -o /opt/GNU-Linux-x86/confxml.xml >/dev/null 2>&1;    
        sleep 3;
        threadnum=$(sersync_is_running);
        if [ $threadnum -eq '0' ]; then
                echo "0";
        else
                echo '1';
        fi
        return;
}
isrunning=$(sersync_is_running);
if [ "$isrunning" -eq '0' ];then  
        logtofile $logfile "sersync service was died.";
        restart=$(sersync_restart);
        if [ $restart -eq '0' ];then
                logtofile $logfile "sersync service restart failed.";
        else
                logtofile $logfile "sersync service restart success.";
        fi
else  
        logtofile $logfile "sersync service is running.";
fi
exit 0;  
 Linux运维笔记|自动化运维攻城狮
 Linux运维笔记|自动化运维攻城狮


 
