Mysql Backup script and delete 5 days before the backup file _mysql

Source: Internet
Author: User
Tags mysql backup
Copy Code code as follows:

User=root #数据库用户名
Password=cucrzmysql #数据库用户密码
#DATABASE =IDSSR #数据库名称
For DATABASE in ' ls/mysql/data/mysql/| grep IDSs ' #数据库名称
Todo
#WEBMASTER =849699940@qq.com #管理员邮箱地址 to send a backup failure message reminder
Backup_dir=/mysql_bak #备份文件存储路径
Logfile=/mysql_bak/data_backup.log #日记文件路径
Date= ' Date ' +%y%m%d-%h%m ' #日期格式 (as filename)
dumpfile= $DATABASE-$DATE. SQL #备份文件名
Archive= $DATABASE-$DATE. Sql.tgz #压缩文件名
date_5= ' date-d '-5 day "+%y%m%d"

#判断备份文件存储目录是否存在, otherwise create the directory
if [!-D $BACKUP _dir];then
Mkdir-p "$BACKUP _dir"
Fi

#开始备份之前, write the backup header to the journal file
echo "" >> $LOGFILE
echo "----------------------" >> $LOGFILE
echo "BACKUP Date:" $ (DATE + "%y-%m-%d%h:%m:%s") >> $LOGFILE
echo "----------------------" >> $LOGFILE

#切换至备份目录
CD $BACKUP _dir
#删除五天前的备份文件
RM-RF $DATABASE-$DATE _5*
#使用mysqldump the command back up the database and name the backup file in a formatted timestamp
mysqldump--opt $DATABASE-uroot-pcucrzmysql >/$BACKUP _dir/$DUMPFILE
#判断数据库备份是否成功
if [[$ = = 0]]; Then
#创建备份文件的压缩包
Tar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1
#输入备份成功的消息到日记文件
echo [$ARCHIVE] Backup successful! >> $LOGFILE
#删除原始备份文件, just keep the compressed package of the database backup file
Rm-f $DUMPFILE
Else
echo "Database Backup fail!" >> $LOGFILE

#备份失败后向网站管理者发送邮件提醒, requires support for mailutils or similar terminals to send mail tools
#mail-S "Database: $DATABASE daily Backup Fail" $WEBMASTER
Fi
#输出备份过程结束的提醒消息
echo "Backup Process Done"
Done
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.