Copy codeThe Code is as follows:
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