天天看點

rsync基本使用總結 三rsync使用總結

三rsync使用總結

3.1Rsync介紹:

1、定義:是開源的、快速的、多功能的、全備份、增量(選擇)備份的本地或遠端的資料同步的備份工具。該軟體适用多種作業系統。

2、遠端拷貝:相當于ssh帶的scp指令,但是scp每次都是全量拷貝。Rsync可以是增量拷貝。

Centos5,rsync2.x比對方法是,把所有的問題比對一邊,然後進行同步。

Centos6,rsync3.x 可以實作增量比對。

3.2Rsync在用戶端的作用比較:

遠端拷貝:scp

本地拷貝:相當于cp覆寫備份

删除功能:rm

可以是一個網絡服務(Port  socket)

rsync的三種特性。通過ssh遠端、通過用戶端操作備份到服務端、 rsync daemon。

3.3rsync用戶端服務的參數介紹:

Rsync主要是提供内網服務的

[[email protected] ~]# rpm -qa rsync 檢視軟體是否安裝。

rsync-3.0.6-12.el6.x86_64

Rsync用戶端參數介紹:

-z壓縮 --compress 傳輸時進行壓縮以提高傳輸效率,--compress-level=NUM可以按級别壓縮,

-a --archive 表示按遞歸方式來傳輸檔案,保持檔案所有屬性。等價于-rtopgDl,

-v顯示資訊輸出過程、         

-t, --times保持檔案時間資訊

-o, --ower 保持檔案屬性資訊

 -P--progress 顯示同步的過程及傳輸時的進度資訊

-D --device 保持裝置檔案屬性

-r, --recursive 對子目錄遞歸模式,即目錄下所有目錄都同樣傳輸,注意小寫r

-e,--rsh=COMMAND 使用信道協定,指定替代rsh的shell程式

例如:ssh

—exclude=PATTERN 指定排除不需要傳輸的檔案模式

—exclude-from=file(檔案名所在的目錄檔案)

—bwlimit=RATE     limitsocket  I/O  bandwidth

--files-from= 指定同步清單檔案中的目錄或者是指定的檔案

--delete:參數适用于增量備份,适用于不同檔案或者目錄進行比對然後備份

Avz相當于vzrtopgDl  生産參數:-avz或者是-vzrtopg

rsync同步指定目錄

rsync使用–include參數與–exclude參數來實作同步指定目錄,并且–exclude目錄放在後面。

3.4rsync軟體服務的工作模式

第一種工作模式:本地單個伺服器上不同路徑檔案或目錄下的資料的備份

示範:相當于cp

[[email protected] opt]# rsync -avz /opt/test01/opt/test02

sending incremental file list

test01

sent 75 bytes received 31 bytes  212.00 bytes/sec

total size is 4 speedup is 0.04

 [[email protected] opt]# cat test0*

234

234

[[email protected] opt]# rsync -avz /opt/test02 /tmp/

sending incremental file list

test02

sent 75 bytes received 31 bytes  212.00bytes/sec

total size is 4 speedup is 0.04

[[email protected] opt]# cat /tmp/test02

234

第二種工作模式:不同伺服器之間資料進行傳輸

示範:

  Access viaremote shell:      遠端            本地

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

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

                            本地         遠端

1.借助ssh通道從遠端主機拉取資料執行個體:

rsync -avzP -e 'ssh -p 52113'[email protected]:/tmp/  /tmp

文法說明:

1、-avz相當于-vzrtopgDl,表示同步時檔案戶目錄屬性不變,

2、-P顯示同步的過程,可以用--progress替換

3、-e’ssh -p 52113’ 表示通過ssh傳輸資料,-p22可以省略

4、[email protected]:/tmp/ 遠端主機的系統使用者,位址,路徑

5、/tmp本地的路徑

注意tmp後面的/,否則把tmp目錄及tmp目錄下的所有東西都拉到本地/tmp下了

示範:推送,把本地的目錄檔案推送到遠端伺服器指定的目錄下

[r[email protected] opt]# ifconfig eth0|awk -F '[ :]+''NR==2{print $4}'

10.0.0.11

[[email protected] opt]# ll /opt/

total 12

drwxr-xr-x. 2 root root 4096 Mar 26  2015 rh

-rw-r--r--. 1 root root    4 Mar 12 18:04 test01

-rw-r--r--. 1 root root    4 Mar 12 18:04 test02

把本地目錄/opt/下檔案拉到遠端伺服器/opt/下

[[email protected] opt]# rsync -avz -e "ssh -p52113" /opt/ [email protected]:/opt/

[email protected]'s password:

sending incremental file list

test01

test02

sent 159 bytes received 51 bytes  60.00 bytes/sec

total size is 8 speedup is 0.04

[[email protected] opt]# ifconfig eth0|awk -F '[:]+' 'NR==2{print $4}'

10.0.0.10

[[email protected] opt]# ls

rh

[[email protected] opt]# ll

total 12

drwxr-xr-x. 2 root root 4096 Mar 26  2015 rh

-rw-r--r--. 1 root root    4 Mar 12 18:04 test01

-rw-r--r--. 1 root root    4 Mar 12 18:04 test02

把遠端伺服器上的目錄檔案拉到本地伺服器的/opt/目錄下

[[email protected] opt]# ls

[[email protected] opt]# rsync -avz -e "ssh -p52113" [email protected]:/opt/ /opt/(顯示傳輸的結果)

[[email protected] opt]# rsync -azP -e "ssh -p52113" [email protected]:/opt/ /opt/(顯示資訊同步的過程及傳輸時的進度資訊)

[email protected]'s password:

receiving incremental file list

./

test01

test02

opt/

opt/test01

opt/test02

opt/rh/

rh/

sent 102 bytes received 334 bytes  34.88bytes/sec

total size is 16 speedup is 0.04

[[email protected] opt]# ls

opt  rh  test01 test02

[[email protected] opt]# pwd

/opt

以上例子都是增量選擇性備份,而不是全部覆寫。如果本地檔案獨有的,不會被從外地拉過來的檔案内容所替換。而且外部檔案獨有的也不會被推過去的本地檔案所覆寫。

第三種工作模式

  Access viarsync daemon:

        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

[[email protected] ~]# vi /etc/rsyncd.conf

#Rsync server

#Rsync server

#created by jianwei 15:01 2009-6-5

##rsyncd.conf start##

uid = rsync它是對共享目錄的權限/backup

gid = rsync

use chroot = no 程式出現問題就會開啟 開啟給個空目錄就行

max connections = 2000 用戶端連接配接數 可以同時連接配接

time out = 600逾時的參數,

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock  鎖檔案

log file = /var/run/rsyncd.log  日志,發現錯誤時,看此日志分析錯誤

ignore errors 忽略錯誤

read only = false可讀寫

list = false 不讓清單,不讓看服務端有什麼

hosts allow = 10.0.0.9/24 允許的網段,通過它,才可以連接配接通路伺服器的/backup下的資料

hosts deny = 10.0.0.0/3拒絕的網段

auth users = rsync_backup 認證使用者 ,虛拟使用者,和系統沒有關系

secrets file = /etc/rsync.password 存放使用者和密碼的檔案

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

[backup] 子產品

comment = backup server by jianwei 14:18 2012-1-13 注釋

path = /backup 共享的目錄,

rsync server 服務端配置步驟

1、vi /etc/rsyncd.conf

   cat/etc/rsyncd.conf

2、建立使用者和共享的目錄/backup

useradd rsync -s /sbin/nologin -M

id rsync

mkdir /backup

chown -R rsync /backup

3、建立使用者及密碼檔案

echo "rsync_backup:jianwei" >/etc/rsync.password

cat /etc/rsync.password

 ll/etc/rsync.password

chmod 600 /etc/rsync.password

4、rsync --daemon 啟動服務

ps -ef|grep rsync|grep -v grep   netstat -lntup|grep rsync  ss -lntup|grep rsync  lsof -i :873

5、加入開機自動挂載

echo " rsync --daemon">>/etc/rc.local

cat /etc/rc.local

注意:服務端隻能存放資料,相當于一個硬碟或U。它隻具有存放資料的功能,其他什麼都不能幹。

Rsync 用戶端配置(pull和push 都在用戶端操作)

1、建立密碼檔案

echo jianwei >/etc/rsync.password

cat /etc/rsync.password

chmod 600 /etc/rsync.password

 ls -l/etc/rsync.password

2、Rsync

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

rsync -avz /tmp/ r[email protected]::backup--password-file=/etc/rsync.password 

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

rsync -avz rsync_b[email protected]::backup /tmp/ --password-file=/etc/rsync.password

注意:用戶端可以向服務端存放資料(推push),也可以像服務端提取資料(拉pull)作用主要是服務端。

rsync -avz tongbu_[email protected]::mhttongbu1 /tmp/ --password-file=/etc/passwd.txt

rsync -avz /tmp/ [email protected]::mhttongbu1--password-file=/etc/passwd.txt

rsync -avz /tmp/ rsync://r[email protected]/backup--password-file=/etc/rsync.password 

3.5Rsync守護程序服務傳輸排錯思路

一、rsync服務端排錯思路

1、檢視rsync服務配置檔案路徑是否正确,正确的預設路徑為/etc/rsyncd.conf

2、檢視配置檔案裡hostallow,host deny,允許的ip網段是否是允許反問的ip網段

3、檢視配置檔案中path參數裡的路徑是否存在,權限是否正确,(正常應為配置檔案中的UID參數對應的屬主群組)

4、檢視rsync服務是否啟動。

5、檢視防火牆和seLinux是否開啟允許rsync服務通過,也可以考慮關閉

6、參看服務端rsync配置的密碼檔案是否為600的權限,密碼檔案格式是否正确,正确格式使用者名:密碼,檔案路徑和配置檔案裡的secrectfiles 參數對應。

如果是推送資料,要檢視一下,配置檔案rsyncd.conf中使用者是否對子產品下目錄有可讀寫權限。

二、rsync用戶端排錯思想:

1、檢視用戶端rsync配置檔案的密碼檔案是否為600的權限,密碼格式是否正确,注意需要有密碼,并且和服務端的密碼五一緻,

2、用telnet連接配接rsync伺服器ip位址873端口,檢視服務是否啟動(可測試服務端防火牆是否阻擋)。Telnet 10.0.1.41 873

3、用戶端執行指令時,rsync-avzP [email protected]::backup /tmp/ /tmp/ --password-file=/etc/rsync.password  此指令細節要記清楚。

3.6自我模拟排錯實踐:

1、密碼設定錯

[[email protected] tmp]# rsync -avz /tmp/rsync://rsy[email protected]/backup --password-file=/etc/rsync.password

@ERROR: auth failed on module backup

rsync error: error startingclient-server protocol (code 5) at main.c(1503) [sender=3.0.6]

這是因為密碼設定錯了,檢查一下用戶端和服務端兩端的密碼/etc/rsync.password檔案中密碼是否一緻。密碼檔案中,密碼是不允許有多餘空格的。

2、服務端的共享目錄被删除

[[email protected] tmp]# rsync -avz /tmp/rsync://rsy[email protected]/backup --password-file=/etc/rsync.password

@ERROR: chdir failed

rsync error: error starting client-server protocol(code 5) at main.c(1503) [sender=3.0.6]

出現這種錯誤說明服務端的共享目錄被删除了,是以在服務端把删除的共享目錄建立回來(但是共享目錄中的資料全部丢失,實際工作中切記不能删除服務端的共享目錄)

做法得有:mkdir /backup/ -p 隻做到這一步是不行的,還會報錯。

還必須得chown rsync.rsync /backup/才行。      

服務端必須得有這兩步:

[email protected] /]# mkdir backup

[[email protected] /]# chown -R rsync /backup/

 rsync -avz/tmp/ rsync://rs[email protected]/backup--password-file=/etc/rsync.password

sending incremental file list

./

rsync: failed to set times on "." (inbackup): Operation not permitted (1)

a.txt

b.txt

c.txt

stu1

test.log

rsync: recv_generator: mkdir ".ICE-unix"(in backup) failed: Permission denied (13)

*** Skipping any contents from this faileddirectory ***

rsync: mkstemp ".a.txt.KD6GT3" (inbackup) failed: Permission denied (13)

rsync: mkstemp ".b.txt.MBzoKP" (in backup)failed: Permission denied (13)

rsync: mkstemp ".c.txt.QKA7AB" (inbackup) failed: Permission denied (13)

rsync: mkstemp ".stu1.Eh9Qrn" (in backup)failed: Permission denied (13)

rsync: mkstemp ".stu2.epUBi9" (in backup)failed: Permission denied (13)

sent 741 bytes received 186 bytes  618.00bytes/sec

total size is 1952 speedup is 2.11

rsync error: some files/attrs were not transferred(see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

出現以下這種錯誤跟上面的是問題是一樣的,隻不過是把/etc/rsyncd.conf配置檔案中的

use chroot = no  改為了use chroot = yes

[[email protected] tmp]# rsync -avz  rsync://rsync_back[email protected]/backup  /tmp/ --password-file=/etc/rsync.password

@ERROR: chroot failed

rsync error: error startingclient-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

修改方法和上面的一樣

3、用戶端的/etc/rsync.password和服務端的/etc/rsync.password沒有任何聯系,隻要和用戶端rsync指令參數----password-file=/etc/rsync.password 中的路徑對應即可。

4、服務端和用戶端的/etc/rsync.password 密碼檔案權限不是600

[[email protected] tmp]# rsync -avz rsync://rsync_bac[email protected]/backup /tmp/ --password-file=/etc/rsync.password

password file must not be other-accessible

continuing without password file

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol(code 5) at main.c(1503) [receiver=3.0.6]

出現這種錯誤,原因是把服務端的/etc/rsync.password 密碼檔案權限給改了,不是600了(用戶端密碼檔案權限是600)。

  必須chmod  600  /etc/rsync.password 改回才行

[[email protected] tmp]#  rsync -avz rsync://rsync_bac[email protected]/backup /tmp/ --password-file=/etc/rsync.password

password file must not be other-accessible

continuing without password file

Password:  此處出現提醒輸入密碼(原因是用戶端的密碼檔案/etc/rsync.password

receiving incremental file list                             權限不是600)

           如果用戶端和服務端密碼都是600,就會自動完成資料的提取和備份

./         

a.txt

b.txt

c.txt

stu1

stu2

stu3

stu4

stu5

test.log

sent 275 bytes received 631 bytes  201.33bytes/sec

total size is 3233 speedup is 3.57

5、用戶端一切問題都正常但是配置檔案中的path = /backup給#注釋掉了

  但是服務端[[email protected] backup]# vi /etc/rsyncd.conf 配置檔案稍作改動,用戶端正常輸出,但是不顯示輸出的檔案,而且服務端的資料沒有被提取到用戶端/tmp/下

操作過程:

[[email protected] tmp]# rsync -avz  rsync://rsync_back[email protected]/backup  /tmp/ --password-file=/etc/rsync.password

receiving incremental file list

./

sent 68 bytes received 161 bytes  152.67bytes/sec

total size is 21536 speedup is 94.04

[[email protected] backup]# ls 服務端

a.txt b.txt  c.txt  stu1 stu2  stu3  stu4 stu5  test.log

[[email protected] tmp]# ls用戶端

test.log

原因是:把配置檔案中的path = /backup給#注釋掉了,系統找不到傳輸資料的通道了,改回原來的就行

6.觀察日志

[[email protected] backup]# cat/var/log/rsyncd.log 定時任務的服務日志

3.7Rsync優點:

1、增量備份同步,支援socket(daemon),集中備份(支援推拉,都是以用戶端為參照物)

2、遠端shell通道模式,還可以加密傳輸(ssh),socket(daemon)需要加密傳輸,還可以利用vpn服務或者ipsec服務

Rsync缺點:

1、大量小檔案同步的時候,比對時間較長,有時候,rsync程序會停止。

解決方法:a打包同步,b.drbd(檔案系統同步複制block)

2、同步大檔案,10G這樣的檔案有時也會有問題,中斷。為完整同步前,是影藏檔案,同步完成改為正常檔案。

3、Rsync -avz--delete /tmp/ ........無差異同步

推:本地有,遠端就有,本地沒有,删除遠端獨有的

危險,把服務端備份的資料幹掉,盡量不要用。如果要用,就先備份遠端的資料再推送。

拉:遠端的資料會替換本地的資料。把本地獨有的資料删除。要用就先備份本地資料。

 本文轉自 wjw555 51CTO部落格,原文連結:http://blog.51cto.com/wujianwei/1962467