mysql自动备份脚本

#!/bin/bash
DBName=news
DBUser=root
DBPasswd=
BackupPath=/daily/mysqlback/
LogFile=/daily/mysqlback/db.log
DBPath=/www/server/mysql-4.1.9/var/news/
BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar



NewFile="$BackupPath"news$(date +%y%m%d).tar.gz
DumpFile="$BackupPath"news$(date +%y%m%d)
#OldFile="$BackupPath"news$(date +%y%m%d --date='5 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
find $BackupPath -atime +5 -name "news*" -exec rm {} /;

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 for mysqldump!" >> $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
else
mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success for mysqlhotcopy!" >> $LogFile
rm -rf $DumpFile
;;
tar)
/www/server/mysql-4.1.9/bin/mysqladmin shutdown >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/www/server/mysql-4.1.9/bin/mysqld_safe --user=mysql & >/dev/null 2>&1
echo "[$NewFile]Backup Success for tar!" >> $LogFile
;;
esac
fi

echo "-------------------------------------------" >> $LogFile
 

你可能感兴趣的:(Linux)