mysql备份脚本

#!/bin/bash
#定义参数
dbname=mysql
dbuser=root
dbpasswd=redhat

#判断目录是否存在
if [ -d /root/backup/mysql ];then
 :;
 else
 mkdir -p /root/backup/mysql
fi
if [ -d /root/backup/log ];then
 :;
 else
 mkdir -p /root/backup/log
fi

#定义路径
backuppath=/root/backup/mysql
logfile=/root/backup/log/mysql-bak.log
dumpfile=mysql-$(date +%F)

back_db()
{
 echo "--------- $(date +%F%t%T) Beginning backup $dbname database -------" >>$logfile
/usr/bin/mysqldump -u${dbuser} -p${dbpasswd} --opt $dbname > ${dumpfile}.sql 2>> $logfile
if [ $? = 0 ];then
 echo "The $dbname backup Sucessfull!" >>$logfile
else
 echo "The $dbname backup Faild!" >>$logfile
fi
echo "$(date +%F%t%T) Beginning zip ${backuppath}/${dumpfile}.sql" >>$logfile
tar -zcf ${dumpfile}.tar.gz ${dumpfile}.sql && rm ${dumpfile}.sql 2>>$logfile
if [ $? = 0 ];then
 echo "The $dbname zip Sucessfull!">>$logfile
else
 echo "The $dbname zip Faild!">>$logfile
fi
echo -e "----------------------End--------------------\n" >>$logfile
cat $logfile | mutt -s "$dbname backup" root@localhost
}

rm_oldfile()
{
 find $backpath -type f -mtime +7 -exec rm {} \;
}

cd $backuppath

back_db

rm_oldfile

你可能感兴趣的:(mysql,备份,脚本,database)