Rsync詳解


1.什么是rsync

Rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。適用於unix/linux/windows等多種操作系統平台。

來自:http://www.samba.org/ftp/rsync/rsync.html

2.rsync特性

l  支持拷貝特殊文件如鏈接文件,設備等

l  可以有排除指定文件或者目錄同步的功能,相當於打包命令tar的排除功能

l  可以做到保持原文件或者目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變-p

l  可以實現增量同步、既只同步發生變化的數據,因此數據傳輸效率很高,tar -N

l  可以實現rcp、rsh、ssh等方式來配合數據文件(rsync本身不對數據加密)

l  可以通過socket(進程方式)傳輸文件和數據(服務端和客戶端)

l  支持匿名的或認證(無需系統用戶)的進程模式傳輸,可以實現方便安全的進行數據備份及鏡像

3.常用參數

rsync參數的具體解釋如下: 

-v, --verbose 詳細模式輸出 

-q, --quiet 精簡輸出模式 

-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗 

-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD 

-r, --recursive 對子目錄以遞歸模式處理 

-R, --relative 使用相對路徑信息 

-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名為~filename。可以使用--suffix選項來指定不同的備份文件前綴。 

--backup-dir 將備份文件(如~filename)存放在在目錄下。 

-suffix=SUFFIX 定義備份文件前綴 

-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件) 

-l, --links 保留軟鏈結 

-L, --copy-links 想對待常規文件一樣處理軟鏈結 

--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結 

--safe-links 忽略指向SRC路徑目錄樹以外的鏈結 

-H, --hard-links 保留硬鏈結 

-p, --perms 保持文件權限 

-o, --owner 保持文件屬主信息 

-g, --group 保持文件屬組信息 

-D, --devices 保持設備文件信息 

-t, --times 保持文件時間信息 

-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間 

-n, --dry-run現實哪些文件將被傳輸 

-W, --whole-file 拷貝文件,不進行增量檢測 

-x, --one-file-system 不要跨越文件系統邊界 

-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節 

-e, --rsh=COMMAND 指定使用rsh、ssh方式進行數據同步 

--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息 

-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件 

--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件 

--delete 刪除那些DST中SRC沒有的文件 

--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件 

--delete-after 傳輸結束以后再刪除 

--ignore-errors 及時出現IO錯誤也進行刪除 

--max-delete=NUM 最多刪除NUM個文件 

--partial 保留那些因故沒有完全傳輸的文件,以是加快隨后的再次傳輸 

--force 強制刪除目錄,即使不為空 

--numeric-ids 不將數字的用戶和組ID匹配為用戶名和組名 

--timeout=TIME IP超時時間,單位為秒 

-I, --ignore-times 不跳過那些有同樣的時間和長度的文件 

--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間 

--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認為0 

-T --temp-dir=DIR 在DIR中創建臨時文件 

--compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份 

-P 等同於 --partial 

--progress 顯示備份過程 

-z, --compress 對備份的文件在傳輸時進行壓縮處理 

--exclude=PATTERN 指定排除不需要傳輸的文件模式 

--include=PATTERN 指定不排除而需要傳輸的文件模式 

--exclude-from=FILE 排除FILE中指定模式的文件 

--include-from=FILE 不排除FILE指定模式匹配的文件 

--version 打印版本信息 

--address 綁定到特定的地址 

--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件 

--port=PORT 指定其他的rsync服務端口 

--blocking-io 對遠程shell使用阻塞IO 

-stats 給出某些文件的傳輸狀態 

--progress 在傳輸時現實傳輸過程 

--log-format=formAT 指定日志文件格式 

--password-file=FILE 從FILE中得到密碼 

--bwlimit=KBPS 限制I/O帶寬,KBytes per second 

-h, --help 顯示幫助信息 

一般同步傳輸目錄都使用azv選項. 

4.工作方式

4.1 本地拷貝

語法如下:
Local:
rsync [OPTION...] SRC... [DEST]
例如:拷貝/root/scripts目錄到/tmp目錄
[root@ansible day01]# rsync -zva --progress /root/scripts /tmp/
…
scripts/httpd-2.4.48/test/travis_run_linux.sh
          5,171 100%    9.33kB/s    0:00:00 (xfr#2971, to-chk=0/3073)

sent 18,756,297 bytes  received 57,117 bytes  5,375,261.14 bytes/sec
total size is 47,457,716  speedup is 2.52
# 查看拷貝
[root@ansible day01]# ls -l /tmp/
總用量 4
drwxr-xr-x 5 root root 4096 10月  3 15:27 scripts
[root@ansible day01]# rsync -zqa --progress /root/scripts/ /tmp/
[root@ansible day01]# ls /tmp/
20                     day01                dos.sh                  fs.dat                if.sh                       nginx_tj.sh                  test12.awk  test4.awk  test8.awk  var.sh
auth_install_lamp2.sh  df.awk               emp.dat                 host.info             linux-utilization-rate2.sh  redaltime-networktraffic.sh  test1.awk   test5.awk  test9.awk  websitecheck.sh
auth_install_lamp.sh   df.sh                findhigh-cpu-memory.sh  httpd-2.4.48          linux-utilization-rate.sh   test10.awk                   test2.awk   test6.awk  test.awk   yp.sh
awk_gj                 disk_monitoring2.sh  for.sh                  httpd-2.4.48.tar.bz2  list.info                   test11.awk                   test3.awk   test7.awk  var2.sh
備注:如果源目錄后面跟"/"會將目錄里的信息拷貝到目標目錄,而不是整個目錄

4.2 遠程同步

 

語法如下:
Access via remote shell:
Pull:
  rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push:
  rsync [OPTION...] SRC... [USER@]HOST:DEST

案例1:將本地/root/scripts目錄文件同步(上傳)到遠程192.168.138服務器/tmp目錄,即push
# rsync -avc /root/scripts  root@192.168.138.22:/tmp
# 需要設置ssh免密登錄

案例2:將遠程192.168.138.22服務器/root/test目錄同步(下載)到本地/tmp目錄,即pull
[root@ansible tmp]# rsync -avz root@192.168.138.22:/root/test /tmp/
receiving incremental file list
test/
test/a.txt

sent 47 bytes  received 116 bytes  326.00 bytes/sec
total size is 0  speedup is 0.00

# 需要設置ssh免密登錄

案例3:有時需要限制帶寬同步數據,可以使用參數—bwlimit單位kb
[root@ansible ~]# rsync -avz --bwlimit=100 /root/tools/nginx-1.12.1.tar.gz root@192.168.138.21:/tmp/
sending incremental file list
nginx-1.12.1.tar.gz

sent 980,442 bytes  received 35 bytes  93,378.76 bytes/sec
total size is 981,093  speedup is 1.00 
[root@ansible ~]# ssh root@192.168.138.21 'ls /tmp/'
nginx-1.12.1.tar.gz
scripts
tmp.fXOl7QSk6X

4.3 以守護進程(socket)的方式傳輸數據

-- 服務端  192.168.138.20

配置文件
# vim /etc/rsyncd.conf
#用戶
uid=rsync
#用戶組  
gid=rsync
# 安全相關   
use chroot=no
#最大連接數    
max connections=200
#超時時間         
timeout=300
#進程對應的進程號文件             
pid file=/var/run/rsyncd.pid
#鎖文件    
lock file=/var/run/rsync.lock
#日志文件      
log file=/var/log/rsync.log
# 模塊名稱       
[backup]
#服務器提供訪問的目錄 
path=/backup
# 忽略錯誤        
ignore errors
# 可寫          
read only=false
# 客戶端請求顯示模塊列表時,本模塊名稱是否顯示        
list=false
#設置哪些主機可以同步數據,多ip和網段之間使用空格分隔             
hosts allow= 192.168.138.0/24
#除了hosts allow定義的主機外,拒絕其他所有
hosts deny=0.0.0.0/32
# 虛擬認證用戶
auth users=rsync_backup
# 虛擬認證用戶賬號密碼信息    
secrets file=/etc/rsync.password  

# 查看虛擬用戶認證密碼信息
[root@ansible ~]# cat /etc/rsync.password 
rsync_backup:123456
# 修改/etc/rsync.password權限
[root@ansible ~]# chmod 600 /etc/rsync.password 
[root@ansible ~]# ls -l /etc/rsync.password 
-rw------- 1 root root 20 10月  4 15:52 /etc/rsync.password

# 查看rsync --daemon 語法信息
[root@ansible ~]# rsync --daemon --help
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

Usage: rsync --daemon [OPTION]...
     --address=ADDRESS       bind to the specified address
     --bwlimit=RATE          limit socket I/O bandwidth
     --config=FILE           specify alternate rsyncd.conf file
 -M, --dparam=OVERRIDE       override global daemon config parameter
     --no-detach             do not detach from the parent
     --port=PORT             listen on alternate port number
     --log-file=FILE         override the "log file" setting
     --log-file-format=FMT   override the "log format" setting
     --sockopts=OPTIONS      specify custom TCP options
 -v, --verbose               increase verbosity
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --help                  show this help screen

# 啟動 rsync
[root@ansible ~]# rsync --daemon
[root@ansible ~]# ps -ef|grep rsync|grep -v grep
root      1600     1  0 11:02 ?        00:00:00 rsync --daemon
# rsync daemon模式默認端口是873
[root@ansible ~]# netstat -lntp|grep 1600
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1600/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      1600/rsync  


-- 客戶端 192.168.138.21

1.需要安裝rsync工具,若沒有可yum安裝
[root@ansb01 ~]# rpm -qa rsync
rsync-3.1.2-10.el7.x86_64
[root@ansb01 ~]# yum -y install rsync

2.添加虛擬認證密碼信息

[root@ansb01 ~]# echo "123456" > /etc/rsync.password
[root@ansb01 ~]# cat /etc/rsync.password 
123456

3.修改權限
[root@ansb01 ~]# chmod 600 /etc/rsync.password 
[root@ansb01 ~]# ls -l /etc/rsync.password 
-rw------- 1 root root 7 10月  5 11:21 /etc/rsync.password


4.rsync daemon 語法格式
Access via rsync 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)


案例1: 從客戶端拉取服務端backup目錄文件到本地/tmp
pull:
rsync [OPTION...] [USER@]HOST::SRC... [DEST]

USER:虛擬認證用戶
HOST:服務器ip
SRC:/etc/rsyncd.conf配置文件中指定的模塊名,比如[backup]

[root@ansb01 ~]# rsync -zva rsync_backup@192.168.138.20::backup /tmp
Password: 
receiving incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt

sent 221 bytes  received 600 bytes  49.76 bytes/sec
total size is 0  speedup is 0.00

# Password 為虛擬認證用戶的密碼 此處為123456


案例1客戶端從服務端拉取文件時需要手動輸入虛擬用戶認證密碼,由於客戶端已經配置了虛擬用戶認證密碼,因此可以通過--password-file=FILE指定配置文件,讀取認證密碼,從而實現免密,如案例2
案例2:從客戶端免密拉取服務端backup目錄文件到本地/tmp
pull:
rsync [OPTION...] [USER@]HOST::SRC... [DEST]

[root@ansb01 ~]# rsync -zva rsync_backup@192.168.138.20::backup /tmp --password-file=/etc/rsync.password 
receiving incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt

sent 221 bytes  received 600 bytes  78.19 bytes/sec
total size is 0  speedup is 0.00
[root@ansb01 ~]# ls /tmp/ -l
總用量 0
-rw-r--r-- 1 root root 0 10月  5 11:35 10.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 1.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 2.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 3.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 4.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 5.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 6.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 7.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 8.txt
-rw-r--r-- 1 root root 0 10月  5 11:35 9.txt


案例3: 從客戶端免密拉取服務端backup目錄文件到本地/tmp
    Pull:
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

[root@ansb01 tmp]# rsync -avz rsync://rsync_backup@192.168.138.20/backup /tmp --password-file=/etc/rsync.password
receiving incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
a
b
c
d
e
f
g

sent 359 bytes  received 1,078 bytes  124.96 bytes/sec
total size is 0  speedup is 0.00




案例4: 客戶端推送src目錄文件到服務端
Push:
        rsync [OPTION...] SRC... [USER@]HOST::DEST
SRC: 客戶端目錄或文件
USER: 虛擬認證用戶
HOST: 服務端地址ip
DEST: /etc/rsyncd.conf配置文件中指定的模塊名,比如[backup]

[root@ansb01 tmp]# rsync -azv /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
a
b
c
d
e
f
rsync: chgrp ".ICE-unix" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".Test-unix" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".X11-unix" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".XIM-unix" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".font-unix" (in backup) failed: Operation not permitted (1)
.ICE-unix/
.Test-unix/
.X11-unix/
.XIM-unix/
.font-unix/
rsync: chgrp ".a.fWKn0x" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".b.C3BWjg" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".c.kF8vDY" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".d.UB26WG" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".e.6hzIgp" (in backup) failed: Operation not permitted (1)
rsync: chgrp ".f.0McmA7" (in backup) failed: Operation not permitted (1)

sent 672 bytes  received 1,014 bytes  160.57 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
# 查看問題2
# 重啟服務端后客戶端重新同步
[root@ansb01 tmp]# rsync -azv /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
a
b
c
d
e
f
g
.ICE-unix/
.Test-unix/
.X11-unix/
.XIM-unix/
.font-unix/

sent 722 bytes  received 184 bytes  86.29 bytes/sec
total size is 0  speedup is 0.00

## 由此可以報錯取消了


案例5:客戶端推送src目錄文件到服務端
    Push:
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST)


[root@ansb01 tmp]# rsync -avz /tmp/ rsync://rsync_backup@192.168.138.20/backup --password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
a
b
c
d
e
f
g

sent 1,063 bytes  received 355 bytes  135.05 bytes/sec
total size is 0  speedup is 0.00


案例6: 客戶端排除單個文件上傳 --exclude
[root@ansb01 tmp]# rsync -avz --exclude=1.txt /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
2.txt
3.txt
4.txt
5.txt

sent 407 bytes  received 108 bytes  49.05 bytes/sec
total size is 0  speedup is 0.00

案例7: 客戶端排除多個文件上傳 --exclude
1、
[root@ansb01 tmp]# rsync -avz --exclude={1.txt,2.txt} /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
3.txt
4.txt
5.txt

sent 355 bytes  received 89 bytes  42.29 bytes/sec
total size is 0  speedup is 0.00
2
[root@ansb01 tmp]# rsync -avz --exclude=1.txt --exclude=2.txt /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
3.txt
4.txt
5.txt

sent 355 bytes  received 89 bytes  42.29 bytes/sec
total size is 0  speedup is 0.00
3、有規律的范圍 --exclude={1..3}.txt
[root@ansb01 tmp]# rsync -avz --exclude={1..3}.txt /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
4.txt
5.txt

4、指定排除文件 --exclude-from=文件路徑
[root@ansb01 tmp]# cat /root/exclude.info 
1.txt
2.txt
[root@ansb01 tmp]# rsync -avz --exclude-from=/root/exclude.info /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
sending incremental file list
./
3.txt
4.txt
5.txt

sent 355 bytes  received 89 bytes  38.61 bytes/sec
total size is 0  speedup is 0.00


案例7: 無差異同步 --delete
rsync推送企業工作場景:push
數據備份
風險:客戶端有啥,服務端就有啥,客戶端沒有的服務端也要刪除。服務端的目錄數據可能丟失

rsync拉取企業工作場景:pull
代碼發布、下載
風險:服務端有啥,客戶端就有啥,服務端沒有的客戶端也要刪除。客戶端的目錄數據可能丟失




問題1:
[root@ansb01 ~]# rsync -zva rsync_backup@192.168.138.20::backup /tmp
rsync: failed to connect to 192.168.138.20 (192.168.138.20): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(125) [Receiver=3.1.2]
解決方案:
服務端端口沒有打開,關閉服務端防火牆或者打開服務端873端口

問題2:
rsync: chgrp ".a.fWKn0x" (in backup) failed: Operation not permitted (1)
解決方案:
在服務端/etc/rsyncd.conf配置文件中加入 "fake super = yes",然后重啟rsync服務

問題3:
[root@ansb01 tmp]# rsync -avz /tmp/ rsync_backup@192.168.138.20::backup --password-file=/etc/rsync.password
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]

[root@ansible /]# cat /var/log/rsync.log 
2021/10/05 15:04:45 [1889] params.c:Parameter() - Ignoring badly formed line in config file: ignore errors
2021/10/05 15:04:55 [1889] name lookup failed for 192.168.138.21: Temporary failure in name resolution
2021/10/05 15:04:55 [1889] connect from UNKNOWN (192.168.138.21)
2021/10/05 15:04:55 [1889] rsync: chdir /backup failed
: No such file or directory (2)
解決方案:
服務端缺少目錄 如/backup

問題4:
rsync: recv_generator: mkdir ".ICE-unix" (in backup) failed: Permission denied (13)
解決方案:
服務端uid用戶沒有指定目錄操作權限,修改文件屬主即可,例如
[root@ansible ~]# chown rsync /backup  -R

問題5:
2021/10/05 15:18:33 [1912] params.c:Parameter() - Ignoring badly formed line in config file: ignore errors
2021/10/05 15:18:43 [1912] name lookup failed for 192.168.138.21: Temporary failure in name resolution
2021/10/05 15:18:43 [1912] connect from UNKNOWN (192.168.138.21)
2021/10/05 15:18:43 [1912] secrets file must not be other-accessible (see strict modes option)
2021/10/05 15:18:43 [1912] auth failed on module backup from UNKNOWN (192.168.138.21) for rsync_backup: ignoring secrets file
解決方案:
密碼文件權限問題,需要授權600
[root@ansible backup]# chmod 600 /etc/rsync.password



總結:
rsync server:
1.vim /etc/rsyncd.conf(用戶,目錄,模塊,虛擬用戶及密碼文件)
2.創建共享目錄 /backup_new
3.創建rsync用戶,並授權訪問/backup_new
4.創建密碼文件,復制配置文件里的路徑,然后添加密碼內容
	格式: 虛擬用戶名:密碼

5.密碼文件授權600
6.rsync --daemon 放入/etc/rc.local
7.查看日志 tailf /var/log/rsyncd.log


rsync client(多個)
1.密碼文件和服務端沒有任何關系
/etc/rsync.password 內容只有密碼

2.密碼文件 600

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM