Mysql backup script and delete the backup file of the previous five days _ MySQL

Source: Internet
Author: User
Mysql backup script and delete the backup file bitsCN.com from the previous five days
USER = root # Database username
PASSWORD = cucrzmysql # database user PASSWORD
# DATABASE = idssr # DATABASE name
For DATABASE in 'ls/mysql/data/mysql/| grep idss' # DATABASE name
Do
# WEBMASTER = 849699940@qq.com # administrator email address to send backup failure message reminder
BACKUP_DIR =/mysql_bak # backup file storage path
LOGFILE =/mysql_bak/data_backup.log # log file path
DATE = 'Date' + % Y % m % d-% H % M'' # date format (as file name)
DUMPFILE = $ DATABASE-$ DATE. SQL # backup file name
ARCHIVE = $ DATABASE-$ DATE. SQL. tgz # compressed file name
DATE_5 = 'date-d "-5 day" + % Y % m % d'

# Determine whether the backup file storage directory exists; otherwise, the directory is created.
If [! -D $ BACKUP_DIR]; then
Mkdir-p "$ BACKUP_DIR"
Fi

# Before starting backup, write the backup information header to the log file
Echo ""> $ LOGFILE
Echo "----------------------"> $ LOGFILE
Echo "backup date:" $ (date + "% Y-% m-% d % H: % M: % S") >>$ LOGFILE
Echo "----------------------"> $ LOGFILE

# Switch to the backup directory
Cd $ BACKUP_DIR
# Delete the backup file five days ago
Rm-rf $ DATABASE-$ DATE_5 *
# Use the mysqldump command to back up the database and name the backup file with the formatted timestamp
Mysqldump -- opt $ DATABASE-uroot-pcucrzmysql>/$ BACKUP_DIR/$ DUMPFILE
# Determine whether the database backup is successful
If [[$? = 0]; then
# Create a compressed backup file package
Tar czvf $ ARCHIVE $ DUMPFILE >>$ LOGFILE 2> & 1
# Enter a message indicating a successful backup to the log file.
Echo "[$ ARCHIVE] Backup Successful! ">>> $ LOGFILE
# Delete the original backup file. you only need to keep the compressed package of the database backup file.
Rm-f $ DUMPFILE
Else
Echo "Database Backup Fail! ">>> $ LOGFILE

# Send an email notification to the website administrator after the backup fails. you need support from mailutils or similar email tools.
# Mail-s "Database: $ DATABASE Daily Backup Fail" $ WEBMASTER
Fi
# Output the reminder message for the backup process to end
Echo "Backup Process Done"
Done
BitsCN.com

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.