rsync數據同步工具

rsyncremote sync能夠實現本地和遠端數據的同步。基於rsync算法相似有提取特徵碼實現數據的增量備份。幫助文檔中的定義是rsync -- a fast, versatile, remote (and local) file-copying tool。linux

1、rsync的特性

  • 能夠鏡像保存整個目錄和文件系統算法

  • 安全可使用scp、ssh方式來傳輸文件數據安全性較高shell

  • 能夠比較容易的作到保持文件原來的權限、時間、軟硬連接等屬性vim

  • 文件支持匿名傳輸方便進行網站鏡像安全

  • 快速第一次同步時rsync會複製所有的內容但在下一次時只同步修改過的文件。rsync在傳輸數據過程當中能夠實行壓縮和解壓縮操做可使用更少的帶寬。bash

2、rsync的工做模式

第一種Local模式相似於本地cp,install命令。服務器

       使用格式 rsync [OPTION...] SRC... [DEST]併發

第二種Access via remote shell遠端shell模式相似於scp命令ssh

使用格式異步

    Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 本地向遠端推送文件

    Push: rsync [OPTION...] SRC... [USER@]HOST:DEST  本地向遠端來去文件

第三種列表模式

    使用格式rsync -n [-v] [USER@]HOST:DEST

第四種 Access via rsync daemon服務模式此時rsync工做位守護進程能接受到客戶端的同步請求。

使用格式以下

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

         rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

3、rsyn命令

常見的參數以下:


-v,--verbose 詳細的輸出模式
-q,--quiet 靜默模式
-c,--checksum 開啓校驗功能
-r,--recursive 遞歸複製
-a.--archive 歸檔保留文件的原有屬性。等價於 -rlptgoD
-l,--links 保留符號連接
-p,--perms 保留文件的權限
-t,--times 保留文件的時間戳
-g,-o              
--group,--owner
保留文件的屬主屬組
-D-devices --specials 保留設備文件
-e ssh 使用ssh做爲傳輸參數
-z,--compress 壓縮傳輸可使用--compress-level指定壓縮級別
--progress 顯示進度
--password-file=FILE   讀取密碼文件

--delete  參數的目的是在同步的時候刪除多餘的文件,只保留同步文件,其餘無關的文件,都刪除。

例如:rsync -a --delete /test1 /test2 ,test1是一個空文件目錄,在沒有同步以前,test2目錄裏有好多文件,可是同步以後,test2 此時就變爲空目錄了。

經常用於刪除大量文件,例如:咱們要刪除 百萬級 文件時,此時 rm 就不起做用了。此時,就會用到這種方式,並且所消耗的時間是能夠接受的。

4、服務模式示例

至於本地模式遠端shell模式和列表模式比較簡單這裏再也不詳述。但注意一點在遠端shell模式下若是SRC中要複製的目錄後有」/「此時會複製此目錄的全部文件不包括目錄自己若是沒有」/「則會複製目錄自己和此目錄下的全部文件。

下圖是對比效果

1

2

服務器模式此時rsync至關一個服務器。更簡單的來講此時至關於samba文件服務器功能上主要值文件共享和上傳有太大的差異配置文件有samba的也至關相似緣由是rsync項目是由samba項目維護的官方站點是rsync.samba,org)。

rsync服務默認是不提供配置文件的,須要本身建立。配置文件時在/etc/rsyncd.conf。配置以下

###本身手動建立 rsyncd.conf 文件
# Global Settings
uid = nobody
gid = root
use chroot = no # 是否啓用chroot的功能
max connection = 10 # 最大併發數
strict modes = yes # 是否啓用 UID 檢測
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log


[share]
path = /data
ignore errors = yes # 忽略錯誤
read only = no
write only = no
hosts allow = 192.168.1.0/24
uid = root 
gid = root


###建立必要文件 pid和log文件
touch /var/run/rsyncd.pid /var/log/rsyncd.log

########啓動服務
chkconfig rsync on
service xinetd start

結果

3

此時傳輸是匿名傳輸不進行用戶名認證

4

 

要想實現用戶認證非系統用戶須要修改配置文件。

# 在配置文件的對應共享目錄下添加
auth users= tom,jerry
secrets file = /etc/rsyncd.pass


# 提供用戶文件 /etc/rsyncd.pass格式以下
tom:guoting
jerry:guoting

chmod 600 /etc/rsyncd.pass

5

4、rsync+inotify實現數據的時時同步

一、inotify簡介

inotify是一種強大的、細粒度的、異步的文件系統事件監控機制linux內核從2.6.13版本起加入了對inotify的支持。經過inotify能夠監控文件系統中添加、刪除、修改、移動等各類細微事件利用這個內核接口第三方軟件能夠監控文件系統下文件的各類變化狀況inotify-tools就是這樣的一個第三方軟件。inotify-tools提供兩種工具一是inotifywait它是用來監控文件或目錄的變化二是inotifywatch它是用來統計文件系統訪問的次數。

二、inotify軟件的安裝

首先查看內核支持inotify的機制有對應的參數表示支持。

7

安裝inotify-tools軟件可使用源碼和rpm方式安裝epel源中。這裏使用rpm方式

yum install inotify-tools -y    

8

安裝完成後會提供inotifywait和inotifywatch 2個命令。

inotifywait命令介紹

inotifywait - wait for changes to files using inotify。使用inotify機制來等待文件的變化。

使用格式inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]

常見的選項以下

-m|--monitor

保持監聽

-r|--recursive

遞歸監聽

-q|--quiet

只打印events信息

-qq

不打印任何信息

--format <fmt>   

指定特定的格式

--timefmt <fmt>

指定時間格式

-e|--event <event1>

常見的監聽動做

access、modify、attrib、close_write、close_nowrite、close、open、moved_to   
moved_from、move、create、delete、delete_self、unmount    

三、inotify的配置實現

實驗拓撲圖以下實如今192.168.1.77上只要有文件指定文件改變就push到遠端rsync服務器端實現數據的時時同步。

6

配置步驟

####################在192.168.1.66#################################################
# 一、vim /etc/rsynd.conf,內容以下
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connection = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log


[share]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0/24
uid = root  
gid = root
auth users= tom,jerry
secrets file = /etc/rsyncd.pass

# 二、建立必要的文件
mkdir /data
touch /var/run/rsyncd.pid /var/log/rsyncd.log

# vim /etc/rsyncd.pass
tom:guoting
jerry:guoting

chmod 600 /etc/rsyncd.pass

# 三、啓動服務
chkconfig rsync on
service xinetd restart

##################################################################################


####################在192.168.1.99#################################################
# 一、vim /etc/rsynd.conf,內容以下
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connection = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log


[share]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0/24
uid = root  
gid = root
auth users= tom,jerry
secrets file = /etc/rsyncd.pass

# 二、建立必要的文件
mkdir /data
touch /var/run/rsyncd.pid /var/log/rsyncd.log

# vim /etc/rsyncd.pass
tom:guoting
jerry:guoting

chmod 600 /etc/rsyncd.pass

# 三、啓動服務
chkconfig rsync on
service xinetd restart

##################################################################################

####################在192.168.1.77#################################################

# 一、提供密碼文件和push目錄
echo "guoting" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
mkdir /mdata

# 二、安裝軟件
yum install inotify-tools -y

# 三、建立監聽腳本

# vim ~/rsyncmon.sh 內容以下
#!/bin/bash
#
passfile=/etc/rsync.pass
rsyncd1=192.168.1.66  
rsyncd2=192.168.1.99              
src=/mdata                       
dst=share                       
user=tom                    
  
#注意這裏的信息是會記錄到192.168.1.66和192.168.1.66服務器的日誌文件中
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src |while read line \
do 
    /usr/bin/rsync -az --delete --progress --timeout=120 --password-file=$passfile $src $user@$rsyncd1::$dst &>/dev/null
    /usr/bin/rsync -az --delete --progress --timeout=120 --password-file=$passfile $src $user@$rsyncd2::$dst &>/dev/null
  
done
########
chmod +x ~/rsyncmon.sh 

# 四、執行腳本
~/rsyncmon.sh  &

結果

192.168.1.77上建立一個文件

10

在192.168.1.66和192.168.1.99上查看

11

查看對應的日誌信息注意格式

12 

補充

一、若是想開機自動監聽echo "/root/rsyncmon.sh &" >> /etc/rc.d/rc.local

二、有關inotify的內核參數

查看系統默認參數值
sysctl -a | grep max_queued_events
結果是fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches
結果是fs.inotify.max_user_watches = 8192

sysctl -a | grep max_user_instances
結果是fs.inotify.max_user_instances = 128

參數說明
max_queued_events
inotify隊列最大長度若是值過小會出現"** Event Queue Overflow **"錯誤致使監控文件不許確

max_user_watches
要同步的文件包含多少目錄能夠用find /path -type d | wc -l 統計必須保證max_user_watches值大於統計結果這裏/path爲同步文件目錄

max_user_instances
每一個用戶建立inotify實例最大值

over.

相關文章
相關標籤/搜索