#!/bin/bash
BakDir=/backup/mysql_backup
LogFile=/backup/mysql_backup.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DB=ghd
DumpFile=$DB$Date.sql
GZDumpFile=$DB-$Date.sql.tar.gz
if [ -d "$BakDir" ]
then
echo "目录已存在"
else
mkdir $BakDir
fi
if [ -f "$LogFile" ]
then
echo "文件已存在"
else
touch $LogFile
fi
/usr//bin/mysqldump -uroot -p"password" $DB > $DumpFile
if [ -f "$DumpFile" ]
then
/usr/bin/tar czvf $GZDumpFile $DumpFile
/usr/bin/rm $DumpFile
/usr/bin/mv $GZDumpFile $BakDir 2> /dev/null
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
else
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile err >> $LogFile
fi
#清理备份,保留30天的备份
find $BakDir/* -mtime +30 -exec rm {} \;
echo "Cleared backup"