自动备份MSQL数据库脚本超级脚本1.0.1修改版 (Linux shell)

偶昨天写的自动备份MSQL数据库脚本超级修改版
Version 1.01
加入了文件校验机制,同时完善日志信息。

如何使用本脚本?
使用FTP ASCII方式上传本脚本到Linux服务器。(或直接用SSH的vim在线建立脚本)
设置其权限可执行后执行其即可。
如果需要定时运行,加入crontab

本脚本正常运行需要mysqldump ,gzip, mutt .


#!/bin/bash
# BEGINNING

# Linux Shell MySQL Automaticly Backup Script By cxy152376
# Version 1.01
# 2007.11.13
# Script HomePage: http://cxy152376.org/project/linux-shell
# This script will backup your MySQL databases(3 most) and send backup files to your email
# 请使用FTP ASCII方式将本脚本上传,并设置其权限为可执行
# 请确保脚本所在目录可以写入,脚本将在备份过程中产生临时文件
# 你可以把本脚本添加到 crontab中实现定时自动运行
# Powered by cxy152376 ( http://cxy152376.org)
# 2007.11
# This script have been tested in Dreamhost SSH

# 将以下数据替换为你的.
# 最多可以备份3个数据库
# 如果你只备份1个或两个数据库,则只需要将DATABASENUM设为1或2
# 则脚本只备份 DB1 或 DB1 和 DB2
# MYNAME是你的名字,将出现在发送给你的邮件标题
# BACKOPTION是 mysqldump备份选项,除非很了解,不要更改
# CHECKFILE如果为1,脚本将校验生成的背份文件MD5

#警告!下面的这块设置请只使用英文/数字,并且不要有任何空格,引号,双引号等特殊字符,否则后果自负!

#————————————
DATABASENUM=3
HOST1=”mysql.yourdomain.com”
HOST2=”localhost”
HOST3=”127.0.0.1″
USER1=”user”
USER2=”user”
USER3=”user2″
PASS1=”password”
PASS2=”password3″
PASS3=”password3″
DB1=”database1name”
DB2=”database2name”
DB3=”datavase3name”
EMAIL=”[email protected]
MYNAME=”cxy152376″
#————————————-

# BACKOPTION=”–opt –compatible=mysql40″
# BACKOPTION=”–opt –compatible=mysql40 –add-locks”
BACKOPTION=”–opt”
CHECKFILE=1

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

# 如果当前目录不可写入,脚本将自动退出而无任何输出
if
[ ! -w$PWD ]
then
printf “Error,can’t write files to tempdir $PWD \n–Done nothing. \n —Linux Shell MySQL Automaticly Backup Script By cxy152376 \n —Version 1.01 -2007.11.13 \n — script powered by cxy152376 ( http://cxy152376.org )” | mutt $EMAIL -s “Failed to backup $MYNAME $DATABASENUM website mysql”
exit
fi
#[ -w $PWD ] || exit

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

if
[ ! -w$BACKUPDIR -o ! -r$BACKUPDIR ]
then
printf “Error,don’t have right to read or write files from tempdir $BACKUPDIR \n–Done nothing. \n —Linux Shell MySQL Automaticly Backup Script By cxy152376 \n —Version 1.01 -2007.11.13 — script powered by cxy152376 ( http://cxy152376.org )” | mutt $EMAIL -s “Failed to backup $MYNAME $DATABASENUM website mysql”
exit
fi

cd $BACKUPDIR

#初始化
echo “$MYNAME mysql backup” > website_backup.txt
echo “Linux Shell MySQL Automaticly Backup Script By cxy152376 ” >> website_backup.txt
echo “Version 1.01 -2007.11.13 ” >> website_backup.txt
date >> website_backup.txt
echo ” ” >> website_backup.txt
echo “—-Log beginning—–” >> website_backup.txt
echo “You have chosed to backup $DATABASENUM mysql databases,” >> website_backup.txt
echo “Using the command ‘mysqldump $BACKOPTION -uUSER -ppASSER -h HOST DATABASENAME > BACKUP.sql'” >> website_backup.txt
mysql –version >> website_backup.txt
echo ” ” >> website_backup.txt

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

# 尝试备份第一个数据库
echo “Start trying to backup the First MySQL database:” >> website_backup.txt
echo “Using the follow information , MySQL Server Host : $HOST1 ; MySQL User : $USER1 ; User Password : ****** ; Databasename : $DB1 ” >> website_backup.txt
mysqldump $BACKOPTION -u$USER1 -p$PASS1 -h $HOST1 $DB1 > backup1.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS backing up $DB1 of $HOST1 to sql file” >> website_backup.txt
else
echo “Failed to back up $DB1 of $HOST1 to sql file” >> website_backup.txt
STATUS=1
fi

echo ” ” >> website_backup.txt

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

if [ $STATUS -eq 0 ]
then

echo “Start trying to backup the Second MySQL database:” >> website_backup.txt
echo “Using the follow information , MySQL Server Host : $HOST2 ; MySQL User : $USER2 ; User Password : ****** ; Databasename : $DB2 ” >> website_backup.txt
mysqldump $BACKOPTION -u$USER2 -p$PASS2 -h $HOST2 $DB2 > backup2.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS backing up $DB2 of $HOST2 to sql file” >> website_backup.txt
else
echo “Failed to back up $DB2 of $HOST2 to sql file” >> website_backup.txt
STATUS=1
fi

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

fi
fi

echo ” ” >> website_backup.txt

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

if [ $STATUS -eq 0 ]
then

echo “Start trying to backup the Third MySQL database:” >> website_backup.txt
echo “Using the follow information , MySQL Server Host : $HOST3 ; MySQL User : $USER3 ; User Password : ****** ; Databasename : $DB3 ” >> website_backup.txt
mysqldump $BACKOPTION -u$USER3 -p$PASS3 -h $HOST3 $DB3 > backup3.sql 2>>website_backup.txt
if [ $? -eq 0 ]
then
echo “SUCCESS backing up $DB3 of $HOST3 to sql file” >> website_backup.txt
else
echo “Failed to back up $DB3 of $HOST3 to sql file” >> website_backup.txt
STATUS=1
fi

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

fi
fi

echo ” ” >> website_backup.txt

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

if [ $STATUS -eq 0 ]
then
echo “Start trying to compress backup file of the First MySQL database” >> website_backup.txt
gzip backup1.sql 2>> website_backup.txt
[ $? -ne 0 ] && $STATUS=1

if [ $DATABASENUM -eq 2 ] || [ $DATABASENUM -eq 3 ]
then
echo “Start trying to compress backup file of the Second MySQL database” >> website_backup.txt
gzip backup2.sql 2>> website_backup.txt
[ $? -ne 0 ] && $STATUS=1
fi

if [ $DATABASENUM -eq 3 ]
then
echo “Start trying to compress backup file of the Third MySQL database” >> website_backup.txt
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 “Error happened,Failed to compress sql files” >> website_backup.txt

fi

echo ” ” >> website_backup.txt

# 如果压缩过程没有错误,则开始发送邮件。
# 邮件是否发送成功将不会进行检测
if [ $STATUS -eq 0 ]
then
mv backup1.sql.gz $TIL1.sql.gz
if
[ $DATABASENUM -eq 2 ] || [ $DATABASENUM -eq 3 ]
then
mv backup2.sql.gz $TIL2.sql.gz
fi
[ $DATABASENUM -eq 3 ] && mv backup3.sql.gz $TIL3.sql.gz

if [ $CHECKFILE -eq 1 ]
then
echo “Begin to check files” >> website_backup.txt
ls -lh $PWD >> website_backup.txt
echo ” ” >> website_backup.txt
for FILE in `ls $PWD`
do
md5sum $FILE >>website_backup.txt
done
echo “Finish to check files” >> website_backup.txt
echo ” ” >> website_backup.txt
fi

echo “All Success,Begin to send mail” >> website_backup.txt
echo “—-Log END—–” >> website_backup.txt
echo ” ” >> website_backup.txt
echo ” script powered by cxy152376 ” >> website_backup.txt
echo “http://cxy152376.org ” >> 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 "Haveing Error, done nothing,Begin to send mail" >> website_backup.txt
echo “—-Log END—–” >> website_backup.txt
echo ” ” >> website_backup.txt
echo ” script powered by cxy152376 ” >> website_backup.txt
echo “http://cxy152376.org ” >> 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数据库脚本超级脚本1.0.1修改版 (Linux shell)”


Comments are currently closed.