基於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的數據無縫遷移完成。

更多精彩,關注公衆號
基於sersync海量文件實時同步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章