- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
拓撲:
Linux 1(源主機192.168.1.63)
Linux 2(目標主機192.168.1.64)
需要關閉selinux,否則即使創建/etc/rsyncd.conf文件之后去啟動服務也不會生效。
[root@Linux 1~]# setenforce 0
[root@Linux 1 ~]#yum -y install rsync
[root@Linux 1 ~]#systemctl start rsyncd
[root@Linux 1~]# netstat -antup | grep 873#查看873端口是否監聽。
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 43447/rsync
tcp6 0 0 :::873 :::* LISTEN 43447/rsync
-a:--archive archive mode權限保存模式,相當于-rlptgoD參數,存檔,遞歸,保持屬性等。
-z:--compress壓縮模式,當資料在傳送到目的端進行檔案壓縮。
-H:--hard-links保留硬鏈接文件。
--version:輸出rsync版本。
-v:--verbose復雜的輸出信息。
-u:--update僅僅進行更新,也就是跳過已經存在的目標位置,并且文件時間要晚于要備份的文件,不
覆蓋新的文件。
--port=PORT:定義rsyncd(daemon)要運行的port(預設為tcp 873)。
--delete:刪除那些目標位置有的文件而備份源沒有的文件。
--password-file=FILE:從指定密碼文件中獲取密碼。
--bwlimit=KBPS:限制I/O帶寬。
--filter “-filename”:需要過濾的文件。
--exclude=filname:需要過濾的文件。
--progress:顯示備份過程。
通常常用的選項–avz
對Linux 1網站根目錄的/var/www/html目錄備份到Linux 2的/web-back 。
服務器:Linux 1
客戶端:Linux 2
1.建立測試用戶(兩臺主機都需要操作):
[root@Linux 2 ~]# useradd rget1 && echo "123456" |passwd --stdin rget1
更改用戶rget1 的密碼。
passwd:所有的身份驗證令牌已經成功更新。
[root@Linux 1 ~]# useradd rget1 && echo "123456" |passwd --stdin rget1
更改用戶rget1 的密碼。
passwd:所有的身份驗證令牌已經成功更新。
2.對目錄賦予ACL權限:
[root@Linux 1~]# mkdir /var/www/html/ -p#創建目錄。
[root@Linux 1 ~]# setfacl -R -m user:rget1:rwx /var/www/html/
[root@Linux 1 ~]# getfacl/var/www/html
getfacl:Removing leading'/'from absolute path names
#file: var/www/html
#owner: root
#group: root
user::rwx
user:rget1:rwx
group::r-x
mask::rwx
other::r-x
3.創建測試數據:
[root@Linux 1 ~]# cp -r /boot/* /var/www/html/
#復制/boot/目錄下所有內容到/var/www/html/目錄下。
4.服務端上操作:
[root@Linux 2 ~]# mkdir /web-back#創建存放數據目錄。
[root@Linux 2 ~]# chown rget1:rget1 -R /web-back/
#修改目錄屬主、屬組為rget1用戶。
[root@Linux 1 ~]#rsync -avz /var/www/html/ rget1@192.168.1.64:/web-back/
The authenticity of host '192.168.0.64 (192.168.0.64)' can't be established.
ECDSA key fingerprint is
SHA256:NN12lfXOanqEZuFwR2k6PEtsUyliHoiMPXgM2RQ0n7E.
ECDSA key fingerprint is MD5:da:20:18:a8:41:bb:33:0e:8f:87:2c:8a:33:fe:ea:3a.
Are you sure you want to continue connecting (yes/no)? yes#輸入yes。
Warning: Permanently added '192.168.0.64' (ECDSA) to the list of known hosts.
rget1@192.168.0.64's password: #輸入rget1用戶密碼。
sending incremental file list
./
sent 29 bytes received 15 bytes 3.03 bytes/sec
total size is 0 speedup is 0.00
實驗開始之前首先客戶端服務器端關閉防火墻firewalld否則實驗失?。?/span>
[root@Linux 1~]# systemctl stop firewalld.service
[root@Linux 2~]# systemctl stop firewalld.service
使用非系統用戶進入備份數據,依靠系統中的rsync配置文件/etc/rsyncd.conf進行備份數據,并創建備份賬戶,最終把rsync以deamon方式運行。
用配置文件定義目錄輸出。
服務器/備份端:Linux 2
客戶端/數據源:Linux 1
[root@Linux 2~]# vim /etc/rsyncd.conf#文件不存在,需要手動創建。
注意:#會出現因注釋不兼容的問題而導致推數據時出現報錯,配置文件需取消注釋或者注釋進行換行處理。
uid = root #運行進程的身份。
gid = root #運行進程的組。
address =192.168.1.64 #監聽IP。
port = 873 #監聽端口。
hosts allow =192.168.1.0/24 #允許同步客戶端的IP地址,可以是網段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0
use chroot = yes #是否囚牢,鎖定家目錄,rsync被黑之后,黑客無法再rsync運行的家目錄之外創建文件,選項設置為yes。
max connections = 5 #最大連接數。
pid file =/var/run/rsyncd.pid #進程PID,自動生成。
lock file =/var/run/rsync.lock #指max connectios參數的鎖文件。
log file =/var/log/rsyncd.log #日志文件位置。
motd file =/etc/rsyncd.motd #客戶端登陸之后彈出的消息,需要創建。
[wwwroot] #共享模塊名稱。
path = /web-back/ #路徑。
comment = used for web-data root #描述。
read only = false #只讀權限為假。
list = yes #是否允許查看模塊信息。
auth users = rsyncuser #備份的用戶,和系統用戶無關。
secrets file =/etc/rsync.passwd #存放用戶的密碼文件,格式是 用戶名:密碼。
自己手寫配置文件,不寫注釋
創建提示文件和用戶密碼。
#編輯歡迎信息文件。
[root@Linux 2 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
[root@Linux 2 ~]# vim /etc/rsync.passwd #創建并編寫密碼文件。
rsyncuser:password123 #格式為 用戶名稱:密碼。
[root@Linux 2 ~]# chmod 600 /etc/rsync.passwd
#密碼文件權限必須是700或者600,否則的話身份驗證會失效。
重新啟動服務
[root@Linux 2~]#systemctl restart rsyncd
[root@Linux 2 ~]# netstat -antup | grep :873#查看873端口是否監聽。
tcp 0 0:::873 :::* LISTEN 45089/xinetd
測試
rsync語法:rsync選項用戶名@備份源服務器IP::共享模塊名目標目錄
[root@Linux 1 ~]# rsync -avz /var/www/html rsyncuser@192.168.0.64::wwwroot
Welcome to Backup Server
Password:#輸入密碼password123
密碼處理
新建一個文件保存好密碼,然后在rsync命令中使用--password-file指定此文件即可
[root@Linux 1~]# vim /etc/rsync.passwd#創建密碼文件,其中中需要指定密碼即可。
password123
[root@Linux 1~]# chmod 600 /etc/rsync.passwd#賦予密碼文件600權限。
[root@Linux 1~]#rsync-avz /var/www/html \\
rsyncuser@192.168.0.64::wwwroot --password-file=/etc/rsync.passwd推push
[root@Linux 1~]#rsync -azP rsyncuser@192.168.1.202::wwwroot /opt --password-
file=/etc/rsync.passwd拉pull
[root@Linux 1 ~]# vim autobackup.sh#編寫腳本。
#!/bin/bash
rsync -avz /var/www/html \\
rsyncuser@192.168.1.64::wwwroot -- password-file=/etc/rsync.passwd
[root@Linux 1 ~]# chmod +x autobackup.sh#賦予腳本執行權限。
[root@Linux 2 ~]# rm -rf /web-back/*
[root@Linux 1 ~]# sh autobackup.sh#測試腳本。
定義計劃任務實現定時備份:
[root@Linux 1 ~]# echo "01 3 * * * sh /root/autoback.sh &" >> /var/spool/cron/root
通過一系列如關閉 selinux、配置 rsync 選項、設置用戶權限、編寫配置文件等操作,以及利用腳本和計劃任務實現定時自動備份,我們成功搭建起基于 rsync 的高效數據備份方案。在實際應用中,合理運用這些知識,能為 Linux 系統的數據安全與管理提供有力保障。
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP