rsync + inotify

是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

[root@MinR ~]# rsync -a /tmp/yum.repo/ 172.16.249.73:/tmp 
[email protected]'s
 password:

在172.16.249.77上拉取172.16.249.73上的文件

[root@MinR ~]# rsync -r -v -progress -e ssh 172.16.249.73:/etc/issue /tmp 
[email protected]'s password: receiving incremental file list 
issue
sent 30 bytes  received 126 bytes  24.00 bytes/sec total size is 48  
speedup is 0.31 [root@MinR ~]# 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

 

創建目錄[root@MinR /]# mkdir data

重啓xinetd確保xinetd監控在tcp/873端口下

[root@MinR ~]# service xinetd restart 
Stopping 
xinetd:                                           [  OK  ] 
Starting 
xinetd:                                           [  OK  ]

在客戶端172.16.249.73向服務器端push和pull文件

[root@localhost ~]# rsync -a /etc/issue 172.16.249.77::gongxiang
[root@localhost ~]# 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個字符

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

推送

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

 

拉取

[root@localhost tmp]# rsync -a [email protected]::gongxiang /tmp 
Password: 
[root@localhost 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

[root@MinR ~]# tar xf inotify-tools-3.14.tar.gz 
[root@MinR ~]# cd 
inotify-tools-3.14 
[root@MinR 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
[root@MinR 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下 


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