rsync 總結回顧

這篇文章是在windows live writer 上面寫的,排版有些很差,博友看的時候體諒下~~ 嘻嘻~linux

======================================================================================
rsync命令是初學者應該掌握的
首先,應該知道
rsync是類unix系統下的數據鏡像備份工具,從軟件的命名上就能夠看出來了——remote sync
那麼,它的特性有哪些呢?
1.能夠鏡像保存整個目錄樹和文件系統。
2.能夠很容易作到保持原來文件的權限、時間、軟硬連接等等。
3.無須特殊權限便可安裝。
4.快速:第一次同步時 rsync 會複製所有內容,但在下一次只傳輸修改過的文件。rsync 在傳輸數據的過程當中能夠實行壓縮及解壓縮操做,所以可使用更少的帶寬。
5.安全:可使用scp、ssh等方式來傳輸文件,固然也能夠經過直接的socket鏈接。
6.支持匿名傳輸,以方便進行網站鏡象。

牢記以上特性以後,咱們再來一步一步的剖析它……
windows

-----------------------------------------------------------------------------------------安全

第一,它可使用在本地單機環境當中。就至關於cp、mv、rm等命令同樣。
來看看實例:
bash

[ lsp@ 59CentOS ~ ]$ alias sl
alias sl='ls -l'
[ lsp@ 59CentOS ~ ]$sl dir1           -----》列出dir1目錄下的內容
total 12
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 c
[ lsp@ 59CentOS ~ ]$sl dir2           -----》列出dir2目錄下的內容
total 8
-rw-r--r-- 1 lsp lsp 0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp 0 Sep 27 03:07 f
[ lsp@ 59CentOS ~ ]$rsync -avz dir1 dir2   --->使dir1目錄總體複製到dir2目錄下面,至關於cp
sending incremental file list
dir1/
dir1/a
dir1/b
dir1/c
sent 185 bytes  received 73 bytes  516.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2            ---》再次列出dir2目錄下的內容。能夠發現,dir1目錄全都被cp過來了
total 16
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f

注意:若是目錄後面不加/ ,那麼就表示將整個目錄cp過去。服務器

[ lsp@ 59CentOS ~ ]$rsync -avz dir1/ dir2      ---》將dir1/目錄下的全部文件,複製到dir2目錄下。若是有相同的文件,則以dir1裏面的爲準。
sending incremental file list
./
a
b
c
sent 170 bytes  received 72 bytes  484.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 28
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f


注意:若是目錄後面加/ ,那麼就表示將目錄下的全部文件cp過去。
ssh

[ lsp@ 59CentOS ~ ]$rsync -avz dir1 dir2/dir3   ---》將dir1目錄,總體拷貝到dir3下面
sending incremental file list
created directory dir2/dir3
dir1/
dir1/a
dir1/b
dir1/c
sent 185 bytes  received 73 bytes  516.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 36
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
[ lsp@ 59CentOS ~ ]$sl dir2/dir3
total 8
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1

注意:個人dir2下面並無dir3目錄,它居然本身建立了一個。socket

[ lsp@ 59CentOS ~ ]$rsync -avz dir1/a dir2/i   --->將dir1/a文件,拷貝到dir2/目錄下,並重命名爲i文件,內容和a文件同樣。
sending incremental file list
a
sent 73 bytes  received 31 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 40
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 i
[ lsp@ 59CentOS ~ ]$echo "ooooooo">>dir1/a
[ lsp@ 59CentOS ~ ]$rsync -avz dir1/a dir2/p
sending incremental file list
a
sent 82 bytes  received 31 bytes  226.00 bytes/sec
total size is 8  speedup is 0.07
[ lsp@ 59CentOS ~ ]$sl dir2
total 52
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
-rw-r--r-- 1 lsp lsp    8 Sep 27 04:17 i
-rw-r--r-- 1 lsp lsp    8 Sep 27 04:17 p
[ lsp@ 59CentOS ~ ]$nl dir2/p
     1  ooooooo


全部的格式,都是以源目錄裏面的文件爲根源,若是目標目錄下沒有源目錄裏面的子目錄或者文件,則將鏡像出一份和源目錄相同的文件或者目錄;
若是目標目錄下有和源目錄下相同的文件,則以源目錄爲標準。多則刪,少則添。
tcp

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir3 dir2
sending incremental file list
dir3/
sent 40 bytes  received 16 bytes  112.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   └── dir3
└── dir3
4 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir3/ dir2
sending incremental file list
./
deleting dir3/
deleting 3.txt
deleting 2.txt
deleting 1.txt
sent 29 bytes  received 15 bytes  88.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
└── dir3
3 directories, 5 files

能夠看出來,若是想要利用長選項--delete,來刪除或者是鏡像文件,那必須後面是接的是文件。而不是目錄。ide

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir2/ dir1
sending incremental file list
./
deleting 5.txt
deleting 4.txt
1.txt
2.txt
3.txt
sent 171 bytes  received 72 bytes  486.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 6 files

這樣就能夠,由於你後面是文件。因此,這個命令就是在dir3下面鏡像/dri2下面的文件。若是dir3裏面有多餘的,則刪掉。工具

那接下來看看對於文件的內容會不會有變化。

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── dir2
│   └── 3.txt
└── dir3
3 directories, 4 files
[root@Jason64-18 lspgyy]# cat dir1/1.txt
/lspgyy/dir1
[root@Jason64-18 lspgyy]# cat dir2/3.txt
/lspgyy/dir2
/lspgyy/dir2
[root@Jason64-18 lspgyy]# rsync -avz --delete dir1/1.txt dir2/3.txt
sending incremental file list
1.txt
sent 83 bytes  received 31 bytes  228.00 bytes/sec
total size is 13  speedup is 0.11
[root@Jason64-18 lspgyy]# cat dir2/3.txt                  
/lspgyy/dir1

能夠看出來,dir2/3.txt文件裏面的內容已經被改成和dir1/1.txt的內容如出一轍了。

因此,能夠判定,rsync是鏡像目錄和文件,包括文件內容的一個很是好的工具。

----------------------------------------------------------------------
第二,使用ssh ,rsh等通道來遠程傳輸文件
來看看實例:

[root@Jason64-16 ~]# rsync -avz -e "ssh -p 22" lsp@10.0.0.123:/tmp/* /root/sdb5/mtp
receiving incremental file list
created directory /root/sdb5/mtp
rsync: opendir "/tmp/ssh-htajkG3141" failed: Permission denied (13)
rsync: opendir "/tmp/ssh-otRApl3239" failed: Permission denied (13)
1.txt
10.txt
100.txt
11.txt
12.txt
13.txt
14.txt
15.txt
16.txt
……
98.txt
99.txt
lsp
rsync: send_files failed to open "/tmp/u2dtmpS46uG3": Permission denied (13)
ssh-htajkG3141/    -->因爲權限問題,某些目錄普通用戶不能對其操做,因此。
ssh-otRApl3239/    -->這裏能夠看見,提示只是複製了一個目錄名過來。
sent 1957 bytes  received 5118 bytes  4716.67 bytes/sec
total size is 6  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]



rsync藉助ssh通道來實現文件傳輸的比較少,通常都是使用它獨特的運行方式,也就是守護進程的形式來進行文件傳輸。
因此,利用ssh通道的例子咱們就簡單的介紹一種,其餘的都是照貓畫虎,沒啥大區別。
那麼接下來,咱們就介紹rsync的守護進程。

-----------------------------------------------------------------------
第三,守護進程方式來進行文件傳輸
1.服務端配置vi /etc/rsyncd.conf

[root@Jason64-17 ~]#cat /etc/rsyncd.conf
#rsync_config_______________start
#first release created by oldboy
#QQ 49000448  blog:http://oldboy.blog.51cto.com
#modification by lisp 2013年9月26日 20:15:44
#QQ 1031239088 blog:http://lspgyy.blog.51cto.com
##rsyncd.conf -------------------start##
#這是rsync以什麼用戶來讀取本地的數據;
uid = root
gid = root
#是否能夠切換路徑
use chroot = no
#最大鏈接數是兩百;
max connections = 200
#鏈接三百秒超時;
timeout = 300
#pid文件;
pid file = /var/run/rsyncd.pid
#鎖文件;
lock file = /var/run/rsync.lock
#日誌文件;
log file = /var/log/rsyncd.log
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
# #認證的虛擬用戶;能夠任意起名;
auth users = rsync_virtual
# #rsync認證的密鑰文件;
secrets file = /etc/rsync.password
#模塊;
[bak]
##路徑,至關於NFS共享的目錄同樣;
path = /bak/
##忽略錯誤;
ignore errors
# #只讀爲假,可寫;
read only = false
# #只讀爲假,可寫;
list = false
#添加多個模塊
[自定義]
path = /自定義/
##忽略錯誤;
ignore errors
# #只讀爲假,可寫;
read only = false
# #只讀爲假,可寫;
list = false
###rsyncd.conf -------------------end##


密碼認證文件

cat /etc/rsync.password
rsync.virtual:lisp


2.客戶端配置

cat /etc/rsync.password
lisp


=============
來看看實例:
=============
服務端配置/etc/rsyncd.conf和/etc/rsync.password
客戶端配置/etc/rsync.password
服務端
啓動rsync進程rsync --daemon
查看rsync進程ps -ef |grep rsync
查看rsync端口netstat -lnt |grep :873
殺掉rsync進程pkill rsync
根據rsync端口號反查進程lsof -i :873
建立/etc/rsync.password 而且受權600(虛擬用戶名和密碼用:隔開)
客戶端
建立/etc/rsync.password 而且受權600(只需用寫入密碼就ok)
==========================
服務端啓動rsync守護進程
==========================

[root@Jason64-16 bak]# pkill rsync
[root@Jason64-16 bak]# ps -ef |grep rsync
root      1943  1675  0 13:30 pts/0    00:00:00 grep --color=auto rsync
[root@Jason64-16 bak]# rsync --daemon
[root@Jason64-16 bak]# ps -ef |grep rsync
root      1945     1  0 13:30 ?        00:00:00 rsync --daemon
root      1947  1675  0 13:30 pts/0    00:00:00 grep --color=auto rsync
[root@Jason64-16 bak]# netstat -lnt |grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN
tcp        0      0 :::873

==========================
客戶端執行rsync
==========================

[ lsp@ 59CentOS ~ ]$rsync -avz /tmp/testdir/* rsync_virtual@10.0.0.16::bak --password-file=/etc/rsync.passwd
sending incremental file list
1
10
100
1000
101
102
103
104
105
106
107
108
109
11
110
111
……
……
sent 43007 bytes  received 19008 bytes  41343.33 bytes/sec
total size is 0  speedup is 0.00


=============================================
服務端查看bak模塊下指定path路徑下目錄的改變狀況
=============================================

[root@Jason64-16 bak]# ls |wc -l
1000
[root@Jason64-16 bak]# pwd
/bak

#能夠看見將客戶端/tmp/testdir 目錄下1000個文件所有備份到/bak目錄下
================================================================
————————————————————————————
服務器能夠指定多個模塊來使用
————————————————————————————

[root@Jason64-16 bak]# cat /etc/rsyncd.conf
……omitting……
[bak]
#模塊;
path = /bak/
##路徑,至關於NFS共享的目錄同樣;
ignore errors
##忽略錯誤;
read only = false
# #只讀爲假,可寫;
list = false
[lisp]
path = /testdir/llsp
ignore errors
read only = false
list = false
……omitting……
#添加了一個模塊lisp

————————————————————————————————————————————————————
測試添加的模塊是否成功,那麼首先要在服務器建立/testdir/llsp

mkdir /testdir/llsp

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
客戶端執行測試
-------------

[root@Jason64-17 ~]# rsync -avz /testdir/ rsync_virtual@10.0.0.16::lisp --password-file=/etc/rsync.passwd
sending incremental file list
./
test01.txt
test02.txt
test03.txt
test05.txt
test06.txt
sent 359 bytes  received 106 bytes  930.00 bytes/sec
total size is 20  speedup is 0.04
[root@Jason64-17 ~]# rsync -avz /testdir/ rsync_virtual@10.0.0.16::bak --password-file=/etc/rsync.passwd
sending incremental file list
sent 152 bytes  received 8 bytes  320.00 bytes/sec
total size is 20  speedup is 0.12

成功執行!
注意!rsync的三種模式通常最經常使用的就是守護進程方式!之後還會涉及到/etc/xinetd.d/rsync 來啓動rsync的守護進程。

固然,rsync的用法,遠遠不止這些。它用在數據同步的時候是個很好的工具。不過它也有很差的地方。

rsync的優勢與不足:

優勢: 與傳統的cp、tar備份方式相比,rsync具備安全性高、備份迅速、支持增量備份等優勢,經過rsync能夠解決對實時性要求不高的數據備份需求,例如按期的備份文件服務器數據到遠端服務器,對本地磁盤按期作數據鏡像等。
缺點: 隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了不少不足,首先,rsync同步數據時,須要掃描全部文件後進行比對,進行差量傳輸。若是文件數量達到了百萬甚至千萬量級,掃描全部文件將是很是耗時的。並且正在發生變化的每每是其中不多的一部分,這是很是低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它能夠經過linux守護進程的方式進行觸發同步,可是兩次觸發動做必定會有時間差,這樣就致使了服務端和客戶端數據可能出現不一致,沒法在應用故障時徹底的恢復數據。

相關文章
相關標籤/搜索