#!/bin/bash
source ~/.bash_profile
#Setting
#dbname
DBName="drpboss"
DBUser=account
DBPasswd=mypwd
BackupPath=/data/
LogFile=/log/$DBName.log
DBPath=/mysql/
BackupMethod=mysqldump
NewFile=$DBName$(date +%Y%m%d).tar.gz
DumpFile=$DBName$(date +%Y%m%d).sql
OldFile="$BackupPath"$DBName$(date +%Y%m%d --date=‘7 days ago‘).tar.gz
echo "-------------------------------------------" >> $LogFile
echo $(date +"%Y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
cd $BackupPath
if [ -f $OldFile ]
then
rm -f $OldFile >> $LogFile 2>&1
echo "[$OldFile]Delete Old File Success!" >> $LogFile
else
echo "[$OldFile]No Old Backup File!" >> $LogFile
fi
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can‘t Backup!" >> $LogFile
else
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser --opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
fi
echo "-------------------------------------------" >> $LogFile