写了一个自动备份MSQL数据库脚本超级 (bash shell) (ssh)

使用Shell备份MySQL数据库,然后自动发送到邮箱脚本
为偶在网上找到一个原型后大幅修改而成。

可以备份最多3个数据库

加入了流程控制和错误检验,任何情况下,都能够正常退出并删除所有临时文件。

如果全部成功,将把备份的sql.gz文件和日志信息发送到指定邮箱。
如果中途出现任何错误,将删除任何遗留文件,并将备份日志发送到邮箱。

脚本写的冗长些,可读性也差
不过,应该没有问题。


#!/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=”HOST1″
HOST2=”HOST2″
HOST3=”HOST3″
USER1=”USER1″
USER2=”USER2″
USER3=”USER3″
PASS1=”PASS1″
PASS2=”PASS2″
PASS3=”PASS3″
DB1=”DB1″
DB2=”DB2″
DB3=”DB3″

EMAIL=”[email protected]
MYNAME=”cxy152376″

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

# 如果当前目录不可写入,脚本将自动退出而无任何输出
[ -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

0 Responses to “写了一个自动备份MSQL数据库脚本超级 (bash shell) (ssh)”


Comments are currently closed.