Shell脚本——MySql数据库备份

2016-7-7 邪哥

清理资料发现的一个历史小脚本,也是之前搜索资料  参考其他人的脚本进行修改的

由于历史久远,这边无法给出原始参考出处

用于单机的 mysql数据库 备份,可用于定时备份任务

弃之可惜,小记于此

#!/bin/bash 
#This is a ShellScript For Auto DB Backup 
#Powered by aspbiz Changed by Jeen
#2004-09 Changed 2014-12

#Setting 
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置
DBName="test"
DBUser="root"
DBPasswd="root"
BackupPath=/data/backup/mysql
LogFile=/data/backup/mysql/bak_$DBName.log
#Setting End 

NewFile=$BackupPath/"$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 
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 
echo "--------------------------" >> $LogFile 
if [ -f $NewFile ] ; then 
    echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile 
else 
    cd $BackupPath
    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

如果您是原脚本的作者,对本文有任何意见或建议,请留言

:)

发表评论: