一个Mysql自动备份脚本
2007-11-11 13:50:52 来源:WEB开发网核心提示:可以将这个脚本放进crontab,每天凌晨执行一次,一个Mysql自动备份脚本,自动备份这个脚本每天最多只执行一次,而且只保留最近五天的备份在服务器上,以及备份方式 #默认情况下备份方式是tar,还可以是MySQL(和PHP搭配之最佳组合)dump,MySQL(和PHP搭配之最佳组合)dotcopy #默认情况下,
可以将这个脚本放进crontab,每天凌晨执行一次,自动备份这个脚本每天最多只执行一次,而且只保留最近五天的备份在服务器上。
代码:
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2004-09
#Setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是MySQL(和PHP搭配之最佳组合)dump,MySQL(和PHP搭配之最佳组合)dotcopy
#默认情况下,用root(空)登录MySQL(和PHP搭配之最佳组合)数据库,备份至/root/dbxxxxx.tgz
DBName=MySQL(和PHP搭配之最佳组合)
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/MySQL(和PHP搭配之最佳组合)/
#BackupMethod=MySQL(和PHP搭配之最佳组合)dump
#BackupMethod=MySQL(和PHP搭配之最佳组合)hotcopy
#BackupMethod=tar
#Setting End
NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date=’5 days ago’).tgz
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
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can’t Backup!" >> $LogFile
else
case $BackupMethod in
MySQL(和PHP搭配之最佳组合)dump)
if [ -z $DBPasswd ]
then
MySQL(和PHP搭配之最佳组合)dump -u $DBUser --opt $DBName > $DumpFile
else
MySQL(和PHP搭配之最佳组合)dump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
MySQL(和PHP搭配之最佳组合)hotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
MySQL(和PHP搭配之最佳组合)hotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
else
MySQL(和PHP搭配之最佳组合)hotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/MySQL(和PHP搭配之最佳组合)d stop >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/etc/init.d/MySQL(和PHP搭配之最佳组合)d start >/dev/null 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
;;
esac
fi
echo "-------------------------------------------" >> $LogFile
代码:
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2004-09
#Setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是MySQL(和PHP搭配之最佳组合)dump,MySQL(和PHP搭配之最佳组合)dotcopy
#默认情况下,用root(空)登录MySQL(和PHP搭配之最佳组合)数据库,备份至/root/dbxxxxx.tgz
DBName=MySQL(和PHP搭配之最佳组合)
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/MySQL(和PHP搭配之最佳组合)/
#BackupMethod=MySQL(和PHP搭配之最佳组合)dump
#BackupMethod=MySQL(和PHP搭配之最佳组合)hotcopy
#BackupMethod=tar
#Setting End
NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date=’5 days ago’).tgz
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
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can’t Backup!" >> $LogFile
else
case $BackupMethod in
MySQL(和PHP搭配之最佳组合)dump)
if [ -z $DBPasswd ]
then
MySQL(和PHP搭配之最佳组合)dump -u $DBUser --opt $DBName > $DumpFile
else
MySQL(和PHP搭配之最佳组合)dump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
MySQL(和PHP搭配之最佳组合)hotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
MySQL(和PHP搭配之最佳组合)hotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
else
MySQL(和PHP搭配之最佳组合)hotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/MySQL(和PHP搭配之最佳组合)d stop >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/etc/init.d/MySQL(和PHP搭配之最佳组合)d start >/dev/null 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
;;
esac
fi
echo "-------------------------------------------" >> $LogFile
更多精彩
赞助商链接