本站就是采用这段代码,不过目前只能备份三个数据库,需要更多,则需要自行修改

功能:

1.可以同时备份多个MySQL数据库(最多3个)并压缩生成的sql文件
2.如果全过程没有错误,将把备份的文件连同日志信息发往你邮箱
3.如果过程中有任何错误,则停止备份,只把日志信息发往你邮箱

脚本为Bash,调用mysqldump备份,gzip压缩,mutt发送邮件

理论上,可以适用于绝大多数支持ssh的国外虚拟主机(只在DH上测试过)

 

#!/bin/bash
# BEGINNING
# This script will backup your MySQL databases(3 most) and send backup files to your email
# 请使用FTP ASCII方式将本脚本上传,并设置其权限为可执行
# 请确保脚本所在目录可以写入,脚本将在备份过程中产生临时文件
# 你可以把本脚本添加到 crontab中实现定时自动运行
# Powered by cxy152376 (cxy152376.org)
# 2007.11
# This script have been tested in Dreamhost SSH
# 将以下数据替换为你的.
# 最多可以备份3个数据库
# 如果你只备份1个或两个数据库,则只需要将DATABASENUM设为1或2
# 则脚本只备份 DB1 或 DB1 和 DB2
DATABASENUM=3
HOST1=””
HOST2=””
HOST3=””
USER1=””
USER2=””
USER3=””
PASS1=””
PASS2=””
PASS3=””
DB1=””
DB2=””
DB3=””
EMAIL=”xxx@gmail.com
MYNAME=”xxx”

# 不要动下面代码除非你知道自己在做什么。

# 如果当前目录不可写入,脚本将自动退出而无任何输出
[ -w $PWD ] || exit

# 脚本生成一个不会与已有文件重名的目录
# 然后进入这个目录
# 所有临时文件都将在这个目录下生成
TEMPNUM=0
BACKUPDIR=”mysqlbackup”
while
[ -e $BACKUPDIR ]
do
TEMPNUM=$[TEMPNUM+1]
BACKUPDIR=”mysqlbackup_$TEMPNUM”
done
mkdir $BACKUPDIR
cd $BACKUPDIR

#初始化
echo “$MYNAME mysql backup” > website_backup.txt
date >> website_backup.txt
mysql –version >> website_backup.txt
echo ” ” >> website_backup.txt
echo “—-Log begin—–” >> website_backup.txt

STATUS=0
DATE=`date +%Y-%m-%d`
TIL1=”$DB1-$HOST1-$DATE”
TIL2=”$DB2-$HOST2-$DATE”
TIL3=”$DB3-$HOST3-$DATE”

# 尝试备份第一个数据库
mysqldump –opt -u$USER1 -p$PASS1 -h $HOST1 $DB1 > backup1.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS BACKUP $DB1 of $HOST1 to sql file” >> website_backup.txt
else
echo “Failed to BACKUP $DB1 of $HOST1 to sql file” >> website_backup.txt
STATUS=1
fi

# 如果需要,尝试备份第二个数据库
if [ $DATABASENUM -eq 2 ] || [ $DATABASENUM -eq 3 ]
then

if [ $STATUS -eq 0 ]
then

mysqldump –opt -u$USER2 -p$PASS2 -h $HOST2 $DB2 > backup2.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS BACKUP $DB2 of $HOST2 to sql file” >> website_backup.txt
else
echo “Failed to BACKUP $DB2 of $HOST2 to sql file” >> website_backup.txt
STATUS=1
fi

else
echo “Stop to BACKUP $DB2 of $HOST2 to sql file” >> website_backup.txt

fi
fi

# 如果需要,尝试备份第三个数据库
if [ $DATABASENUM -eq 3 ]
then

if [ $STATUS -eq 0 ]
then

mysqldump –opt -u$USER3 -p$PASS3 -h $HOST3 $DB3 > backup3.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS BACKUP $DB3 of $HOST3 to sql file” >> website_backup.txt
else
echo “Failed to BACKUP $DB3 of $HOST3 to sql file” >> website_backup.txt
STATUS=1
fi

else
echo “Stop to BACKUP $DB3 of $HOST3 to sql file” >> website_backup.txt

fi
fi

# 如果以上没有任何错误,压缩生成的sql文件为gz格式

if [ $STATUS -eq 0 ]
then
gzip backup1.sql 2>> website_backup.txt
[ $? -ne 0 ] && $STATUS=1

if [ $DATABASENUM -eq 2 ] || [ $DATABASENUM -eq 3 ]
then
gzip backup2.sql 2>> website_backup.txt
[ $? -ne 0 ] && $STATUS=1
fi

if [ $DATABASENUM -eq 3 ]
then
gzip backup3.sql 2>> website_backup.txt
[ $? -ne 0 ] && $STATUS=1
fi

[ $STATUS -eq 0 ] && echo “SUCCESS compress sql files” >> website_backup.txt
[ $STATUS -eq 1 ] && echo “Failed to compress sql files” >> website_backup.txt

fi

# 如果压缩过程没有错误,则开始发送邮件。
# 邮件是否发送成功将不会进行检测
if [ $STATUS -eq 0 ]
then
mv backup1.sql.gz $TIL1.sql.gz
[ $DATABASENUM -eq 2 ] || [ $DATABASENUM -eq 3 ] && mv backup2.sql.gz $TIL2.sql.gz
[ $DATABASENUM -eq 3 ] && mv backup3.sql.gz $TIL3.sql.gz
echo “All Success,Begin to send mail” >> website_backup.txt
echo “—-Log END—–” >> website_backup.txt

if [ $DATABASENUM -eq 1 ]
then
mutt $EMAIL -s “SUCCESS backup $MYNAME $DATABASENUM mysql databases ” -a $TIL1.sql.gz < website_backup.txt
elif [ $DATABASENUM -eq 2 ]
then
mutt $EMAIL -s “SUCCESS backup $MYNAME $DATABASENUM mysql databases ” -a $TIL1.sql.gz -a $TIL2.sql.gz < website_backup.txt
else
mutt $EMAIL -s “SUCCESS backup $MYNAME $DATABASENUM mysql databases ” -a $TIL1.sql.gz -a $TIL2.sql.gz -a $TIL3.sql.gz < website_backup.txt
fi

else
echo “Have Error, done nothing,Begin to send mail” >> website_backup.txt
echo “—-Log END—–” >> website_backup.txt
mutt $EMAIL -s “Failed to backup $MYNAME website mysql” < website_backup.txt
fi

# 删除一开始生成的临时操作目录
# 不需要一个个删除文件
# delete all files which may have been generated
# [ -f backup1.sql ] && rm backup1.sql
# [ -f backup2.sql ] && rm backup2.sql
# [ -f backup3.sql ] && rm backup3.sql
# [ -f $TIL1.sql.gz ] && rm $TIL1.sql.gz
# [ -f $TIL2.sql.gz ] && rm $TIL2.sql.gz
# [ -f $TIL3.sql.gz ] && rm $TIL3.sql.gz
# [ -f website_backup.txt ] && rm website_backup.txt
cd ..
rm -r $BACKUPDIR

# END