標籤:rsync 全網備份企業案例
某公司裡有一台web伺服器,裡面的資料很重要,但是如果硬碟花了,資料就會丟失,現在領導要求你把資料在其他的伺服器上做一個周期性定時備份,要求如下:
每天晚上00點鐘在web伺服器A上打包備份網站目錄並通過rsync命令推送到伺服器B上備份保留(備份思路可以是按日期打包,然後再推到備份伺服器上。)
具體要求如下
1、 web伺服器A和備份伺服器B的備份目錄必須都是/backup
2、 web伺服器網站目錄假定(/var/www/html)
3、 web伺服器本地僅保留7天內的備份
4、 備份伺服器上檢查備份結果是否正常,並將每天的備份結果發送給管理員信箱
5、 備份伺服器上每周6的資料都保留,其他備份僅僅保留180天備份
客服端
cd / &&\
ip=`ifconfig eth1|awk -F "[:]+" ‘NR==2{print $4}‘`
mkdir /backup/$ip -p
tar zcfh /backup/$ip/bak_$(date+%F_%w).tar.gz var/www/html app/logs var/spool/cron/root
etc/rc.local
&&\
find /backup/$ip -type f -name"*$(date +%F_%w).tar.gz"|xargs md5sum >/backup/$ip/flag_$(date+%F_%w).
txt &&\
rsync -av /backup/$ip [email protected]::backup/--password-file=/etc/rsync.password
find /backup/$ip -type f -mtime +7-name "*.tar.gz"|xargs rm -fr
cd / &&\
第一條命令切換到上級目錄
ip=`ifconfig eth1|awk -F "[: ]+"‘NR==2{print $4}‘`
mkdir /backup/$ip -p
因為我們類比的是50台伺服器要區分打包的檔案是那台伺服器的所以我們取沒台伺服器的IP作為目錄把去出的IP數字變數為ip 建立以ip命名的目錄
tar zcfh /backup/$ip/bak_$(date +%F_%w).tar.gzvar/www/html app/logs var/spool/cron/root etc/rc.local
打包我們要備份的資料,打包到上一步建立的以IP為目錄下面,因為要刪除備份伺服器180天前的備份資料
,為了區分是哪天備份的資料所以把包名以date+%F日期命名,題目還要求保留每周六的所以加了%w列印出周幾以方便以後操作
rsync -av /backup/[email protected]::backup/ --password-file=/etc/rsync.password
把打包的資料推送到服務端要讓rsync成免Cipher 模式
find /backup/$ip -type f -name "*$(date+%F_%w).tar.gz"|xargs md5sum >/backup/$ip/flag_$(date +%F_%w)
尋找出當天打包的資料並附上md5sum,同時把產生的md5sum值儲存到flag_$(date +%F_%w)下,給服務端對比md5sum值用
find /backup/$ip -type f -mtime +7 -name"*.tar.gz"|xargs rm -fr
因為目前的目錄只用保留七天的備份資料所以刪除七天以前的資料
服務端
LANG=en
flag_num=/tmp/check_$(date +%F).txt
find /backup/ -type f -name"flag_$(date +%F_%w).txt"|xargs md5sum -c|grep FAILED&>$flag_num
if [ `cat $flag_num|wc -l` -gt 0];then
mail -s "$(date +%F\ %T) backup isfail!!" [email protected] <$flag_num
else
echo "backup is ok"|mail -s"$(date +%F\ %T) backup is successful" [email protected]
fi
#find /backup/172.16.1.31/ -type f -mtime -7 \! -name "bak*_6.tar.gz"|xargs rm -fr
LANG=en
把字元集調成英文
flag_num=/tmp/check_$(date +%F).txt
做一個變數把/tmp/check_$(date+%F).txt變數成flag_num
find /backup/ -type f -name "flag_$(date+%F_%w).txt"|xargs md5sum -c|grep FAILED &>$flag_numif [ `cat$flag_num|wc -l` -gt 0 ];then
對比用戶端打包的md5sum值如服務端的md5sum有沒有變化,沒有變化會輸出成FAILED然後用grep過濾出來檔案內容是空,md5sum值如有變化檔案不會變裡面則有內容,最後查看檔案裡的內容,是空的則代表沒有問題,如有內容代表包出了故障
mail -s"$(date +%F\ %T) backup is fail!!" [email protected]<$flag_numelse
如果裡面有內容就會向設定的郵箱發報錯資訊,
echo"backup is ok"|mail -s "$(date +%F\ %T) backup issuccessful" [email protected]
fi
如果沒有內容就會向設定郵箱發ok資訊
find /backup/172.16.1.31/ -type f -mtime +180 \! -name "bak*_6.tar.gz"|xargs rm -fr
按題目最後要求刪除180天以前的資料,但周六的永久儲存
想要發郵件到自己的郵箱必須要在/etc/mail.rc設定檔新增內容
set [email protected] smtp=smtp.163.comsmtp-auth-user=15855157334 smtp-auth-password=cai1234 s
mtp-auth=login
rsync 全網備份企業案例