標籤:完全備份資料和差異備份資料的shell指令碼
#!/bin/bash ## Description: 該指令碼功能,以周為單位迴圈;周五實行完全備份,周六不備份,周日至周四實行差異備份;# 因工作需要,不實行壓縮打包的方式,採用cp完全備份,rsync差異備份;# 若要使用該指令碼,請直接修改下面的兩個參數:BackupPath和DATAPath,其他地方請不要修改;# DATE: 2017-04-24 22:43:00# MODIFY DATE: 2017-04-28 10:00:00 # VERSION: 1.1# Author: xixi_chan# 匯入PATH環境變數export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin:/root/bin# 重新定義IFS變數避免目錄或檔案名稱存在空格時被分割IFS=`echo -en "\n\b"`# 定義全域變數BackupPath=/mnt/backup_data/# 指定備份路徑 DATAPath=/data/ac-cnc/ # 需要備份的目錄 Time=`date +%Y-%m-%d`# 定義Today的時間變數Host=`echo $HOSTNAME | awk -F‘.‘ ‘{print $1}‘`# 定義主機名稱LogFile="/var/log/${Host}/${Host}_${Time}.log" # 定義記錄檔名稱LogDir=`echo "$LogFile" | sed -r ‘[email protected][^/]+/[email protected]@g‘`# 定義記錄檔所在的目錄DirFile=`ls $DATAPath`# 將備份路徑下的檔案及目錄儲存至變數DirFile中FirstFile="${BackupPath}${Host}_${Time}/`basename ${DATAPath}`/" # 根據時間定義當前備份的目錄OldFirstFile="${BackupPath}${Host}_$(date +%Y-%m-%d --date=‘7 days ago‘)"# 定義7天前的備份目錄# 記錄記錄檔的開頭部分Head() {[ ! -d $LogDir ] && mkdir $LogDirecho "---------------------The backup starts----------------------" > $LogFile echo >> $LogFile 2>&1echo "Start Backup $DATAPath $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1 echo >> $LogFile 2>&1echo "---------------------The backup starts----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1echo >> $LogFile 2>&1}# 完全備份FullBackup(){echo "Today is week: `date +%a`, Start performing a full backups..."echo >> $LogFile 2>&1echo >> $LogFile 2>&1if [ -d $OldFirstFile ];then rm -rf $OldFirstFile >> $LogFile 2>&1 echo "[ $OldFirstFile ] Delete Old File Success!`date`" >> $LogFile 2>&1[ ! -d $FirstFile ] && mkdir -pv $FirstFile >> $LogFile 2>&1cd $DATAPathfor File in $DirFile; docp -a ${File} $FirstFile >> $LogFile 2>&1if [ $? -eq 0 ];thenecho "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fidoneelseif [ ! -d $FirstFile ];thenmkdir -pv $FirstFile >> $LogFile 2>&1 cd $DATAPathfor File in $DirFile; docp -a ${File} $FirstFile >> $LogFile 2>&1 if [ $? -eq 0 ];thenecho "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fidoneelseecho "[ $DATAPath ]The Backup File is exists,Can‘t Backup!`date`" >> $LogFile 2>&1 fifi }# 差異備份DiffBackup() {echo "Today is week: `date +%a`, Start performing differential backups..."echo >> $LogFile 2>&1echo >> $LogFile 2>&1if [ -d $OldFirstFile ];then rm -rf $OldFirstFile >> $LogFile 2>&1 echo "[ $OldFirstFile ] Delete Old File Success! `date`" >> $LogFile 2>&1 [ ! -d $FirstFile ] && mkdir -pv $FirstFile >> $LogFile 2>&1 rsync --dry-run -racnC --out-format="%n" $DATAPath $d_dir | grep -v "/$" | xargs -I{} rsync -R -a -z ${DATAPath}/./{} $FirstFileif [ $? -eq 0 ];thenecho "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fielse if [ ! -d $FirstFile ];then mkdir -pv $FirstFile >> $LogFile 2>&1 rsync --dry-run -racnC --out-format="%n" $DATAPath $d_dir | grep -v "/$" | xargs -I{} rsync -R -a -z ${DATAPath}/./{} $FirstFile if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi else echo "[ ${DATAPath} ]The Backup File is exists,Can‘t Backup! `date`" >> $LogFile 2>&1 fifi}# 日誌的結束部分end(){echo >> $LogFile 2>&1echo >> $LogFile 2>&1echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1echo " $DATAPath backup ends $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1echo >> $LogFile 2>&1echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 }a=`date +%u`# 定義Today是星期幾b=7 # 定義多少天迴圈一次完全備份和差異備份,這裡定義為7點迴圈一次,如果要增大迴圈天數,請修改此處c=5# 這裡5代表是周五完全備份,如果要改成周六完全備份,請修改為6d=6# 定義每周6不備份資料ago=$[ $a + $b -$c ]# 定義Today離$c有多少天d_dir=${BackupPath}${Host}_$(date +%Y-%m-%d -d "`echo $ago` days ago")/`basename ${DATAPath}`/#date +%Y-%m-%d -d "`echo $ago` days ago" # 定義備份策略if [ "$a" -eq "$c" ]; thenHeadFullBackupendelif [ "$a" -eq "$d" ]; thenHeadecho "Today is Saturday 6, according to the backup strategy, today no data backup." >> $LogFile 2>&1endelseHeadDiffBackupendfi# 備份完成後發送郵件至指定郵箱Mail_Group="[email protected] [email protected]"mail -s "From $Host backup information" $Mail_Group < $LogFileexit 0
本文出自 “ccschan” 部落格,轉載請與作者聯絡!
完全備份資料和差異備份資料的shell指令碼