一、Rsync 簡介
1.1 Rsync 是一個遠程增量文件備份軟件工具
1.2 Rsync 的特性
- 支持拷貝特殊文件,如連接文件、設備等。
- 可以有排除指定文件或目錄同步的功能,相當于打包命令 tar 的排除功能。
- 可以做到保持原文件或目錄的權(quán)限、時間、軟硬鏈接、屬主、組等所有屬性均不改變(參數(shù):–p)。
- 可以實現(xiàn)增量同步,既只同步發(fā)生變化的數(shù)據(jù),因此數(shù)據(jù)傳輸效率很高(tar -N)。
- 可以使用 scp、rsh、ssh 等方式來配合傳輸文件(rsync 本身不對數(shù)據(jù)加密)。
- 可以通過 socket(進程方式)傳輸文件和數(shù)據(jù)(服務端和客戶端)。
- 支持匿名的活認證(無需系統(tǒng)用戶)的進程模式傳輸,可以實現(xiàn)方便安全的進行數(shù)據(jù)備份和鏡像。
1.3?Rsync 的相關參數(shù)
-a #歸檔模式傳輸, 等于-tropgDl -t -r -o -p -g -D -l
-r #遞歸傳輸目錄及子目錄,即目錄下得所有目錄都同樣傳輸。
-p #保持文件權(quán)限
-o #保持文件屬主信息
-g #保持文件屬組信息
-t #保持文件時間信息
-l #保留軟連接
-D #保持設備文件信息
-L #保留軟連接指向的目標文件
-z #傳輸時進行壓縮以提高效率
-v #詳細模式輸出, 打印速率, 文件數(shù)量等
-P #顯示備份過程 + --partial
-e #使用的信道協(xié)議,指定替代 rsh 的 shell 程序
-n #顯示那些將要傳輸?shù)奈募?,并不真正傳?--exclude=PATTERN #指定排除不需要傳輸?shù)奈募?--exclude-from=file #排除不需要的文件
--include=PATTERN
--include-from=file
--bwlimit=100 #限速傳輸,單位 MB
--partial #斷點續(xù)傳
--existing #僅僅更新那些已經(jīng)存在于 DST 的文件,而不備份那些新創(chuàng)建的文件
--delete #刪除那些 DST 中 SRC 沒有的文件
--force #強制刪除目錄,即使不為空
--password-file=xxx #使用密碼文件
--port #指定端口傳輸
1.4?Rsync 常見的使用場景
- 定時同步:Rsync + Cron
- 實時同步:Rsync + Innotify
1.5 Rsync 常見的傳輸模式
(1) 本地傳輸
格式:
rsync [參數(shù)] [源路徑] [目的路徑]
參數(shù):
-t #保持文件時間信息
-r #遞歸傳輸
示例:
1.將 /opt/test.tar.gz 備份至 /backup 目錄下:rsync /opt/test.tar.gz /backup
2.將 /var/log 目錄備份至 /backup/log 目錄下:rsync -r /var/log/ /backup/log/
(2)?遠程傳輸
1.常規(guī)遠程模式
格式:
rsync [參數(shù)] [本地源路徑] [用戶]@[ip]:[遠程目的路徑]
示例:
(1).上傳數(shù)據(jù):
將 /opt/test.tar.gz 上傳目標服務器的 /backup 目錄下:
rsync /opt/test.tar.gz root@ip:/backup
(2).下載數(shù)據(jù):
將目標服務器 /opt/test.tar.gz 下載到 /backup 目錄下:
rsync root@ip:/opt/test.tar.gz /backup
2.守護進程模式
即 以守護進程的模式運行服務端
優(yōu)點:(1)保證數(shù)據(jù)安全、(2)數(shù)據(jù)歸檔
二、Rsync 的搭建部署
2.1 資源準備
服務器 | IP 地址 | 操作系統(tǒng) | 作用 |
目標服務器 | 192.168.0.162 | Centos7 | 服務端 |
目標服務器 | 192.168.0.163 | WinServer | 服務端 |
源服務器 | 192.168.0.164 | Centos7 | 客戶端 |
源服務器 | 192.168.0.165 | WinServer | 客戶端 |
????????說明:本次配置實驗分兩部分:
? ? ? ? (1)linux 作為服務端(目標服務器),linux + windows 作為客戶端
????????(2)windows 作為服務端(目標服務器),linux + windows 作為客戶端
2.2 以 linux 作為服務端
2.2.1 linux 服務端進行配置
? ? ? ??Linux 服務端:192.168.0.162
(1)服務安裝
#服務端安裝 Rsync 服務
[root@rsync162 ~]# yum install -y rsync
(2)配置 rsync 配置文件
[root@rsync162 ~]# vi /etc/rsyncd.conf
#rsync 服務端口
port=9999
#rsync 日志文件
log file = /var/log/rsync.log
#rsync 進程 id 文件
pid file = /var/run/rsyncd.pid
#同步模塊項目
[proname]
#同步的目標目錄
path = /backup
#同步傳輸前是否切換到指定目錄下(用于增強傳輸?shù)陌踩裕?use chroot = yes
#最大連接數(shù)
max connections = 4
#只讀文件是否同步,yes表示無法同步只讀文件
read only = no
#客戶端請求顯示模塊列表是,是否顯示該模塊
list = true
#服務運行的用戶
uid = root
#服務運行的用戶組
gid = root
#進行驗證的用戶,即客戶端進行傳輸時的用戶
auth users = test
#連接時的用戶和密碼信息
secrets file=/etc/rsyncd.passwd
#允許的 ip
hosts allow = *
(3)創(chuàng)建測試用戶及配置密碼文件
[root@rsync162 ~]# useradd test
[root@rsync162 ~]# passwd test
更改用戶 test 的密碼 。
新的 密碼:
無效的密碼: 密碼包含用戶名在某些地方
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經(jīng)成功更新。
[root@rsync162 ~]#
[root@rsync162 ~]#
[root@rsync162 ~]# vi /etc/rsyncd.passwd
test:test123456
(3)目標文件夾創(chuàng)建與授權(quán)
#創(chuàng)建備份目錄
[root@rsync162 ~]# mkdir /backup
[root@rsync162 ~]# chown test:test /backup
(4)啟動 rysnc 服務并開放端口
#先關閉 selinux
[root@rsync162 ~]# setenforce 0
#啟動 rsync 服務
[root@rsync162 ~]# systemctl start rsyncd
#查看服務狀態(tài)
[root@rsync162 ~]# netstat -tunlp |grep 9999
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 30129/rsync
tcp6 0 0 :::9999 :::* LISTEN 30129/rsync
#防火墻開放端口
[root@rsync162 ~]# firewall-cmd --permanent --add-port=9999/tcp
success
[root@rsync162 ~]# firewall-cmd --reload
success
(5)設置開機自啟
[root@rsync162 ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service
2.2.2 以 windows 作為客戶端
? ? ? ? Windows 客戶端:192.168.0.165
(1)下載客戶端:
????????官網(wǎng)下載安裝文件:https://www.itefix.net/cwrsync
????????這邊實驗版本為:5.4.1 (文件最后會提供下載,若官網(wǎng)無法下載可參考使用)
(2)在 windows 客戶端目錄下創(chuàng)建 passwd.txt 文件,并配置同服務端的?/etc/rsyncd.passwd 密碼相一致,不同的是客戶端不需要加上用戶名(這里的密碼,可以同 test 系統(tǒng)用戶密碼一樣,也可以不一樣,總之就是服務端和客戶端要一致即可)
(3)測試客戶端傳輸文件 (這里的 passwd.txt 和 windows.zip.gz 都在當前目錄下)
C:\soft\cwRsync_5.4.1_x86_Free>.\rsync.exe -avzP --password-file=passwd.txt --port=9999 windows.zip.gz test@192.168.0.162::proname
(4)在服務器上查看接收到的文件
[root@localhost ~]# ll /backup/
總用量 20424
-rwx------. 1 root 401 20913856 3月 20 15:52 windows.zip.gz
(5)測試客戶端傳輸目錄
C:\soft\cwRsync_5.4.1_x86_Free>.\rsync.exe -avzP --password-file=passwd.txt --po
rt=9999 windows_test test@192.168.0.162::proname
?(6)在服務器上查看接收到的目錄和文件
[root@rsync162 ~]# ll /backup/
總用量 20424
drwx------. 2 root 401 106 4月 23 2023 windows_test
-rwx------. 1 root 401 20913856 3月 20 15:52 windows.zip.gz
[root@rsync162 ~]# ll /backup/windows_test/
總用量 23684
-rwx------. 1 root 401 284 3月 20 17:29 cc.txt
-rwx------. 1 root 401 3328826 4月 13 17:09 cwRsync_5.4.1_XiaZaiBa.zip
-rwx------. 1 root 401 20913856 3月 20 15:52 windows.zip.gz
-rwx------. 1 root 401 286 4月 3 13:10 新建文本文檔.txt
(7)測試從服務端同步數(shù)據(jù)到客戶端
#這里我把目錄 windows_test 重命名為 server_test
[root@rsync162 ~]# mv /backup/windows_test /backup/server_test
#在windows的執(zhí)行文件目錄下創(chuàng)建了 from_server 文件夾
#C:\soft\cwRsync_5.4.1_x86_Free\from_server
C:\soft\cwRsync_5.4.1_x86_Free>.\rsync.exe -avzP --password-file=passwd.txt --port=9999 test@192.168.0.162::proname from_server
(8)在客戶端中新建的?from_server 文件夾下可以看到已經(jīng)接收到數(shù)據(jù)
(9)如果要使用絕對路徑
# /cygdrive/c/ 代表 C 盤目錄
C:\soft\cwRsync_5.4.1_x86_Free>.\rsync.exe -avzP --password-file=passwd.txt --port=9999 test@192.168.0.162::proname /cygdrive/c/soft/cwRsync_5.4.1_x86_Free/from_server
#不能使用如下路徑
C:\soft\cwRsync_5.4.1_x86_Free\from_server
?2.2.3 以 Linux 作為客戶端
? ? ? ? Linux 客戶端:192.168.0.164
(1)客戶端安裝 rsync 服務
#客戶端安裝 Rsync 服務
[root@rsync164 ~]# yum install -y rsync
(2)Linux 客戶端以常規(guī)遠程方式傳輸數(shù)據(jù)文件測試(不同于 windows 客戶端,linux 客戶端可以直接使用常規(guī)的遠程模式進行數(shù)據(jù)同步,其用法和 scp 命令相似)
#客戶端測試傳輸文件
[root@rsync164 ~]# rsync -avz linux.tar.gz root@192.168.0.162:/backup
root@192.168.0.162's password:
sending incremental file list
linux.tar.gz
sent 2,465,340 bytes received 35 bytes 182,620.37 bytes/sec
total size is 2,465,302 speedup is 1.00
#服務端查看文件
[root@rsync162 ~]# ll /backup/
總用量 2408
-rw-r--r--. 1 root root 2465302 4月 13 22:29 linux.tar.gz
(3)Linux 客戶端以守護進程方式傳輸數(shù)據(jù)文件測試(服務端在上面已經(jīng)以守護進程模式啟動了服務,所以這里測試我們只需要在 linux 客戶端上做配置)
#創(chuàng)建密碼文件
[root@rsync164 ~]# vi /etc/rsync.pass
test123456
#傳輸測試
[root@rsync164 ~]# rsync -avz --port 9999 --password-file /etc/rsync.pass linux.tar.gz test@192.168.0.162::proname
ERROR: password file must not be other-accessible
#修改文件權(quán)限
[root@rsync164 ~]# chmod 600 /etc/rsync.pass
#重新測試
[root@rsync164 ~]# rsync -avz --port 9999 --password-file /etc/rsync.pass linux.tar.gz test@192.168.0.162::proname
sending incremental file list
linux.tar.gz
sent 2,465,341 bytes received 35 bytes 234,797.71 bytes/sec
total size is 2,465,302 speedup is 1.00
#服務端查看接收的文件
[root@rsync162 ~]# ll /backup/
總用量 22832
-rw-r--r--. 1 root root 2465302 4月 13 22:29 linux.tar.gz
2.3 以 Windows 作為服務端
? ? ? ? Windows 服務端版本使用 4.1.0 版本(文件最后會提供下載)
?2.3.1 服務端安裝與配置
? ? ? ? Windows 服務端:192.168.0.163
(1)雙擊服務進行安裝,服務用戶可以用默認的,建議是自己定義
(2)安裝即可,默認安裝位置在:C:\Program Files (x86)\ICW\
(3)安裝完成之后,系統(tǒng)會自動多出一個 rsync 用戶、服務中會多出一個 RsyncServer的服務(注意:如果兩者都沒有,可能是沒有用管理員模式運行安裝軟件的緣故,如果也用了但是沒有生成用戶和服務,那么請手動創(chuàng)建系統(tǒng)用戶,賬戶和密碼同步驟(1)的要保持一致)
?(4)將 RsyncServer 服務的啟動類型改為自動并啟動服務
(5)新建一個數(shù)據(jù)同步目錄data(自己定義及選擇位置)
?(6)將目錄權(quán)限提供給rsync用戶
(7)編輯配置文件:C:\Program Files (x86)\ICW\rsyncd.conf?內(nèi)容如下
(8)編輯密碼文件:C:\Program Files (x86)\ICW\etc\rsyncd.secrets
(9)重啟 rsyncd 服務
(10)關閉防火墻或開啟rsync默認端口 tcp:873(因為配置文件沒有配置服務端口,所以要開放默認的端口)
2.3.2 windows 客戶端
????????Windows 客戶端:192.168.0.165
(1)在 windows?客戶端測試機上進行測試
#測試傳輸數(shù)據(jù)
C:\soft\cwRsync_5.4.1_x86_Free>.\rsync.exe -avzP --password-file=passwd.txt windows.log test@192.168.0.163::proname
sending incremental file list
windows.log
5,291 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 2,457 bytes received 34 bytes 383.23 bytes/sec
total size is 5,291 speedup is 2.12
(2)在服務端數(shù)據(jù)目錄查看
?2.3.3?linux 客戶端
????????Linux 客戶端:192.168.0.164
(1)在 linux 客戶端測試機上進行測試
#測試傳輸數(shù)據(jù)
[root@rsync164 ~]# rsync -avz --password-file /etc/rsync.pass linux.log test@192.168.0.163::proname
sending incremental file list
linux.log
sent 512 bytes received 34 bytes 99.27 bytes/sec
total size is 712 speedup is 1.30
(2)在服務端數(shù)據(jù)目錄查看文章來源:http://www.zghlxwxcb.cn/news/detail-428227.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-428227.html
2.4 幾個常見的錯誤點
#日志 /var/log/rsyncd.log
#錯誤一:
auth failed on module proname from WIN-NEQ2FNHO1US (192.168.0.165) for root: no secrets file
# 錯誤一重點查看 /etc/rsyncd.conf 配置是否有問題,配置中的路徑文件是否存在,權(quán)限是否正確,比如 /etc/rsyncd.passwd 文件的權(quán)限是 600;又比如
#錯誤二:
auth failed on module proname from WIN-NEQ2FNHO1US (192.168.0.165) for test: no matching rule
#錯誤二通常是由于客戶端配置的密碼和服務端配置的密碼不一致導致,又或者服務端不存在該用戶(用戶一定要存在)
#錯誤三:
ERROR: password file must not be other-accessible
#錯誤三通常是客戶端報出來的錯誤,即 linux 客戶端 的 passwd 文件權(quán)限要修改為 600
到了這里,關于數(shù)據(jù)備份系列:Rsync 備份詳解(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!