rsync基本使用總結

rsync使用總結

3.1Rsync介紹:

1、定義:是開源的、快速的、多功能的、全備份、增量(選擇)備份的本地或遠程的數據同步的備份工具。該軟件適用多種操作系統。

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

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

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

 

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

遠程拷貝:scp

本地拷貝:相當於cp覆蓋備份

刪除功能:rm

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

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

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

Rsync主要是提供內網服務的

[root@wjw66 ~]# 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 使用信道協議,指定替代rshshell程序

例如: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

[root@client01 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

 [root@client01 opt]# cat test0*

234

234

[root@client01 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

[root@client01 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-essh -p 52113 表示通過ssh傳輸數據,-p22可以省略

4[email protected]:/tmp/ 遠程主機的系統用戶,地址,路徑

5/tmp本地的路徑

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

 

演示:推送,把本地的目錄文件推送到遠程服務器指定的目錄下

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

10.0.0.11

[root@client01 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/下

[root@client01 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

[root@zabbix-server opt]# ifconfig eth0|awk -F '[:]+' 'NR==2{print $4}'

10.0.0.10

[root@zabbix-server opt]# ls

rh

[root@zabbix-server 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/目錄下

[root@client01 opt]# ls

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

[root@client01 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

[root@client01 opt]# ls

opt  rh  test01 test02

[root@client01 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

 

 

[root@Backup ~]# 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 服務端配置步驟

1vi /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

4rsync --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 客戶端配置pullpush 都在客戶端操作)

1、創建密碼文件

echo jianwei >/etc/rsync.password

cat /etc/rsync.password

chmod 600 /etc/rsync.password

 ls -l/etc/rsync.password

2Rsync

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

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

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

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

 

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

 

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

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

rsync -avz /tmp/ rsync://[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、密碼設置錯

[root@nfs01 tmp]# rsync -avz /tmp/rsync://[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、服務端的共享目錄被刪除

[root@nfs01 tmp]# rsync -avz /tmp/rsync://[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/才行。      

服務端必須得有這兩步:

root@backup /]# mkdir backup

[root@backup /]# chown -R rsync /backup/

 

 rsync -avz/tmp/ rsync://[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

[root@nfs01 tmp]# rsync -avz  rsync://[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

[root@nfs01 tmp]# rsync -avz rsync://[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 改回才行

 

[root@nfs01 tmp]#  rsync -avz rsync://[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#註釋掉了

  但是服務端[root@backup backup]# vi /etc/rsyncd.conf 配置文件稍作改動,客戶端正常輸出,但是不顯示輸出的文件,而且服務端的數據沒有被提取到客戶端/tmp/下

操作過程:

[root@nfs01 tmp]# rsync -avz  rsync://[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

[root@backup backup]# ls 服務端

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

[root@nfs01 tmp]# ls客戶端

test.log

原因是:把配置文件中的path = /backup給#註釋掉了,系統找不到傳輸數據的通道了,改回原來的就行

 

6.觀察日誌

[root@backup backup]# cat/var/log/rsyncd.log 定時任務的服務日誌

 

3.7Rsync優點:

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

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

Rsync缺點:

1、大量小文件同步的時候,比對時間較長,有時候,rsync進程會停止。

解決方法:a打包同步,b.drbd(文件系統同步複製block)

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

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

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

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

拉:遠端的數據會替換本地的數據。把本地獨有的數據刪除。要用就先備份本地數據。


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