您好,登錄后才能下訂單哦!
#!/bin/bash # # Description: 該腳本功能,以周為單位循環;周五實行完全備份,周六不備份,周日至周四實行差異備份; # 因工作需要,不實行壓縮打包的方式,采用cp完全備份,rsync差異備份; # 若要使用該腳本,請直接修改下面的兩個參數:BackupPath和DATAPath,其他地方請不要修改; # DATE: 2017-04-24 22:43:00 # MODIFY DATE: 2017-08-28 17:20:00 # VERSION: 2.0 # 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/# 指定備份路徑 MountPath=//ds-bk01/drv-y# 指定windwos備份服務器共享路徑 DATAPath=/data/ # 需要備份的目錄 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 's@[^/]+/?$@@g'`# 定義日志文件所在的目錄 DirFile=`ls $DATAPath`# 將備份路徑下的文件及目錄保存至變量DirFile中 FirstFile="${BackupPath}${Host}_${Time}/`basename ${DATAPath}`/" # 根據時間定義當前備份的目錄 OldFirstFile="${BackupPath}${Host}_$(date +%Y-%m-%d --date='7 days ago')"# 定義7天前的備份目錄 Mail_Group="test@aaa.com" # 定義收件人 username=user # 定義掛載備份服務器的用戶名 password=password # 定義掛載備份服務器的密碼 # 記錄日志文件的開頭部分 Head() { [ ! -d $LogDir ] && mkdir $LogDir echo "[ 日文 ] このメッセ?ジはAB Linuxサ?バを介して、このメッセ?ジを介してs-smb01サ?バのデ?タバックアップを??するために送信されます。" > $LogFile echo "[ 中文 ] 這封郵件是通過AB的linux服務器發出來的,通過這封郵件來確認s-smb01服務器的數據備份情況!" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup starts----------------------" >> $LogFile echo >> $LogFile 2>&1 echo "Start Backup $DATAPath $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup starts----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 } # 日志的結束部分 end(){ echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo " $DATAPath backup ends $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 } # 完全備份 FullBackup(){ echo "Today is week: `date +%a`, Start performing a full backups..." >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 if [ -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 cd $DATAPath for File in $DirFile; do cp -a ${File} $FirstFile >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi done else if [ ! -d $FirstFile ];then mkdir -pv $FirstFile >> $LogFile 2>&1 cd $DATAPath for File in $DirFile; do cp -a ${File} $FirstFile >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi done else echo "[ $DATAPath ]The Backup File is exists,Can't Backup!`date`" >> $LogFile 2>&1 fi fi } # 差異備份 DiffBackup() { echo "Today is week: `date +%a`, Start performing differential backups..." >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 if [ -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}/./{} $FirstFile if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi else 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 fi fi } # 日志開始部分 Head # 判斷是否掛載//ds-bk01/drv-y,如果沒有則掛載,如果掛載不成功則退出腳本,并記錄至日志文件 mount | grep $MountPath >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ $Host ] has successfully installed backup server shared directories: [ $MountPath ] to local: [ $BackupPath ] ... " >> $LogFile 2>&1 echo >> $LogFile 2>&1 else echo "[ $Host ] does not mount a backup server shared directory: [ $MountPath ] ..." >> $LogFile 2>&1 mount.cifs -o username=$username,password=$password,dir_mode=0777,file_mode=0777 $MountPath $BackupPath if [ $? -eq 0 ]; then echo "Mount successfully!" >> $LogFile 2>&1 else echo "[ $Host ] failed to mount backup server shared directory: [ $MountPath ] to local: [ $BackupPath ] ..." >> $LogFile 2>&1 echo "Mount again, please!" >> $LogFile 2>&1 echo "The shell script has exited..." >> $LogFile 2>&1 end mail -s "From [ $Host ] backup information [ $Time ]" $Mail_Group < $LogFile exit 8 fi fi # 備份策略的變量 a=`date +%u`# 定義Today是星期幾 b=7 # 定義多少天循環一次完全備份和差異備份,這里定義為7天循環一次,如果要增大循環天數,請修改此處 c=5# 這里5代表是周五完全備份,如果要改成周六完全備份,請修改為6,注意:c與d的值不能相同 d=6# 定義每周6不備份數據,注意:c與d的值不能相同 # 注意,下面的語句很重,判斷Today是否大于$c,定義變量ago將采用不同的表達式計算 if [ "$a" -gt "$c" ]; then ago=$[ $a - $c ]# 定義Today離$c有多少天 else ago=$[ $a + $b - $c ] # 定義Today離$c有多少天 fi 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" ]; then FullBackup elif [ "$a" -eq "$d" ]; then echo "Today is Saturday 6, according to the backup strategy, today no data backup." >> $LogFile 2>&1 else DiffBackup fi # 日志結束部分 end # 備份完成后發送郵件至指定郵箱 mail -s "From [ $Host ] backup information [ $Time ]" $Mail_Group < $LogFile exit 0
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。