日期:2014-05-16  浏览次数:20730 次

liunx下实现mysql自动定时备份

编写shell脚本.


#!/bin/sh
#
#This is a ShellScript For Auto DB Backup
#Powerd by hiker
#2012-02

DBHost=127.0.0.1
DBName=test
DBUser=root
DBPassword=123456
BackupPath=/data/mysqldata/
LogFile=/data/log/backup.log
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#Setting End

NewFile="$BackupPath$DBName"$(date +%Y%m%d).sql
DumpFile="$BackupPath$DBName"$(date +%Y%m%d).sql
OldFile="$BackupPath$DBName"$(date +%Y%m%d --date='5 days ago').sql

#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
 mysqldump -h $DBHost -u $DBUser -p$DBPassword $DBName>$DumpFile
fi


添加定时处理


vi etc/crontab
01 7 * * * root /data/sh/backupmysql //每天早上7点1分执行
/sbin/service crond restart