rsync + inotify

來源:互聯網
上載者:User

標籤:rsync

是linux系統下的資料鏡像備份工具——remote sync

特點:
        1、可以鏡像儲存整個分類樹或檔案系統;
        2、較高的資料轉送效率;
        3、可以藉助於ssh實現安全資料轉送;
        4、支援匿名傳輸;

rsync命令的選項:
        -n: 同步測試,不執行真正的同步過程;
        -v: 詳細輸出模式
        -q: 靜默模式
        -c: checksum,開啟校正功能
        -r: 遞迴複製

 

        -a: 歸檔,保留檔案的原有屬性;
        -p: 保留檔案的許可權;
        -t: 保留檔案的時間戳記;
        -l: 保留符號連結
        -g: 保留屬組
        -o: 保留屬主
        -D:保留裝置檔案

        -e ssh: 使用ssh作為傳輸承載;
        -z: 壓縮後傳輸;

        --progress: 顯示進度條
        --stats: 顯示如何執行壓縮和傳輸

在172.16.249.77上推送檔案至172.16.249.73

[[email protected] ~]# rsync -a /tmp/yum.repo/ 172.16.249.73:/tmp [email protected]‘s password:

在172.16.249.77上拉取172.16.249.73上的檔案

[[email protected] ~]# rsync -r -v -progress -e ssh 172.16.249.73:/etc/issue /tmp [email protected]‘s password: receiving incremental file list issuesent 30 bytes  received 126 bytes  24.00 bytes/sec total size is 48  speedup is 0.31 [[email protected] ~]# ls /tmp issue

    

 

 

 

rsync的服務模式

rsync是瞬時進程,需要超級守護xitend進程的監聽,故先安裝xitend超級守護進程

# yum install xinted –y# vim /etc/xinetd.d/rsync   把disable =yes   改為 no即可開啟rsync的伺服器模式

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

 

為rsync提供設定檔 /etc/rsyncd.conf

                # Global Settings  全域配置段
            uid = nobody
            gid = nobody
            use chroot = no
            max connections = 10
            strict modes = yes
            pid file = /var/run/rsyncd.pid
            log file = /var/log/rsyncd.log

            # Directory to be synced    共用配置段
            [gongxiang]
            path = /data
            ignore errors = yes
            read only = no
            write only = no
            hosts allow = 172.16.0.0/16
            hosts deny = *
            list = false
            uid = root
            gid = root

 

建立目錄[[email protected] /]# mkdir data

重啟xinetd確保xinetd監控在tcp/873連接埠下

[[email protected] ~]# service xinetd restart Stopping xinetd:                                           [  OK  ] Starting xinetd:                                           [  OK  ]

在用戶端172.16.249.73向伺服器端push和pull檔案

[[email protected] ~]# rsync -a /etc/issue 172.16.249.77::gongxiang[[email protected] ~]# rsync -a 172.16.249.77::gongxiang /tmp

 

 


      服務端啟用使用者認證的功能

           在前面rsync的設定檔 /etc/rsyncd.conf的共用定義處加配置:

           auth users = tt1,tt2
           secrets file = /etc/rsyncd.passwd

           

建立密碼檔案/etc/rsyncd.passwd
            tt1:mima1

            tt2:mima2

注意:此檔案不能允許其它使用者有存取權限,即600許可權,且密碼不能超過8個字元

[[email protected] data]# chmod 600 /etc/rsyncd.passwd [[email protected] data]# ll /etc/rsyncd.passwd -rw-------. 1 root root 35 Aug 21 22:45 /etc/rsyncd.passwd

推送

[[email protected] tmp]# rsync -a /etc/issue [email protected]::gongxiang Password:

 

拉取

[[email protected] tmp]# rsync -a [email protected]::gongxiang /tmp Password: [[email protected] tmp]# ls issue

 

 

inotify-tools 是為linux下inotify檔案監控工具提供的一套c的開發介面庫函數,同時還提供了一系列的命令列工具,這些工具可以用來監控檔案系統的事件。 inotify-tools是用c編寫的,除了要求核心支援inotify外,不依賴於其他。inotify-tools提供兩種工具,一是inotifywait,它是用來監控檔案或目錄的變化,二是inotifywatch,它是用來統計檔案系統訪問的次數。

 

文法:
inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ] [ ... ]
參數:
-h,–help
輸出協助資訊
@
排除不需要監視的檔案,可以是相對路徑,也可以是絕對路徑。
–fromfile
從檔案讀取需要監視的檔案或排除的檔案,一個檔案一行,排除的檔案以@開頭。
-m, –monitor
接收到一個事情而不退出,無限期地執行。預設的行為是接收到一個事情後立即退出。
-d, –daemon
跟–monitor一樣,除了是在後台運行,需要指定–outfile把事情輸出到一個檔案。也意味著使用了–syslog。
-o, –outfile
輸出事情到一個檔案而不是標準輸出。
-s, –syslog
輸出錯誤資訊到系統日誌
-r, –recursive
監視一個目錄下的所有子目錄。
-q, –quiet
指定一次,不會輸出詳細資料,指定二次,除了致命錯誤,不會輸出任何資訊。
–exclude
正則匹配需要排除的檔案,大小寫敏感。
–excludei
正則匹配需要排除的檔案,忽略大小寫。
-t , –timeout
設定逾時時間,如果為0,則無限期地執行下去。
-e , –event
指定監視的事件。
-c, –csv
輸出csv格式。
–timefmt
指定時間格式,用於–format選項中的%T格式。
–format
指定輸出格式。
%w 表示發生事件的目錄
%f 表示發生事件的檔案
%e 表示發生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定義的時間格式

inotifywatch

文法:
inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]
參數:
-h, –help
輸出協助資訊
-v, –verbose
輸出詳細資料
@
排除不需要監視的檔案,可以是相對路徑,也可以是絕對路徑。
–fromfile
從檔案讀取需要監視的檔案或排除的檔案,一個檔案一行,排除的檔案以@開頭。
-z, –zero
輸出表格的行和列,即使元素為空白
–exclude
正則匹配需要排除的檔案,大小寫敏感。
–excludei
正則匹配需要排除的檔案,忽略大小寫。
-r, –recursive
監視一個目錄下的所有子目錄。
-t , –timeout
設定逾時時間
-e , –event
只監聽指定的事件。
-a , –ascending
以指定事件升序排列。
-d , –descending
以指定事件降序排列。

 

下載安裝inotify-tools

[[email protected] ~]# tar xf inotify-tools-3.14.tar.gz [[email protected] ~]# cd inotify-tools-3.14 [[email protected] inotify-tools-3.14]# ls

aclocal.m4  ChangeLog     config.h.in  configure     COPYING  INSTALL     libinotifytools  Makefile.am  man      NEWS    src
AUTHORS     config.guess  config.sub   configure.ac  depcomp  install-sh  ltmain.sh        Makefile.in  missing  README
[[email protected] inotify-tools-3.14]# ./configure && make && make install

編輯一個指令碼

# vim   jiankong.sh


#!/bin/bash
/usr/local/bin/inotifywait -rmq /data | while read file
  do
     echo "It is change"
     rsync -a /data/ 172.16.249.73:/tmp
  done

用以監控172.16.249.77的/data/下的檔案的變動情況,有變動就同步到172.16.249.73下 


rsync + inotify

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.