1.1. Rsync介紹html
1.1.1 什麼是Rsync
Rsync是一款開源的、快速的、多功能的、可實現全量或增量的本地或者遠程數據鏡像同步複製、備份的優秀工具。Rsync適用於unix、linux、windows等各類平臺。
Rsync 是一個遠程數據同步工具,可經過 LAN/WAN 快速同步多臺主機間的文件。
Rsync 原本是用以取代scp 的一個工具,它當前由 Rsync.samba.org 維護。
Rsync 使用所謂的「Rsync 演算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。運行 Rsync server 的機器也叫 backup server,一個 Rsync server 可同時備份多個 client 的數據;也能夠多個Rsync server 備份一個 client 的數據。
Rsync 能夠搭配 rsh 或 ssh 甚至使用 daemon 模式。Rsync server 會打開一個873的服務通道(port),等待對方 Rsync 鏈接。鏈接時,Rsync server 會檢查口令是否相符,若經過口令查覈,則能夠開始進行文件傳輸。第一次連通完成時,會把整份文件傳輸一次,下一次就只傳送二個文件之間不一樣的部份。
參考資料:http://www.samba.org/ftp/rsync/rsync.htmllinux
1.1.2 Rsync 簡介
Rsync 英文全稱:Remote Rynchronization,可實現全量或增量的本地或者遠程數據鏡像同步複製、備份,相似於scp命令,可是優於scp,能夠作增量的備份。Rsync還能夠在本地主機的不一樣分區或目錄之間全量及增量的複製數據,這相似與cp命令,一樣優於cp命令(增量)。
提示:Rsync 還能夠實現相似rm的刪除功能( --process --delete)
在同步備份數據時,默認狀況下,Rsync經過其獨特的quick check 算法,它僅同步大小或者最後修改時間發生變化的文件或目錄,也能夠根據全縣,屬主等屬性的變化同步,可是須要指定相應的參數,設置能夠實現只同步一個文件裏有變化部分的內容。算法
1.1.3 Rsync特性
支持拷貝特殊文件如連接、設備等
能夠有排除特定文件或目錄同步的功能,至關於打包命令tar的排除功能。
能夠作到保持源文件或目錄的權限、時間、軟硬連接、屬主屬組等全部屬性的不改變。
能夠實現增量同步,既只同步發生變化的數據,由於數據傳輸效率很高。
可使用rcp、rsh、ssh等方式配合傳輸文件,也能夠直接經過socket(進程方式)傳輸。
支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。shell
1.1.4 Rsync 的核心算法
假定在名爲 α 和 β 的兩臺計算機之間同步類似的文件 A 與 B,其中 α 對文件A擁有訪問權,β 對文件 B 擁有訪問權,而且假定主機 α 與 β 之間的網絡帶寬很小。Rsync 算法將經過下面的五個步驟來完成:
①β 將文件 B 分割成一組不重疊的固定大小爲 S 字節的數據塊。最後一塊可能會比 S 小。
②β 對每個分割好的數據塊執行兩種校驗:一種是32位的滾動弱校驗,另外一種是128位的 MD4 強校驗。
③β 將這些校驗結果發給 α。
④α 經過搜索文件A的全部大小爲 S 的數據塊(偏移量能夠任選,不必定非要是 S 的倍數),來尋找與文件B 的某一塊有着相同的弱校驗碼和強校驗碼的數據塊。這項工
能夠藉助滾動校驗的特性很快完成。
⑤α 發給 β 一串指令來生成文件 A 在 β 上的備份。這裏的每一條指令要麼是對文件 B 經擁有某一個數據塊而不須重傳的證實,要麼是一個數據塊,這個數據塊確定是沒有與文件 B 的任何一個數據塊匹配上的。vim
2.1 Rsync的工做方式
一、主機本地間的數據傳輸(相似cp命令的功能)
二、藉助rcp、ssh等通道來傳輸數據(相似scp命令的功能)
三、以守護進程(socket)的方式傳輸數據(重要 ※※※※※)windows
2.1.1 本地數據傳輸模式(local-only mode)安全
1 Local: rsync [OPTION...] SRC... [DEST]
2.1.2 經過遠程shell進行數據傳輸(remote shell mode)服務器
1 Access via remote shell: 2 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 3 Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
2.1.3 經過守護進程的方式進行數據傳輸網絡
1 Access via rsync daemon: 2 Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] 3 rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] 4 Push: rsync [OPTION...] SRC... [USER@]HOST::DEST 5 rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Rsync命令同步參數選項
rsync [OPTION...] SRC ... DEST
經常使用參數選項說明:
-v, --verbose 詳細模式輸出,傳輸時的進度等信息
-z, --compress 傳輸時進行壓縮以提升傳輸效率,--compress-level=NUM可按級別壓縮
-r, --recursive 對子目錄以遞歸模式,即目錄下的全部目錄都一樣傳輸,注意是小寫r
-t, --times 保持文件時間信息
-o, --owner 保持文件屬主信息
-p, --perms 保持文件權限
-g, --group 保持文件屬組信息
-P, --progress 顯示同步的過程及傳輸時的進度等信息
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性,等於-rtopgDl
-D, --devices 保持設備文件信息
-l, --links 保留軟連接
-e, --rsh=COMMAND 使用的信道協議, 指定替代rsh的shell程序。例如:ssh
--exclude=PATTERN 指定排除不須要傳輸的文件模式ssh
以上爲經常使用的參數:更多參數請自行man rsync 或查看資料地址: http://www.samba.org/ftp/rsync/rsync.html
vza至關於 vzrtopgDl
特別提示:
rsync -avz /opt/ /mnt #加/,只同步目錄內的內容
rsync -avz /opt /mnt #不加/,會將目錄和目錄內的內容一塊兒同步
3.1 經過Rsync在本地傳輸數據實踐
1 #經過rsync命令,推送56_rsync_a.log到10.0.0.57的家目錄 2 [root@Rsync_A ~]# ll 3 total 76 4 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log 5 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg 6 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log 7 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog 8 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh 9 [root@Rsync_A ~]# rsync -avzP 56_rsync_a.log root@10.0.0.57:~ 10 sending incremental file list 11 56_rsync_a.log 12 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) 13 14 sent 75 bytes received 31 bytes 212.00 bytes/sec 15 total size is 0 speedup is 0.00 16 17 [root@Rsync_B ~]# ll 18 total 68 19 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log 20 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg 21 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log 22 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog 23 24 #經過rsync命令,拉取10.0.0.57主機/tmp目錄數據到本地/tmp目錄下 25 [root@Rsync_B tmp]# ll 26 total 4 27 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log 28 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492 29 30 [root@Rsync_A ~]# ll /tmp/ 31 total 8 32 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537 33 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh 34 [root@Rsync_A ~]# rsync -avz root@10.0.0.57:/tmp /tmp 35 receiving incremental file list 36 tmp/ 37 tmp/57.tmp.log 38 tmp/.ICE-unix/ 39 tmp/ssh-XpIRDi2492/ 40 tmp/ssh-XpIRDi2492/agent.2492 41 42 sent 45 bytes received 210 bytes 510.00 bytes/sec 43 total size is 0 speedup is 0.00 44 [root@Rsync_A ~]# ll /tmp/ 45 total 12 46 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537 47 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh 48 drwxrwxrwt 4 root root 4096 May 30 2013 tmp 49 [root@Rsync_A ~]# tree /tmp/ 50 /tmp/ 51 |-- ssh-aiWOPN2537 52 | `-- agent.2537 53 |-- system_init.sh 54 `-- tmp 55 |-- 57.tmp.log 56 `-- ssh-XpIRDi2492 57 `-- agent.2492 58 59 3 directories, 4 files
3.2藉助rcp、ssh等通道來傳輸數據實踐
1 #經過ssh通道推送數據 2 [root@Rsync_A ~]# rsync -avzP -e "ssh -p22" 56_rsync_a.log root@10.0.0.57:/tmp 3 sending incremental file list 4 56_rsync_a.log 5 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) 6 7 sent 75 bytes received 31 bytes 212.00 bytes/sec 8 total size is 0 speedup is 0.00 9 10 [root@Rsync_B ~]# ll /tmp/ 11 total 4 12 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log 13 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log 14 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492 15 16 #經過ssh通道拉取數據 17 [root@Rsync_A ~]# rsync -avz -e "ssh -p22" root@10.0.0.57:/tmp . 18 receiving incremental file list 19 tmp/ 20 tmp/56_rsync_a.log 21 tmp/57.tmp.log 22 tmp/.ICE-unix/ 23 tmp/ssh-XpIRDi2492/ 24 tmp/ssh-XpIRDi2492/agent.2492 25 26 sent 64 bytes received 274 bytes 676.00 bytes/sec 27 total size is 0 speedup is 0.00 28 [root@Rsync_A ~]# ll 29 total 80 30 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log 31 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg 32 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log 33 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog 34 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh 35 drwxrwxrwt 4 root root 4096 May 30 2013 tmp 36 [root@Rsync_A ~]# tree tmp/ 37 tmp/ 38 |-- 56_rsync_a.log 39 |-- 57.tmp.log 40 `-- ssh-XpIRDi2492 41 `-- agent.2492 42 43 1 directory, 3 files
3.3 經過ssh key+rsync實現批量免密碼加密分發數據
1 #批量將任意文件服務分發至/tmp目錄下 2 #!/bin/sh 3 . /etc/init.d/functions 4 if [ $# -ne 1 ];then 5 echo "Usage:$0 argv" 6 exit 7 fi 8 for ip in `cat iplist` 9 do 10 rsync -avzP $1 -e "ssh -p 52113" lican888@$ip:~ >&/dev/null 11 ssh -p52113 -t lican888@$ip sudo rsync ~/$1 /etc >&/dev/null 12 if [ $? -eq 0 ];then 13 action "fenfa $1 successful." /bin/true 14 else 15 action "fenfa $1 failure." /bin/false 16 fi 17 done
3.4 以守護進程(socket)的方式傳輸數據(※※※※※)
主機網絡參數設置:
主機名 |
IP |
GATEWAY |
備註 |
Rsync_A |
10.0.0.56 |
10.0.0.254 |
rsync服務端 |
Rsync_B |
10.0.0.57 |
10.0.0.254 |
rsync節點 |
3.4.1 開始配置
1)配置rsyncd.conf
1 #確認安裝 2 [root@Rsync_A ~]# rpm -qa rsync 3 rsync-3.0.6-4.el5_7.1 ---> 3.0版本,一邊比對,一邊同步 4 5 [root@Rsync_A ~]# vim /etc/rsyncd.conf 6 #rsync_config_______________start 7 uid = rsync 8 gid = rsync 9 use chroot = no 10 max connections = 200 11 timeout = 300 12 pid file = /var/run/rsyncd.pid 13 lock file = /var/run/rsync.lock 14 log file = /var/log/rsyncd.log 15 16 [skyex] 17 path = /skyex/ 18 ignore errors 19 read only = false 20 list = false 21 hosts allow = 10.0.0.0/24 22 hosts deny = 0.0.0.0/32 23 auth users = rsync_backup 24 secrets file = /etc/rsync.password 25 #rsync_config_______________end
-->配置多個模塊路徑的配置文件簡便寫法:
1 [root@Rsync_A ~]# cat /etc/rsyncd.conf 2 ##rsyncd.conf start## 3 uid = rsync 4 gid = rsync 5 use chroot = no 6 max connections = 200 7 timeout = 300 8 pid file = /var/run/rsyncd.pid 9 lock file = /var/run/rsync.lock 10 log file = /var/log/rsyncd.log 11 ignore errors 12 read only = false 13 list = false 14 hosts allow = 10.0.0.0/24 15 hosts deny = 0.0.0.0/32 16 auth users = rsync_backup 17 secrets file = /etc/rsync.password 18 [skyex] 19 path = /backup/ 20 [skyex01] 21 path = /backup01/ 22 #rsync_config_______________end
2)配置用戶目錄、密碼文件
1 #目錄、用戶權限建立配置 2 [root@Rsync_A ~]# useradd rsync -s /sbin/nologin -M 3 [root@Rsync_A ~]# grep rsync /etc/passwd 4 rsync:x:502:502::/home/rsync:/sbin/nologin 5 [root@Rsync_A ~]# chown rsync.rsync /skyex/ 6 [root@Rsync_A ~]# ls -ld /skyex/ 7 drwxr-xr-x 2 rsync rsync 167936 May 30 22:10 /skyex/ 8 9 #配置密碼文件(格式---> 用戶:密碼) 10 [root@Rsync_A ~]# echo "rsync_backup:skyex" >> /etc/rsync.password 11 [root@Rsync_A ~]# cat /etc/rsync.password 12 rsync_backup:skyex 13 14 #更改密碼文件權限600 15 [root@Rsync_A ~]# chmod 600 /etc/rsync.password 16 [root@Rsync_A ~]# ls -ld /etc/rsync.password 17 -rw------- 1 root root 19 May 27 22:14 /etc/rsync.password
3.4.2啓動Rsync服務
1 #啓動rsync 2 [root@Rsync_A ~]# rsync --daemon 3 4 #查看rsync進程 5 [root@Rsync_A ~]# ps -ef|grep rsync 6 root 2779 1 0 22:41 ? 00:00:00 rsync --daemon 7 root 2785 2678 0 22:41 pts/0 00:00:00 grep rsync 8 9 #根據端口查看進程 10 [root@Rsync_A ~]# lsof -i tcp:873 11 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 12 rsync 2779 root 4u IPv4 8610 0t0 TCP *:rsync (LISTEN) 13 14 [root@Rsync_A ~]# netstat -lntup |grep 873 15 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2779/rsync
3.4.3 Rsync 客戶端配置
1 #配置密碼文件 2 [root@Rsync_B ~]# echo "skyex">>/etc/rsync.passwored 3 [root@Rsync_B ~]# cat /etc/rsync.password 4 skyex 5 6 #更改密碼文件權限爲600 7 [root@Rsync_B ~]# chmod 600 /etc/rsync.password 8 [root@Rsync_B ~]# ls -ld /etc/rsync.password 9 -rw------- 1 root root 6 May 27 22:17 /etc/rsync.password
3.4.4 Rsync推送數據
1 #推送/var/www/html到服務器端 2 [root@Rsync_B script]# cd /var/www/ 3 [root@Rsync_B www]# tar zcvf html_$(date +%F).tar.gz ./html/ 4 ./html/ 5 ./html/d 6 ./html/a 7 ./html/c 8 ./html/e 9 ./html/b 10 ./html/f 11 [root@Rsync_B www]# ll 12 total 8 13 drwxr-xr-x 2 root root 4096 May 30 23:44 html 14 -rw-r--r-- 1 root root 190 May 30 23:59 html_2013-05-30.tar.gz 15 [root@Rsync_B www]# 16 17 #推送數據壓縮包至rsync服務器skyex模塊內 18 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex 19 Password: 20 sending incremental file list 21 html_2013-05-30.tar.gz 22 190 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) 23 24 sent 277 bytes received 27 bytes 46.77 bytes/sec 25 total size is 190 speedup is 0.62 26 27 #免密碼輸入,推送數據 28 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex --password-file=/etc/rsync.password 29 sending incremental file list 30 31 sent 43 bytes received 8 bytes 102.00 bytes/sec 32 total size is 190 speedup is 3.73 33 [root@Rsync_B www]# 34 35 #經過rsync協議進行推送 36 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync://rsync_backup@10.0.0.56/skyex --password-file=/etc/rsync.password 37 sending incremental file list 38 39 sent 43 bytes received 8 bytes 102.00 bytes/sec 40 total size is 190 speedup is 3.73 41 [root@Rsync_B www]#
FQA:
1 問題1: no route 2 [root@Client-B www]# rsync -avzP html_2013-05-26.tat.gz rsync_backup@10.0.0.101::skyex --password-file=/etc/rsync.password 3 rsync: failed to connect to 10.0.0.101: No route to host (113) 4 rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 5 解答:可能防火牆阻擋了。 telnet ip 873 檢查,若是出現connection refuse字樣表示防火牆阻擋或者服務沒有啓動好。 6 7 問題2: 8 root@Client-B www]# rsync -avz html_2013-05-26.tar.gz rsync_backup@10.0.0.101::/skyex --password-file=/etc/rsync.password 9 ERROR: The remote path must start with a module name not a / 10 rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6] 11 解答: 雙冒號後面是模塊名稱,不是路徑,去掉/ 。 12 13 問題3: 14 [root@Client-B www]# rsync -avzp html_2013-05-26.tar.gz rsync_backup@10.0.0.101::skyex 15 Password: 16 @ERROR: auth failed on module skyex 17 rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6] 18 [root@C-client www]# 19 解答:@ERROR: auth failed on module skyex --->配置文件不正確 20 密碼設置錯誤,也可致使此錯誤。 21 22 問題4: 23 [root@Client-B ~]# rsync -avzP 1.txt rsync_backup@10.0.0.101::skyex --password-file=/etc/rsync.password 24 sending incremental file list 25 1.txt 26 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) 27 rsync: mkstemp "/.1.txt.ynR66y" (in skyex) failed: Permission denied (13) 28 29 sent 62 bytes received 27 bytes 178.00 bytes/sec 30 total size is 0 speedup is 0.00 31 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6] 32 [root@Client-B ~]#
3.5 總結