基于sersync海量文件实时同步

 今天我们主要讲解海量文件实时同步,最近涉及到数百万图片数据迁移,为了使图片数据快速迁移,并保证数据数据的一致性,无缝切换。尝试了多种方案。

方案1:rsync+inotify同步,最先想到的是此方案,以前在多台服务器间的做代码同步就常用此方法,因为代码文件数并不多。现在用在海量文件同步时,其缺陷也暴露出来,分析原理后,我们知道,每次新增数据都会做一次全量同步,在数据量不大的情况下同步挺快,但当文件数达到数百万就相当慢,每次对比整个目录的数量庞大的文件;第二,inotify监听的事件也非常多,创建一个文件会产生open、modify、close_write和close事件,这样就触发4次全量同步,恐怖至极,因此此种方案最终被我放弃。 但对于同步代码这种文件数不多也是极快,特分享本如下:


#!/bin/bash

host02=10.104.162.233  #需要同步的目标主机2

#本地监听目录

src="/usr/local/apache-tomcat-6.0.39/webapps/"

 #同步的的rsync服务的模块名

dst="/usr/local/apache-tomcat-6.0.39/webapps/"       

 #rsync服务端认证用户

user=root      

rsync_passfile=/etc/rsyncd.pass   

inotify_home=/usr/local/inotify    

records="/scripts/logs/records.txt"

#judge

if [ ! -e "$src" ] \

|| [ ! -e "${rsync_passfile}" ] \

|| [ ! -e "${inotify_home}/bin/inotifywait" ] \

|| [ ! -e "/usr/bin/rsync" ];

then

echo "Check File and Folder"

exit 9

fi

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e'  -e close_write,delete,create,attrib $src >>$records &

while true

do

        if [ -s $records ];then

                echo "-------$now_time--------" >>/var/log/rsync.log

                rsync  -avzc --exclude-from=/scripts/exclude.conf --delete $src $user@$host02:$dst >>/var/log/rsync.log

                if [ $? -ne 0 ];then

                        now_time=$(date "+%Y-%m-%d %H:%M:%S")

                        context="$now_time测试1同步到测试2失败"

                        recipients="[email protected]"

                        echo $context |mailx -s "rsync is error" $recipients

                fi

                cat /dev/null >/scripts/logs/records.txt

                #防止清空后,遗漏事件,再同步一次

                rsync  -avzc --exclude-from=/scripts/exclude.conf --delete $src $user@$host02:$dst >>/var/log/rsync.log

        else

                # 让循环休息1s,利于cpu

                sleep 1
        fi
done
exit 0

接着尝试了方案2:sersync,sersync由金山的一位同学开发,实际上是整合了在inotify和rsync上做了二次开发,对监听事件进行了过滤,并且同步时只同步单个文件或单个目录,对于数百万的文件或目录比较多的的情况,只用同步单个子目录而不是整个目录全部同步,并且支持多线程同步,也就是说可同时同步多个子目录,这样同步效率大大提高,并且达到实时同步,而不像方案1中每一次全量同步没完成时,新增文件也没同步,只能等下一次同步,没达到实时同步的效果。

海量文件同步推荐sersync

sersync下载

wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

2.配置sersync

<?xml version="1.0" encoding="ISO-8859-1"?>

<head version="2.5">

<host hostip="localhost" port="8008"></host>

<debug start="start"/>

<fileSystem xfs="false"/>

<filter start="true">

    <exclude expression="^.glusterfs/*"></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/brick1/Data">

        <remote ip="10.100.21.19" name="web_tomcat"/>

    </localpath>

    <rsync>

        <commonParams params="-artuz"/>

        <auth start="true" users="root" passwordfile="/etc/rsyncd.pass"/>

        <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-->

    <crontab start="true" schedule="600"><!--600mins-->

        <crontabfilter start="true">

            <exclude expression=".glusterfs/*"></exclude>

        </crontabfilter>

    </crontab>

    <plugin start="false" name="command"/>

</sersync>

</head>

主要改下以上几项即可,.glusterfs是过滤的文件,此方法只能用rsync的daemon模式,也就是目标机得配置rsync daemon,不能rsync ssh方式。crontab定时任务每10小时,全量同步一次,为防止文件遗漏。

3.启动sersync

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

总结:经过实际检验,sersync的方式对海量文件同步速度快, 对我数百万的图片数据迁移,做到了实时同步,然后切换流量,几十G的数据无缝迁移完成。

猜你喜欢

转载自blog.51cto.com/jin544642965/2339314