mysql备份脚本(转)

#!/bin/bash
#Setting start
DBName=mysqldb_name#数据库名称
DBUser=root#数据库账号
DBPasswd=passwd#密码
BackupPath=/data/backup/
LogFile=/data/backup/dbbackup.log
DBPath=/usr/local/mysql/bin/
BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupPath=/usr/local/mysql/backup/hotcopy/
BackupCommand=$DBPath$BackupMethod

#Setting End

NewFile="$BackupPath"db_"$DBName"_$(date +%y%m%d)."tar".gz
DumpFile="$BackupPath"db_"$DBName"_$(date +%y%m%d)
OldFile="$BackupPath"db_"$DBName"_$(date +%y%m%d --date='7 days ago')."tar".gz

echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "----------starting backup [$DBName]----------------" >> $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 $NewFile ]; then
   echo "[$NewFile] The Backup File is exists,Can't Backup!" >> $LogFile
else
   case $BackupMethod in
        mysqldump)
            if [ -z $DBPasswd ]; then
               $BackupCommand -u $DBUser --opt --extended-insert --max_allowed_packet=2147000000
$DBName > $DumpFile
            else
               $BackupCommand -u $DBUser -p$DBPasswd --opt --extended-insert --max_allowed_packet
=2147000000 $DBName > $DumpFile
            fi
            if [ -f $DumpFile ]; then
               tar czvf $NewFile $DumpFile >> $LogFile 2>&1
               echo "[$NewFile]Backup Success!" >> $LogFile
               rm -rf $DumpFile
               chown daemon.daemon $NewFile
            else
               echo "Backup Error" >> $LogFile
            fi

        ;;
        mysqlhotcopy)
            rm -rf $DumpFile
            mkdir $DumpFile
            if [ -z $DBPasswd ]; then
               $BackupCommand -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
            else
               $BackupCommand -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
            fi
            if [ -f $DumpFile ]; then
               tar czvf $NewFile $DumpFile >> $LogFile 2>&1
               echo "[$NewFile]Backup Success!" >> $LogFile
               rm -rf $DumpFile
            else
               echo "Backup Error" >> $LogFile
            fi
        ;;
        *)
            echo "please select backup method" >> $LogFile
        ;;
    esac
fi

echo "----------ending backup [$DBName]----------------" >> $LogFile


你可能感兴趣的:(mysql,备份)