拓?fù)洌?/span>
Linux 1(源主機(jī)192.168.1.63)
Linux 2(目標(biāo)主機(jī)192.168.1.64)
需要關(guān)閉selinux,否則即使創(chuàng)建/etc/rsyncd.conf文件之后去啟動(dòng)服務(wù)也不會(huì)生效。
[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端口是否監(jiān)聽(tīng)。
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 43447/rsync
tcp6 0 0 :::873 :::* LISTEN 43447/rsync
rsync命令選項(xiàng)
-a:--archive archive mode權(quán)限保存模式,相當(dāng)于-rlptgoD參數(shù),存檔,遞歸,保持屬性等。
-z:--compress壓縮模式,當(dāng)資料在傳送到目的端進(jìn)行檔案壓縮。
-H:--hard-links保留硬鏈接文件。
--version:輸出rsync版本。
-v:--verbose復(fù)雜的輸出信息。
-u:--update僅僅進(jìn)行更新,也就是跳過(guò)已經(jīng)存在的目標(biāo)位置,并且文件時(shí)間要晚于要備份的文件,不
覆蓋新的文件。
--port=PORT:定義rsyncd(daemon)要運(yùn)行的port(預(yù)設(shè)為tcp 873)。
--delete:刪除那些目標(biāo)位置有的文件而備份源沒(méi)有的文件。
--password-file=FILE:從指定密碼文件中獲取密碼。
--bwlimit=KBPS:限制I/O帶寬。
--filter “-filename”:需要過(guò)濾的文件。
--exclude=filname:需要過(guò)濾的文件。
--progress:顯示備份過(guò)程。
通常常用的選項(xiàng)–avz
使用系統(tǒng)普通用戶rsync備份數(shù)據(jù)保持原有文件權(quán)限
對(duì)Linux 1網(wǎng)站根目錄的/var/www/html目錄備份到Linux 2的/web-back 。
服務(wù)器:Linux 1
客戶端:Linux 2
1.建立測(cè)試用戶(兩臺(tái)主機(jī)都需要操作):
[root@Linux 2 ~]# useradd rget1 && echo "123456" |passwd --stdin rget1
更改用戶rget1 的密碼。
passwd:所有的身份驗(yàn)證令牌已經(jīng)成功更新。
[root@Linux 1 ~]# useradd rget1 && echo "123456" |passwd --stdin rget1
更改用戶rget1 的密碼。
passwd:所有的身份驗(yàn)證令牌已經(jīng)成功更新。
2.對(duì)目錄賦予ACL權(quán)限:
[root@Linux 1~]# mkdir /var/www/html/ -p#創(chuàng)建目錄。
[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.創(chuàng)建測(cè)試數(shù)據(jù):
[root@Linux 1 ~]# cp -r /boot/* /var/www/html/
#復(fù)制/boot/目錄下所有內(nèi)容到/var/www/html/目錄下。
4.服務(wù)端上操作:
[root@Linux 2 ~]# mkdir /web-back#創(chuàng)建存放數(shù)據(jù)目錄。
[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
使用非系統(tǒng)用戶備份數(shù)據(jù)
實(shí)驗(yàn)開(kāi)始之前首先客戶端服務(wù)器端關(guān)閉防火墻firewalld否則實(shí)驗(yàn)失?。?/span>
[root@Linux 1~]# systemctl stop firewalld.service
[root@Linux 2~]# systemctl stop firewalld.service
使用非系統(tǒng)用戶進(jìn)入備份數(shù)據(jù),依靠系統(tǒng)中的rsync配置文件/etc/rsyncd.conf進(jìn)行備份數(shù)據(jù),并創(chuàng)建備份賬戶,最終把rsync以deamon方式運(yùn)行。
使用rsync服務(wù)進(jìn)行數(shù)據(jù)同步。
用配置文件定義目錄輸出。
服務(wù)器/備份端:Linux 2
客戶端/數(shù)據(jù)源:Linux 1
[root@Linux 2~]# vim /etc/rsyncd.conf#文件不存在,需要手動(dòng)創(chuàng)建。
注意:#會(huì)出現(xiàn)因注釋不兼容的問(wèn)題而導(dǎo)致推數(shù)據(jù)時(shí)出現(xiàn)報(bào)錯(cuò),配置文件需取消注釋或者注釋進(jìn)行換行處理。
uid = root #運(yùn)行進(jìn)程的身份。
gid = root #運(yùn)行進(jìn)程的組。
address =192.168.1.64 #監(jiān)聽(tīng)I(yíng)P。
port = 873 #監(jiān)聽(tīng)端口。
hosts allow =192.168.1.0/24 #允許同步客戶端的IP地址,可以是網(wǎng)段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0
use chroot = yes #是否囚牢,鎖定家目錄,rsync被黑之后,黑客無(wú)法再rsync運(yùn)行的家目錄之外創(chuàng)建文件,選項(xiàng)設(shè)置為yes。
max connections = 5 #最大連接數(shù)。
pid file =/var/run/rsyncd.pid #進(jìn)程PID,自動(dòng)生成。
lock file =/var/run/rsync.lock #指max connectios參數(shù)的鎖文件。
log file =/var/log/rsyncd.log #日志文件位置。
motd file =/etc/rsyncd.motd #客戶端登陸之后彈出的消息,需要?jiǎng)?chuàng)建。
[wwwroot] #共享模塊名稱。
path = /web-back/ #路徑。
comment = used for web-data root #描述。
read only = false #只讀權(quán)限為假。
list = yes #是否允許查看模塊信息。
auth users = rsyncuser #備份的用戶,和系統(tǒng)用戶無(wú)關(guān)。
secrets file =/etc/rsync.passwd #存放用戶的密碼文件,格式是 用戶名:密碼。
自己手寫配置文件,不寫注釋
創(chuàng)建提示文件和用戶密碼。
#編輯歡迎信息文件。
[root@Linux 2 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
[root@Linux 2 ~]# vim /etc/rsync.passwd #創(chuàng)建并編寫密碼文件。
rsyncuser:password123 #格式為 用戶名稱:密碼。
[root@Linux 2 ~]# chmod 600 /etc/rsync.passwd
#密碼文件權(quán)限必須是700或者600,否則的話身份驗(yàn)證會(huì)失效。
重新啟動(dòng)服務(wù)
[root@Linux 2~]#systemctl restart rsyncd
[root@Linux 2 ~]# netstat -antup | grep :873#查看873端口是否監(jiān)聽(tīng)。
tcp 0 0:::873 :::* LISTEN 45089/xinetd
測(cè)試
rsync語(yǔ)法:rsync選項(xiàng)用戶名@備份源服務(wù)器IP::共享模塊名目標(biāo)目錄
[root@Linux 1 ~]# rsync -avz /var/www/html rsyncuser@192.168.0.64::wwwroot
Welcome to Backup Server
Password:#輸入密碼password123
密碼處理
新建一個(gè)文件保存好密碼,然后在rsync命令中使用--password-file指定此文件即可
[root@Linux 1~]# vim /etc/rsync.passwd#創(chuàng)建密碼文件,其中中需要指定密碼即可。
password123
[root@Linux 1~]# chmod 600 /etc/rsync.passwd#賦予密碼文件600權(quán)限。
[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
腳本實(shí)現(xiàn)定時(shí)自動(dòng)備份
[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#賦予腳本執(zhí)行權(quán)限。
[root@Linux 2 ~]# rm -rf /web-back/*
[root@Linux 1 ~]# sh autobackup.sh#測(cè)試腳本。
定義計(jì)劃任務(wù)實(shí)現(xiàn)定時(shí)備份:
[root@Linux 1 ~]# echo "01 3 * * * sh /root/autoback.sh &" >> /var/spool/cron/root
通過(guò)一系列如關(guān)閉 selinux、配置 rsync 選項(xiàng)、設(shè)置用戶權(quán)限、編寫配置文件等操作,以及利用腳本和計(jì)劃任務(wù)實(shí)現(xiàn)定時(shí)自動(dòng)備份,我們成功搭建起基于 rsync 的高效數(shù)據(jù)備份方案。在實(shí)際應(yīng)用中,合理運(yùn)用這些知識(shí),能為 Linux 系統(tǒng)的數(shù)據(jù)安全與管理提供有力保障。
藍(lán)隊(duì)云官網(wǎng)上擁有完善的技術(shù)支持庫(kù)可供參考,大家可自行查閱,更多技術(shù)問(wèn)題,也可以直接咨詢。同時(shí),藍(lán)隊(duì)云整理了運(yùn)維必備的工具包免費(fèi)分享給大家使用,需要的朋友可以直接咨詢。更多技術(shù)知識(shí),藍(lán)隊(duì)云期待與你一起探索,助力你在 Linux 運(yùn)維之路上穩(wěn)步前行。