# |------------------------------------------------------|
# | Create by D.W.H 2010.7.13 |
# | Backup extmail to mysql.extmail.tar.gz |
# |------------------------------------------------------|
# crontab
# 30 6 * * * /root/script/mysqlbackup.sh extmail -7
# 10 7 * * 0 /root/script/mysqlbackup.sh dspam -14
# 10 6 1 */3 * /root/script/mysqlbackup.sh --all-database -93
mypasswd=‘yourpassword’ #MYSQL的密码
mydatabase=$1 # 数据库名
dayover=$2 #备份文件保留长
nsuser='username for remote mache' #远程共享服务器的用户名和密码
nspasswd=’password for remote mache’
command=/usr/bin/mysqldump
#把第一个参数以“-”分割并取第三段。--all-database 结果为all.
dateover=`date -d "$2 days" +%Y-%m-%d`
filename="mysql.$f1.$datef.tar.gz" #备份输入压缩后的文件名
overfilename="mysql.$f1.$dateover.tar.gz" #形成超期的文件名
#为了保证数据能正常输出压缩,采用先本地备份成功后移动到远程的方式。
tempfilename="$tempfolder/mysql.$f1.temp.sql"
TAR=tar
remoteshare=//windows share server ip/mail$
dest_dir=/mnt/ip0.16
log=/var/log/backup.log
recipient=postmaster@yourdomain.com
if [ $# -ne 2 ]; then
echo "|-------------------------------------------------------------------|"
echo "|usage:mysqlbackup.sh <--all-database | dbname> <-days>. |"
echo "| --all-database: all database to backup. |"
echo "| dbname: database name to backup. |"
echo "| -days: delete file for over date. |"
echo "|-------------------------------------------------------------------|"
echo " usage error." > $log
mail -s "MySQL backup faild for Mail Server.." $recipient
# $SENDM
exit 1
fi
if [ $1 != "--all-database" ]; then
if [ ! -d "$mysqldbfolder/$1" ]; then
echo "Database no exit!" >$log
mail -s "MYSQL Backup faild,database no exit!" $recipient
#$SENDM
exit 1
fi
fi
# If the function of the second parameter is equal to 0,then write “sucess” to the log file.
{
if [ $2 -eq 0 ]; then
echo $1 " ...... backup Sucess." >> $log
else
echo $1 " ..... backup Faild." >> $log
mail -s "$1 Backup faild on Mail Server." $recipient
# $SENDM
exit 1
fi;
}
# MySQL DUMP TO temp File /tmp/tempfilename
# tar tempfile to mysql.$f1.date.tar.gz
echo "Start Backuping...." > $log
echo "$datef "`date +%T` >> $log
echo "------------------------" >> $log
$command -u$myuser -p$mypasswd $mydatabase > $tempfilename
writelog "mysql $1 ...." $?
$TAR czf $tempfolder/$filename $tempfilename
writelog "Tar...." $?
if [ $count -eq 0 ]; then
mount -o username=$nsuser,password=$nspasswd $remoteshare $dest_dir
fi
writelog "Mount...." $?
# Move Mysql Backup File to Network that is $remoteshare or /mnt/ip0.16
#
writelog "move $filename to $dest_dir " $?
# 删除过期的文件。
if [ -f "$dest_dir/$overfilename" ]; then
rm -rf $dest_dir/$overfilename
writelog "$overfilename was deleted...." $?
fi;
writelog "umount $dest_dir" $?
#$SENDM