mysql backup script and delete backup files from the previous 5 days
USER=root # Database usernamePASSWORD=cucrzmysql # Database user password#DATABASE=idssr # Database namefor DATABASE in `ls /mysql/data/mysql/ | grep idss` # Database namedo#WEBMASTER=849699940@qq.com # Administrator email address to send backup failure message reminderBACKUP_DIR=/mysql_bak # Backup file storage pathLOGFILE=/mysql_bak/data_backup.log # Journal file pathDATE=`date '+%Y%m%d-%H%M'` # Date format (as file name)DUMPFILE=$DATABASE-$DATE.sql # Backup file nameARCHIVE=$DATABASE-$DATE.sql.tgz # Compressed file nameDATE_5=`date -d "-5 day" +%Y%m%d`# Determines if the backup file storage directory exists, otherwise it is createdif [ ! -d $BACKUP_DIR ];thenmkdir -p "$BACKUP_DIR"fi# Before starting the backup, write the backup header to a journal fileecho " ">> $LOGFILEecho "----------------------">> $LOGFILEecho "BACKUP DATE:" $(date +"%Y-%m-%d %H:%M:%S") >> $LOGFILEecho "----------------------">> $LOGFILE# Switch to the backup directorycd $BACKUP_DIR# delete 5 Days before the backup filerm -rf $DATABASE-$DATE_5*# use mysqldump Command to backup the setup database and name the backup file after the formatted timestampmysqldump --opt $DATABASE -uroot -pcucrzmysql > /$BACKUP_DIR/$DUMPFILE# Determine if the database backup was successfulif [[ $? == 0 ]]; then# Create a zip file for the backuptar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1# Enter the successful backup message into the diary fileecho "[$ARCHIVE] Backup Successful!" >> $LOGFILE# Delete the original backup file, just protect it Leave the database backup files can be compressed packagerm -f $DUMPFILEelseecho "Database Backup Fail!" >> $LOGFILE# Send an email reminder to the site administrator after the backup failed, as needed mailutils Or similar support for sending mail tools under the terminal#mail -s " Database:$DATABASE Daily Backup Fail " $WEBMASTERfi# Output a reminder that the backup process is overecho "Backup Process Done"done