Mysql 通用备份脚本

#!/bin/bash
for DBName in atrium beisen_sns blog cacti ucenter ultrax wiki wordpress wordpressnew wordpress_pingtai zentao extmail
do
DBUser=root
DBPasswd=beisen
BackupPath=/mysqlbackup/
LogFile=/mysqlbackup/"$DBName"$(date +%y%m%d).log
#DBPath=/var/lib/mysql/
BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting End
NewFile="$BackupPath"$DBName$(date +%y%m%d).tgz
DumpFile="$BackupPath"$DBName$(date +%y%m%d)
OldFile="$BackupPath"$DBName$(date +%y%m%d --date='5 days ago').tgz
#OldFile="$Mysql_bk_dir"/$i$(date -v -5d +%Y%m%d).tgz                                  #freebsd
OldLogFile="$BackupPath"$DBName$(date +%y%m%d --date='5 days ago').log

echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
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 $OldLogFile ]
then
   rm -f $OldLogFile >> $LogFile 2>&1
   echo "[$OldLogFile]Delete Old File Success!" >> $LogFile
else
   echo "[$OldLogFile]No Old Backup File!" >> $LogFile
fi

if [ -f $NewFile ]
then
   echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile
else
   case $BackupMethod in
   mysqldump)
      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
      ;;
   esac
fi
sleep 20
echo "-------------------------------------------" >> $LogFile
done

你可能感兴趣的:(mysql,备份通用脚本)