內容全為個人理解和自查資料梳理,歡迎各位大神指點!
每天學習較為零散。
day24
一、Rsync傳輸文件
#安裝rsync#-a遞歸同步(包含子目錄)保留文件權限、所有者、組、時間戳等元數據
#??-z傳輸時壓縮數據
#??-v顯示詳細同步過程
#??-P??顯示傳輸進度 #傳輸目錄 #加/ /my_smb/ 表示拷貝該目錄下所有文件
[root@01c/]# rsync -azvP /my_smb/ /my_test/
sending incremental file list
./
Windows_put.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
sam.txt0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)sent 203 bytes received 57 bytes 520.00 bytes/sec
total size is 0 speedup is 0.00
[root@01c/]# ls /my_test/
233.jpg sam.txt Windows_put.txt#不加/ /my_smb 表示拷貝該目錄
[root@01c/]# rsync -azvP /my_smb /my_test/
sending incremental file list
my_smb/
my_smb/Windows_put.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
my_smb/sam.txt0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)sent 219 bytes received 58 bytes 554.00 bytes/sec
total size is 0 speedup is 0.00
[root@01c/]# ll /my_test/
total 0
drwxrwxrwx 2 samba01 samba01 44 Jul 24 18:56 my_smb
#傳輸單個小文件
[root@01c/]# rsync -azvP /my_smb/Windows_put.txt /my_test/
sending incremental file list
Windows_put.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)sent 112 bytes received 35 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00
[root@01c/]# ls /my_test/
Windows_put.txt#傳輸時排除隱藏文件和目錄
[root@01c/my_test]# rsync -avzP --exclude=.* /my_test/ /my_smb/#傳輸單個大文件 --bwlimit 限速,限制傳輸單個大文件時占用的磁盤io[root@01c/]# dd bs=50M count=2 if=dev/zero of=/my_smb/100M.log
2+0 records in
2+0 records out
104857600 bytes (105 MB) copied, 0.549043 s, 191 MB/s
[root@01c/]# rsync -avzP --bwlimit=10 /my_smb/100M.log /my_test/
sending incremental file list
100M.log104,857,600 100% 25.91MB/s 0:00:03 (xfr#1, to-chk=0/1)sent 102,065 bytes received 35 bytes 9,723.81 bytes/sec
total size is 104,857,600 speedup is 1,027.01
#鏡像同步(源和目標完全一致)
# /my_test/test01/源目錄,末尾的 /表示同步目錄??內容??(不包含目錄本身) /my_test/test02/目標目錄
#01中的文件會傳輸給02
[root@01c/my_test/test02]# rsync -avzP --delete /my_test/test01/ /my_test/test02/
sending incremental file list
./
aabb0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=3/5)
ccdd10 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=2/5)
ccdd20 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=1/5)
ccdd30 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=0/5)sent 259 bytes received 95 bytes 708.00 bytes/sec
total size is 0 speedup is 0.00[root@01c/my_test/test01]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 24 19:12 aabb
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd1
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd2
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd3[root@01c/my_test/test02]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 24 19:12 aabb
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd1
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd2
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd3[root@01c/my_test/test02]# touch test{1..5}
[root@01c/my_test/test02]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 24 19:12 aabb
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd1
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd2
-rw-r--r-- 1 root root 0 Jul 24 19:14 ccdd3
-rw-r--r-- 1 root root 0 Jul 24 19:19 test1
-rw-r--r-- 1 root root 0 Jul 24 19:19 test2
-rw-r--r-- 1 root root 0 Jul 24 19:19 test3
-rw-r--r-- 1 root root 0 Jul 24 19:19 test4
-rw-r--r-- 1 root root 0 Jul 24 19:19 test5#如果02目錄里有01目錄中沒有的文件,例如test{1..5},這些文件會被??永久刪除
[root@01c/my_test/test02]# rsync -avzP --delete /my_test/test01/ /my_test/test02/
sending incremental file list
deleting test5
deleting test4
deleting test3
deleting test2
deleting test1
./sent 115 bytes received 64 bytes 358.00 bytes/sec
total size is 0 speedup is 0.00
#編輯01目錄的test1 完全鏡像后會重新同步進02目錄
[root@01c/my_test/test01]# vim test1
[root@01c/my_test/test01]# rsync -avzP --delete /my_test/test01/ /my_test/test02/
sending incremental file list
./
test110 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/10)sent 241 bytes received 38 bytes 558.00 bytes/sec
total size is 10 speedup is 0.04
?Rsync遠程備份文件
大文件可以用--bwlimit 限速,限制傳輸大文件時占用的寬帶
發送
#rsync -avzP /本機需要備份的目錄/ 備份機用戶名@備份機IP:/備份機目錄/
#01c機的數據[root@01c/my_test/test02]# rsync -avzP /my_test/ root@192.168.195.123:/test_file/
The authenticity of host '192.168.195.123 (192.168.195.123)' can't be established.
ECDSA key fingerprint is SHA256:7/ofSV+xfCRuwEcQXC5UxjVMG1hVruPIrWQxlhFhgvw.
ECDSA key fingerprint is MD5:fc:a9:52:a6:bb:8b:7d:8a:bf:12:51:61:30:e3:8c:8a.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.195.123' (ECDSA) to the list of known hosts.
root@192.168.195.123's password:
sending incremental file list
./
test01/
sent 1,077 bytes received 373 bytes 126.09 bytes/sec
total size is 20 speedup is 0.01#01c的數據備份在02c的 /test_file/ 目錄下
[root@02c/test_file]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 23 16:18 233.txt
drwxr-xr-x 2 root root 122 Jul 24 19:32 test01
-rw-r--r-- 1 root root 0 Jul 23 16:12 test_012_get.txt
drwxr-xr-x 2 root root 122 Jul 24 19:32 test02
[root@02c/test_file]#
獲取?
#rsync -avzP 對方用戶名@對方機IP:/需要獲取的資源目錄/ /本機目錄/
#02c機獲取01c機的資源[root@02c/test_file]# rsync -avzP root@192.168.195.189:/my_test/ /test_file/
The authenticity of host '192.168.195.189 (192.168.195.189)' can't be established.
ECDSA key fingerprint is SHA256:7/ofSV+xfCRuwEcQXC5UxjVMG1hVruPIrWQxlhFhgvw.
ECDSA key fingerprint is MD5:fc:a9:52:a6:bb:8b:7d:8a:bf:12:51:61:30:e3:8c:8a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.195.189' (ECDSA) to the list of known hosts.
root@192.168.195.189's password:
receiving incremental file list
./
test01/0 100% 0.00kB/s 0:00:00 (xfr#9, to-chk=9/21)
test02/sent 381 bytes received 1,073 bytes 171.06 bytes/sec
total size is 20 speedup is 0.01#02c機中 將01c數據存放在 /test_file/ 目錄下
[root@02c/test_file]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 23 16:18 233.txt
drwxr-xr-x 2 root root 122 Jul 24 19:32 test01
-rw-r--r-- 1 root root 0 Jul 23 16:12 test_012_get.txt
drwxr-xr-x 2 root root 122 Jul 24 19:32 test02
[root@02c/test_file]#
二、Rsync服務端
rsync借助ssh協議同步數據存在缺陷,使用root用戶不安全,普通用戶可能導致權限不足。
所以用rsync的守護程序傳輸方式,不使用root系統用戶更安全并且有權限。
Rsync的配置設置
[root@01c/]# vim /etc/rsyncd.conf uid = work_rsyncd
gid = work_rsyncd
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors = yes
read only = false
auth users = rsync_backup #用于客戶端登陸的用戶名
secrets file = /etc/rsync.passwd #密碼文件
log file = /var/log/rsync.log #日志
[backup] #用于客戶端備份的模塊設置和目錄
path = /backup
comment = Backup Directory
read only = false[data]
path = /data
comment = Data Directory
read only = false
創建用戶以及目錄?
#創建不能登陸的賬號,僅用于運行進程
[root@01c/my_test]# useradd -u 1999 -M -s /sbin/nologin work_rsyncd#創建rsync配置里需要的目錄
[root@01c/my_test]# mkdir -p /data/ /backup#修改目錄所屬
[root@01c/my_test]# chown -R work_rsyncd:work_rsyncd /data/
[root@01c/my_test]# chown -R work_rsyncd:work_rsyncd /backup/[root@01c/my_test]# ll -d /data/ /backup/
drwxr-xr-x 2 work_rsyncd work_rsyncd 6 Jul 24 23:21 /backup/
drwxr-xr-x 2 work_rsyncd work_rsyncd 6 Jul 24 23:21 /data/
創建虛擬用戶密碼文件?
#創建密碼文件設置賬號和密碼,用于客戶端連接時的認證
[root@01c/my_test]# vim /etc/rsync.passwd
[root@01c/my_test]# cat /etc/rsync.passwd
rsync_backup:zxcvbn#密碼文件權限600
[root@01c/my_test]# chmod 600 /etc/rsync.passwd
[root@01c/my_test]# ll /etc/rsync.passwd
-rw------- 1 root root 20 Jul 24 23:37 /etc/rsync.passwd
#啟動,查看狀態log
[root@01c/my_test]# systemctl start rsyncd
[root@01c/my_test]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemonLoaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)Active: active (running) since Fri 2025-07-25 00:29:16 CST; 13s agoMain PID: 5350 (rsync)CGroup: /system.slice/rsyncd.service└─5350 /usr/bin/rsync --daemon --no-detachJul 25 00:29:16 01c systemd[1]: Started fast remote file copy program daemon.#查看進程狀態
[root@01c/my_test]# netstat -tunlp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 5350/rsync
tcp6 0 0 :::873 :::* LISTEN 5350/rsync
[root@01c/my_test]# ps -ef |grep rsync
root 5350 1 0 00:29 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 5375 1265 0 00:33 pts/0 00:00:00 grep --color=auto rsync
三、Rsync客戶端
交互式客戶端向服務端傳輸備份
# 客戶端需要備份的文件或目錄 服務端預留登陸的賬號@服務端IP::服務端備份配置模塊名
[root@02c/test_file]# rsync -avzP /test_file/233.txt rsync_backup@192.168.195.189::backup
Password:
sending incremental file list
233.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)sent 90 bytes received 43 bytes 29.56 bytes/sec
total size is 0 speedup is 0.00
可用于腳本的客戶端向服務端傳輸備份
#在客戶端創建密碼文件
[root@02c/test_file]# vim /etc/my_rsync.pwd
[root@02c/test_file]# cat /etc/my_rsync.pwd
zxcvbn#權限改為600
[root@02c/test_file]# chmod 600 /etc/my_rsync.pwd#腳本中省略傳輸過程和進度 --password-file指定客戶端密碼文件 客戶端需要備份的文件 在服務端data模塊進行傳輸
[root@02c/test_file]# rsync -az --password-file=/etc/my_rsync.pwd /test_file/233.txt rsync_backup@192.168.195.189::data
[root@02c/test_file]# #服務端的/data目錄中已經成功備份
[root@01c/my_test]# ll /data/
total 0
-rw-r--r-- 1 work_rsyncd work_rsyncd 0 Jul 23 16:18 233.txt
#先定義密碼變量
[root@02c/test_file]# export RSYNC_PASSWORD='zxcvbn'#客戶端需要備份文件 向服務端data模塊傳輸
[root@02c/test_file]# rsync -avzP /test_file/test_012_get.txt rsync_backup@192.168.195.189::data
sending incremental file list
test_012_get.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)sent 99 bytes received 43 bytes 284.00 bytes/sec
total size is 0 speedup is 0.00#取消密碼變量或者重啟客戶端,密碼變量失效需要重新定義
[root@02c/test_file]# unset RSYNC_PASSWORD
下載服務端的備份到客戶端?
#獲取服務端data模塊內容 到客戶端/test_file/目錄下
[root@02c/test_file]# rsync -avzP rsync_backup@192.168.195.189::data /test_file/
Password:
receiving incremental file list
./
233.txt0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
test_012_get.txt0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)sent 69 bytes received 216 bytes 81.43 bytes/sec
total size is 0 speedup is 0.00
#指定密碼文件 從服務端backup模塊下載備份到 客戶端
[root@02c/test_file]# rsync -az --password-file=/etc/my_rsync.pwd rsync_backup@192.168.195.189::backup /test_file/
[root@02c/test_file]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 23 16:18 233.txt
-rw-r--r-- 1 root root 0 Jul 25 01:14 626.txt
-rw-r--r-- 1 root root 0 Jul 23 16:12 test_012_get.txt
四、Rsync排錯
服務端
1.檢査rsync服務端的配置文件路徑是否正確:/etc/rsyncd.conf
2.查看配置文件的 host allow,host deny 允許的ip網段是否允許客戶端訪問
3.查看配置文件中的path參數路徑是否存在,權限是否正確(和配置文件的UUID參數對應)
4.查看rsync服務是否啟動,端口、進程是否存活
5.查看iptables防火墻、selinux是否允許rsync服務通過,或是關閉
6.查看服務端rsync配置文件的密碼文件,權限是否600,格式,語法是否正確,且和配置文件的 secrect files 參數對應
7.如果是推送數據,要査看配置rsyncd.conf中的用戶對該 rsync模塊 下的文件是否可以讀取
客戶端
1.查看rsync客戶端配置的密碼文件權限是否600,密碼文件格式是否正確,是否和服務端的密碼一致(客戶端密碼文件里只寫密碼,不寫賬戶)
2.嘗試teInet連接rsync服務端的 873 端口,檢測服務是否可以連接
3.客戶端執行命令語法要仔細檢查
五、服務端和客戶端的備份腳本
#在backup目錄下創建備份目錄 命名格式:主機名 主機IP (用/分割后取第一部分,即刪掉/24部分)當前年月日
[root@01c/]# mkdir /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")#將etc和log目錄打包備份在/backup目錄下
#被打包文件不用絕對路徑,盡量用相對路徑
[root@01c/backup]# cd / && tar -zcf /backup/01c_192.168.195.189_2025-07-25/etc.tgz etc
[root@01c/]# cd / && tar -zcf /backup/01c_192.168.195.189_2025-07-25/log.tgz var/log/[root@01c/]# ll /backup/
total 0
drwxr-xr-x 2 root root 36 Jul 25 14:19 01c_192.168.195.189_2025-07-25[root@01c/]# ll /backup/01c_192.168.195.189_2025-07-25/
total 10812
-rw-r--r-- 1 root root 10313617 Jul 25 14:19 etc.tgz
-rw-r--r-- 1 root root 755637 Jul 25 14:19 log.tgz
客戶端備份腳本?
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binmkdir -p /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")cd / && tar -zcf /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")/etc.tgz etccd / && tar -zcf /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")/log.tgz var/logmd5sum /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")/*.tgz > /backup/$(hostname)_$(ip a show ens33|awk 'NR==3{print $2}'|cut -d'/' -f1)_$(date "+%F")/all_data_md5.txt #對數據進行校驗,生成校驗文件export RSYNC_PASSWORD=zxcvbn #定義密碼變量rsync -az /backup/ rsync_backup@192.168.195.189::backupfind /backup -type f -mtime +7 -delete #找到超過7天的備份刪除,節約資源
客戶端向服務端備份腳本測試?
[root@02c~]# bash -x 02c_client.sh
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
++ hostname
++ ip a show ens33
++ awk 'NR==3{print $2}'
++ cut -d/ -f1
++ date +%F
+ mkdir -p /backup/02c_192.168.195.123_2025-07-25
+ cd /
++ hostname
++ awk 'NR==3{print $2}'
++ cut -d/ -f1
++ ip a show ens33
++ date +%F
+ tar -zcf /backup/02c_192.168.195.123_2025-07-25/etc.tgz etc
+ cd /
++ hostname
++ ip a show ens33
++ awk 'NR==3{print $2}'
++ cut -d/ -f1
++ date +%F
+ tar -zcf /backup/02c_192.168.195.123_2025-07-25/log.tgz var/log
++ hostname
++ ip a show ens33
++ awk 'NR==3{print $2}'
++ cut -d/ -f1
++ date +%F
+ md5sum /backup/02c_192.168.195.123_2025-07-25/etc.tgz /backup/02c_192.168.195.123_2025-07-25/log.tgz
++ hostname
++ ip a show ens33
++ awk 'NR==3{print $2}'
++ cut -d/ -f1
++ date +%F
+ export RSYNC_PASSWORD=zxcvbn
+ RSYNC_PASSWORD=zxcvbn
+ rsync -az /backup/ rsync_backup@192.168.195.189::backup
+ find /backup -type f -mtime +7 -delete
#創建定時任務
[root@02c~]# crontab -e
crontab: installing new crontab#每天分鐘執行一次腳本
[root@02c~]# crontab -l
* * * * * /usr/sbin/ntpdate time1.aliyun.com >> /dev/null 2>&1
* * * * * /bin/bash 02c_client.sh#成功備份
[root@01c/backup]# ll
total 0
drwxr-xr-x 2 work_rsyncd work_rsyncd 36 Jul 25 17:34 02c_192.168.195.12[root@02c/backup]# ll
total 0
drwxr-xr-x 2 root root 36 Jul 25 17:34 02c_192.168.195.123_2025-07-2
[root@02c/backup]# #校驗通過
[root@01c/backup]# md5sum -c /backup/02c_192.168.195.123_2025-07-25/all_data_md5.txt
/backup/02c_192.168.195.123_2025-07-25/etc.tgz: OK
/backup/02c_192.168.195.123_2025-07-25/log.tgz: OK
備份完成發送郵箱
郵箱配置
[root@01c/]# vim /mail.rc set from=xxxxxxxx@163.com # 發件人地址
set smtp=smtps://smtp.163.com # SMTP服務器(SSL加密)
set smtp-auth-userxxxxxxx@163.com # 認證用戶名
set smtp-auth-password=xxxxxxxxxxx # 認證密碼
set smtp-auth=login # 認證方式
set ssl-verify=ignore # 忽略SSL證書驗證
set nss-config-dir=/etc/pki/nssdb/ # NSS證書數據庫路徑
發送校驗結果到指定郵箱?
[root@01c/]# md5sum -c /backup/02c_192.168.195.123_2025-07-25/all_data_md5.txt
/backup/02c_192.168.195.123_2025-07-25/etc.tgz: OK
/backup/02c_192.168.195.123_2025-07-25/log.tgz: OK#將校驗結果寫進check_md5_result.txt文件里
[root@01c/]# md5sum -c /backup/02c_192.168.195.123_2025-07-25/all_data_md5.txt >/backup/02c_192.168.195.123_2025-07-25/check_md5_result.txt#郵件命名為 check-rsync-年月日 將備份的校驗結果發送到運維郵箱里
[root@01c/]# mail -s "check-rsync-$(date +%F)" xxxxxxxx@163.com < /backup/02c_192.168.195.123_2025-07-25/check_md5_result.txt
?服務端校驗和郵箱腳本
[root@01c~]# vim check_md5.sh#!/bin/bash#對備份文件校驗保存校驗結果
md5sum -c /backup/02c_192.168.195.123_$(date +%F)/all_data_md5.txt >/backup/02c_192.168.195.123_$(date +%F)/check_md5_result.txt#將結果發送到郵箱
mail -s "check-rsync-$(date +%F)" xxxxx@163.com < /backup/02c_192.168.195.123_$(date +%F)/check_md5_result.txt #刪除超過30天的備份
find /backup -type f -mtime +30 -delete
#創建定時任務 每月5號運行一次腳本
[root@01c~]# crontab -l
* * * * * /usr/sbin/ntpdate time1.aliyun.com >> /dev/null 2>&1
* * 5 * * /bin/bash check_m5d.sh