Sersync實現觸發式檔案同步

來源:互聯網
上載者:User

標籤:blog   http   ar   io   os   使用   sp   for   strong   

序言:如果我們後端有多台網站伺服器或者檔案伺服器,而且沒有好的檔案同步機制,那麼當我們升級程式或者更新檔案的時候,就需要每台伺服器或者目錄都要更新,這樣很容易出問題,並很容易導致兩邊的檔案不一致,從而出現很多莫民其妙的問題。因此我們需要使用好的檔案同步方式來實現幾個後端伺服器檔案的同步,目前廣泛採用的方案是使用rsync+inotify的方式來實現檔案的觸發更新。原理是採用inotify來對檔案進行監控,當監控到檔案有檔案發生改變的時候,就會調用rsync實現觸發式即時同步!本文就來詳細介紹金山的一個居於inotify+rsync進行二次開發實現檔案同步的小工具sersync,能夠很方便的實現檔案觸發式同步

一、   inotify簡介

Inotify 是基於inode層級的檔案系統監控技術,是一種強大的、細粒度的、非同步機制,它滿足各種各樣的檔案監控需要,不僅限於安全和效能,核心要求2.6.13以上,inotify能監控非常多的檔案系統事件,通過監控這些事件來監控檔案是否發生變更,然後通過rsync來更新發生變更的檔案,Inotify 可以監視的檔案系統事件包括:

  • IN_ACCESS,即檔案被訪問
  • IN_MODIFY,檔案被 write
  • IN_ATTRIB,檔案屬性被修改,如 chmod、chown、touch 等
  • IN_CLOSE_WRITE,可寫檔案被 close
  • IN_CLOSE_NOWRITE,不可寫檔案被 close
  • IN_OPEN,檔案被 open
  • IN_MOVED_FROM,檔案被移走,如 mv
  • IN_MOVED_TO,檔案被移來,如 mv、cp
  • IN_CREATE,建立新檔案
  • IN_DELETE,檔案被刪除,如 rm
  • IN_DELETE_SELF,自刪除,即一個可執行檔在執行時刪除自己
  • IN_MOVE_SELF,自移動,即一個可執行檔在執行時移動自己
  • IN_UNMOUNT,宿主檔案系統被 umount
  • IN_CLOSE,檔案被關閉,等同於(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
  • IN_MOVE,檔案被移動,等同於(IN_MOVED_FROM | IN_MOVED_TO)

備忘:上面的檔案也包括目錄。

   

二、   Rsync簡介

 rsync,remote synchronize顧名思意就知道它是一款實現遠程同步功能的軟體,它在同步檔案的同時,可以保持原來檔案的許可權、時間、軟永久連結等附加資訊。rsync是用 “rsync 演算法”提供了一個客戶機和遠程檔案伺服器的檔案同步的快速方法,而且可以通過ssh方式來傳輸檔案,這樣其保密性也非常好,另外它還是免費的軟體。

rsync 包括如下的一些特性:

  • 能更新整個目錄和樹和檔案系統;
  • 有選擇性的保持符號鏈鏈、永久連結、檔案屬於、許可權、裝置以及時間等;
  • 對於安裝來說,無任何特殊許可權要求;
  • 對於多個檔案來說,內部流水線減少檔案等待的延時;
  • 能用rsh、ssh 或直接連接埠做為傳輸入連接埠;
  • 支援匿名rsync 同步檔案,是理想的鏡像工具;

 

三、   sersync簡介

sersync利用inotify與rsync對伺服器進行即時同步,其中inotify用於監控檔案系統事件,rsync是目前廣泛使用的同步演算法,其優點是只對檔案不同的部分進行操作,所以其優勢大大超過使用掛接檔案系統的方式進行鏡像同步。由金山的周洋開發完成,是目前使用較多的檔案同步工具之一。該工具和其他的工具相比有如下優點:

  • sersync是使用c++編寫,由於只同步發生更改的檔案,因此比其他同步工具更節約時間、頻寬;
  • 安裝方便、配置簡單;
  • 使用多線程進行同步,能夠保證多個伺服器即時保持同步狀態;
  • 內建出錯處理機制,通過失敗隊列對出錯的檔案重新出錯,如果仍舊失敗,則每10個小時對同步失敗的檔案重新同步;
  • 內建crontab功能,只需在xml設定檔中開啟,即可按您的要求,隔一段時間整體同步一次;
  • 內建socket與http協議擴充,你可以方便的進行二次開發;

 

 

四、   sersync實現觸發式檔案同步實戰

  1. 1.  伺服器檔案同步圖
  1. 2.  從伺服器的安裝配置

a)  安裝rsync

yum -y install rsync

b)  配置rsync

Rsync設定檔的配置

cat /etc/rsyncd.conf

# Rsync configuration file

uid = root

gid = root

port = 873

max connections = 20000

use chroot = yes

timeout = 200

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log format = %t %a %m %f %b

auth users = root

secrets file = /etc/rsyncd.secret

 

[test]

path = /data/test/

comment = "test directory file"

list = yes

read only = no

ignore errors = yes

hosts allow = 192.168.3.205

hosts deny = *

 

Rsync密碼檔案的配置

cat /etc/rsyncd.secret

root:[email protected]#$

chmod 600 /etc/rsyncd.secret

需要設定密碼檔案為600許可權,不然同步的時候會報password file must not be other-accessible錯誤!

 

c)  Rsync的啟動

Rsync的啟動有兩種方式,兩種方式哪一種都可以,讀者可以自行選擇:

一種是採用daemon的方式啟動:

/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

另外一種是xinetd整合啟動方式:

chkconfig xined on

chkconfig rsync on

/etc/init.d/xinetd start

 

 

d)  驗證rsync是否啟動

查看進程和連接埠是否存在

[[email protected]_machine test]# ps aux | grep rsync

root      3275  0.0  0.0  65516   472 ?        Ss   13:44   0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

root      3306  0.0  0.1  71812   936 ?        S    14:49   0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

[[email protected]_machine test]# netstat -lnp | grep rsync

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      3275/rsync         

tcp        0      0 :::873                      :::*                        LISTEN      3275/rsync

 

  1. 3. 主伺服器的安裝配置

u 安裝rsync

yum -y install rsync

 

u 安裝sersync

下載安裝檔案

wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz

解壓並拷貝到安裝目錄

tar xzvf sersync2.5_64bit_binary_stable_final.tar.gz

mv GNU-Linux-x86 /usr/local/sersync

 

u 配置sersync

Sersync的配置

cat confxml.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"/>

    <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="true"/>

        <modify start="true"/>

    </inotify>

 

    <sersync>

        <localpath watch="/data/test">

       <!—-設定監控的目錄-->

            <remote ip="192.168.3.203" name="test"/>

           <!—設定從伺服器的IP-->

            <!--<remote ip="192.168.8.39" name="tongbu"/>-->

            <!--<remote ip="192.168.8.40" name="tongbu"/>-->

        </localpath>

        <rsync>

            <commonParams params="-artuz"/>

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

              <!—-設定同步的使用者名稱和密碼檔案-->

            <userDefinedPort start="true" port="873"/><!-- port=874 -->

           <!—-設定rsync的連接埠,要和從那邊開啟的連接埠一致-->

            <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="300"><!--600mins-->

       <!—-設定300分鐘全部同步一次-->

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

 

</head>

 

密碼檔案的配置

cat /etc/rsyncd.secret

[email protected]#$

主伺服器的密碼設定檔不需要使用者,如果添加使用者的話同步的時候會報

rsync error: error starting client-server protocol (code 5) at main.c(1296) [sender=2.6.8]錯誤

 

u 啟動sersync

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

將上面的命令添加進/etc/rc.local,以後重啟系統以後才能正常同步

u 指令碼監控sersync

因為有的時候sersync指令碼會自動關掉,因此需要寫一個指令碼自動的去檢測該進程是否存在,不存在就啟動,指令碼內容如下:

cat /var/script/check_sersync.sh

#!/bin/bash

#Purpose: Check sersync whether it is alive

#Author: Carl Zhang

SERSYNC="/usr/local/sersync/sersync2"

CONF_FILE="/usr/local/sersync/confxml.xml"

STATUS=$(ps aux |grep ‘sersync2‘|grep -v ‘grep‘|wc -l)

if [ $STATUS -eq 0 ];

then

        $SERSYNC -d -r -o $CONF_FILE &

else

        exit 0;

fi

 

指令碼寫好以後,添加到計劃任務中去

*/5 * * * * /var/script/check_sersync.sh > /dev/null 2>&1

 

總結:通過以上幾步以後,你從伺服器的/data/test就能夠從主伺服器即時更新,升級的時候只升級主伺服器的檔案,從伺服器也會自動同步過去,減少你的工作量以及出錯的機率!

如果讀者想瞭解更多的資訊,請直接存取sersync開發人員的部落格,部落格裡有更詳細的說明!

http://blog.johntechinfo.com/sersyncguild

Sersync實現觸發式檔案同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.