rsync學習筆記
課程目標
1.能夠使用rsync實現本地檔案同步(cp效果)
2.能夠使用rsync實現遠端網路檔案同步(scp效果)
3.將rsync放入後臺執行,實現資料定時同步。
一、rsync是什麼
Rsync是一款開源的、快速的、多功能的、可實現全量及增量
的本地或遠端資料同步備份的優秀工具。並且可以不進行改變原有資料的屬性資訊,實現資料的備份遷移特性。
圖解rsync增量備份
二、rsync語法
rsync命令超詳細解釋。
rsync [選項] 源資料 目的資料
1.安裝
yum install rsync -y
2.命令語法,分幾個模式
- 本地模式
rsync 引數 源路徑 目標路徑
rsync -xxxxx /var/log /tmp
- 遠端模式,推送方式,把自己的資料推送到另一臺機器上(上傳)
語法1 ,rsync預設走ssh協議
rsync 引數 源路徑 user@ip:目標路徑
rsync -avzP /var/log/ root@10.0.0.31:/tmp/
語法2
rsync 引數 源路徑 user@ip::目標路徑
- 遠端模式,拉取方式,拉取別人機器的資料到自己的機器上(下載)
rsync 引數 user@ip:源路徑 目標路徑
rsync 引數 user@ip::源路徑目標路徑
rsync -avzP root@10.0 .0.31:/var/log/ /tmp/
引數解釋
-v 詳細模式輸出
-a 歸檔模式,遞迴的方式傳輸檔案,並保持檔案的屬性,等同於 -rlptgoD
-r 遞迴拷貝目錄
-l 保留軟連結
-p 保留原有許可權
-t 保留原有時間(修改)
-g 保留屬組許可權
-o 保留屬主許可權
-D 等於--devices --specials 表示支援b,c,s,p型別的檔案
-R 保留相對路徑
-H 保留硬連結
-A 保留ACL策略
-e 指定要執行的遠端shell命令
-E 保留可執行許可權
-X 保留擴充套件屬性資訊 a屬性
比較常用的組合引數
rsync -avzP
-a 保持檔案原有屬性
-v 顯示傳輸細節情況
-z 對傳輸資料壓縮傳輸
-P 顯示檔案傳輸的進度資訊
你在命令列裡,執行命令,喜歡看到命令的執行過程
-avzP
腳本里面?
bash xxx.sh
rsync -az
三、rsync命令使用
1. 本機同步
注意:
1. 本地資料同步的時候,源目錄後面的“/”會影響同步的結果
# rsync -av /dir1/ /dir3 //只同步目錄下面的檔案到指定的路徑
# rsync -av /dir1 /dir2 //將當前目錄dir1和目錄下的所有檔案一起同步
2. -R:不管加不加"/",都會將源資料的絕對路徑一起同步
# rsync -avR /dir1/ /dir2/
3. --delete:刪除目標目錄裡多餘的檔案
# rsync -avR --delete /dir1/ /dir2/
本機同步練習
1.本機同步檔案
[root@nfs-31 /]$rm -rf text3 [root@nfs-31 /]$rm -rf text4 [root@nfs-31 /]$ [root@nfs-31 /]$mkdir /text1 /text2 [root@nfs-31 /]$touch /text1/haha.txt [root@nfs-31 /]$rsync -avzP /text1/ /text2 sending incremental file list ./ haha.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) sent 104 bytes received 38 bytes 284.00 bytes/sec total size is 0 speedup is 0.00 [root@nfs-31 /]$ls text2 haha.txt
2.本機同步資料夾
[root@nfs-31 /]$rsync -avzP /text1 /text2
sending incremental file list
text1/
text1/haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 118 bytes received 39 bytes 314.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs-31 /]$ls text2
haha.txt text1
[root@nfs-31 /]$
遠端rsync備份
生成測試資料
[root@nfs-31 /]$dd bs=200M count=10 if=/dev/zero of=/text 1/2g.log 10+0 records in 10+0 records out 2097152000 bytes (2.1 GB) copied, 2.88561 s, 727 MB/s [root@nfs-31 /]$
另外一臺機器上建立檔案
[root@rsync-41 /]$mkdir /text3 /text4
[root@rsync-41 /]$
推送資料
[root@nfs-31 /]$rsync -avzP /text1/ root@rsync-41:/text3
The authenticity of host 'rsync-41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:iSKUuRM4GkuUBlCpq2MZHDK5hG126KG3lzafo4cOOA4.
ECDSA key fingerprint is MD5:d7:69:65:64:bb:92:37:81:74:96:69:6a:6d:13:76:32.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'rsync-41' (ECDSA) to the list of known hosts.
root@rsync-41's password:
sending incremental file list
./
2g.log
2,097,152,000 100% 162.43MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 2,039,634 bytes received 57 bytes 99,497.12 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17
[root@nfs-31 /]$
驗證接收
[root@rsync-41 /]$ls /text3
2g.log haha.txt
[root@rsync-41 /]$
限制傳輸io佔用
[root@nfs-31 /]$rsync -avzP --bwlimit=20 /text1/ root@rsync-41:/text4
root@rsync-41's password:
sending incremental file list
./
2g.log
2,097,152,000 100% 20.67MB/s 0:01:36 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 2,039,634 bytes received 57 bytes 19,518.57 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17
[root@rsync-41 /]$ls /text4
2g.log haha.txt
拉取資料的模式
[root@rsync-41 /]$mkdir text5
[root@rsync-41 /]$rsync -avzP root@nfs-31:/text1/ /text5
root@nfs-31's password:
receiving incremental file list
./
2g.log
2,097,152,000 100% 166.51MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 65 bytes received 2,039,634 bytes 123,618.12 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17
[root@rsync-41 /]$ls /text5
2g.log haha.txt
[root@rsync-41 /]$
無差異化同步
[root@rsync-41 /]$mkdir /text3
[root@rsync-41 /]$touch /text3/wudi.php
[root@rsync-41 /]$ls text3/
wudi.php
[root@rsync-41 /]$
[root@nfs-31 /]$rsync -avzP --delete /text1/ root@rsync-41:/text3
root@rsync-41's password:
sending incremental file list
deleting wudi.php
./
2g.log
2,097,152,000 100% 161.54MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 2,039,638 bytes received 69 bytes 116,554.69 bytes/sec
total size is 2,097,152,000 speedup is 1,028.16
[root@nfs-31 /]$
[root@rsync-41 /]$ls text3/
wudi.php
[root@rsync-41 /]$ls text3/
2g.log haha.txt
[root@rsync-41 /]$
搭建rsync服務模式
需要先進行配置檔案引數設定
- rsync配置檔案解讀
配置引數 | 引數說明 |
---|---|
uid = rsync | 指定rsync服務執行的時候,向磁碟進行讀取和寫入操作的操作者 |
gid = rsync | 指定rsync服務執行的時候,向磁碟進行讀取和寫入操作的操作者 |
use chroot = no | 進行資料同步儲存時,安全相關引數,預設內網進行資料同步,可以關閉 |
max connections = 200 | 定義向備份伺服器進行資料儲存的併發連線數 |
timeout = 300 | 定義與備份伺服器建立的網路連線,在多長時間沒有資料傳輸時,就釋放連線 |
pid file = /var/run/rsyncd.pid | 服務程式執行時,會將程序的pid資訊儲存到一個指定的pid檔案中 |
lock file = /var/run/rsync.lock | 定義鎖檔案,主要用於配合max connections 引數,當達到最大連線就禁止繼續訪問 |
配置引數 | 引數說明 |
---|---|
log file = /var/log/rsyncd.log | 定義服務的日誌檔案儲存路徑資訊 |
[backup] | 指定備份目錄的模組名稱資訊 |
path = /backup | 指定資料進行備份的目錄資訊 |
ignore errors | 在進行資料備份傳輸過程過程中,忽略一些I/O產生的傳輸錯誤 |
read only = false | 設定對備份的目錄的具有讀寫許可權,即將只讀模式進行關閉 |
list = false | 確認是否可以將服務配置的模組資訊,在客戶端可以檢視顯示 |
hosts allow = 172.16.1.0/24 | 設定備份目錄允許進行網路資料備份的主機地址或網段資訊,即設定白名單 |
配置引數 | 引數說明 |
---|---|
hosts deny = 0.0.0.0/32 | 設定備份目錄禁止進行網路資料備份的主機地址或網段資訊,即設定黑名單 |
auth users = rsync_backup | 指定訪問備份資料目錄的認證使用者資訊,為虛擬定義的使用者,不需要進行建立 |
secrets file = /etc/rsync.password | 設定訪問備份資料目錄進行認證使用者的密碼檔案資訊,會在檔案中設定認證使用者密碼資訊 |
配置引數 | 引數說明 |
---|---|
[backup] | 指定模組名稱,便於日後維護 |
path=/backup | 在當前模組中,Daemon使用的檔案系統或目錄,注意目錄許可權和配置檔案許可權一直,防止讀寫出問題 |
#exclude= |
排除檔案或目錄,相對路徑 |
[chaoge] | 還可以新增其他模組 |
cat >> /etc/rsyncd.conf << 'EOF'
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = yuchaoit.cn about rsync
path = /backup
[data]
comment = this is secord backup dir,to website data..
path = /data
EOF
3.建立使用者以及資料目錄
根據你的配置檔案中定義的資訊,建立對應的使用者,備份的目錄
該無法登入的使用者,只是用於執行程序的賬戶
useradd -u 1000 -M -s /sbin/nologin www
建立配置檔案中定義的2個備份目錄
mkdir -p /data/ /backup
修改備份目錄的許可權
[root@rsync-41 /]$useradd -u 1000 -M -s /sbin/nologin www
[root@rsync-41 /]$mkdir -p /data/ /backup
[root@rsync-41 /]$chown -R www:www /data/
[root@rsync-41 /]$chown -R www:www /backup/
[root@rsync-41 /]$ll -d /data /backup/
drwxr-xr-x 2 www www 6 Apr 20 17:18 /backup/
drwxr-xr-x 2 www www 6 Apr 20 17:18 /data
[root@rsync-41 /]$\\
4.建立rsync專用的賬戶密碼
1.建立密碼檔案,寫入賬戶和密碼,用於和客戶端連線時候的認證 vim /etc/rsync.passwd
2.寫入賬戶密碼
[root@rsync-41 /]$cat /etc/rsync.passwd
rsync_backup:123123
3.待會客戶端向carsync伺服器推送資料,就得用這個賬號密碼!!!!
4.這一步,非常重要,rsync要求降低密碼檔案的許可權,且必須是600
[root@rsync-41 /]$chmod 600 /etc/rsync.passwd
[root@rsync-41 /]$ll /etc/rsync.passw
ls: cannot access /etc/rsync.passw: No such file or directory
[root@rsync-41 /]$ll /etc/rsync.passwd
-rw------- 1 root root 20 Apr 20 17:29 /etc/rsync.passwd
[root@rsync-41 /]$
5.加入開機自啟動
設定rsyncd服務,執行,且開機自啟
systemctl start rsyncd
檢查rsyncd服務是否執行,以及該服務的執行日誌
[root@rsync-41 /]$systemctl start rsyncd
[root@rsync-41 /]$systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2022-04-20 17:35:52 CST; 13s ago
Main PID: 4365 (rsync)
CGroup: /system.slice/rsyncd.service
└─4365 /usr/bin/rsync --daemon --no-detach
Apr 20 17:35:52 rsync-41 systemd[1]: Started fast remote ...
Apr 20 17:35:52 rsync-41 systemd[1]: Starting fast remote...
Apr 20 17:35:52 rsync-41 rsyncd[4365]: params.c:Parameter...
Apr 20 17:35:52 rsync-41 rsyncd[4365]: rsyncd version 3.1...
Hint: Some lines were ellipsized, use -l to show in full.
[root@rsync-41 /]$
yum install rsync -y
2.配置密碼檔案及授權
客戶端需要做的操作有2個,提供密碼認證
1. 生成密碼檔案,每次連線都指定這個密碼檔案
2. 生成密碼變數,讓當前系統中存在叫做 RSYNC_PASSWORD 這個變數,以及變數的值,是配置檔案中的密碼即可
把客戶端的資料,傳送給服務端的backup備份模組下
語法,不一樣了,注意語法的寫法!!!
吧客戶端的 /tmp/200M.log 備份,傳送到rsync-41機器上的 backup模組下
rsync -avzP /tmp/200M.log 賬戶@主機名::模組名
[root@nfs-31 /]$dd bs=200M count=1 if=/dev/zero of=/tmp/200M.log
1+0 records in
1+0 records out
209715200 bytes (210 MB) copied, 0.709325 s, 296 MB/s
[root@nfs-31 /]$
[root@nfs-31 /]$rsync -avzP /tmp/200M.log rsync_backup@rsync-41::backup
Password:
sending incremental file list
200M.log
209,715,200 100% 172.39MB/s 0:00:01 (xfr#1, to-chk=0/1)
sent 204,046 bytes received 43 bytes 31,398.31 bytes/sec
total size is 209,715,200 speedup is 1,027.57
[root@nfs-31 /]$
此時需要互動式進行密碼輸入
此時嘗試設定非互動式密碼的操作,如下兩個方法
1. 生成密碼檔案,每次連線都指定這個密碼檔案(在客戶端生成)
echo '123123' > /etc/my_rsync.pwd
[root@nfs-31 /]$echo '123123' > /etc/my_rsync.pwd
[root@nfs-31 /]$chmod 600 /etc/my_rsync.pwd
[root@nfs-31 /]$rsync -avzP --password-file=/etc/my_rsync.pwd /tmp/200M.log rsync_backup@rsync-41::data
sending incremental file list
200M.log
209,715,200 100% 174.19MB/s 0:00:01 (xfr#1, to-chk=0/1)
sent 204,046 bytes received 43 bytes 81,635.60 bytes/sec
total size is 209,715,200 speedup is 1,027.57
2. 生成密碼變數,讓當前系統中存在叫做 RSYNC_PASSWORD 這個變數,以及變數的值,是配置檔案中的密碼即可
export RSYNC_PASSWORD='123123'
[root@rsync-41 /]$ls /backup/
200M.log
[root@rsync-41 /]$rm -rf /backup/200M.log
[root@rsync-41 /]$ls /backup/
[root@rsync-41 /]$
[root@nfs-31 /]$export RSYNC_PASSWORD='123123'
[root@nfs-31 /]$rsync -avzP /tmp/200M.log rsync_backup@rsync-41::backup
sending incremental file list
200M.log
209,715,200 100% 172.24MB/s 0:00:01 (xfr#1, to-chk=0/1)
sent 204,046 bytes received 43 bytes 136,059.33 bytes/sec
total size is 209,715,200 speedup is 1,027.57
[root@rsync-41 /]$ls /backup/
200M.log
[root@nfs-31 /]$rsync -avzP rsync_backup@rsync-41::backup /opt/
receiving incremental file list
./
200M.log
209,715,200 100% 191.94MB/s 0:00:01 (xfr#1, to-chk=0/2)
sent 50 bytes received 204,089 bytes 136,092.67 bytes/sec
total size is 209,715,200 speedup is 1,027.32
[root@nfs-31 /]$ls /opt
200M.log network_init.sh
[root@nfs-31 /]$