Back up the MySQL database

Source: Internet
Author: User

To back up the MySQL database script:

#!/bin/bash# description:mysql buckup shell script# author:lmj# Web Site:http://www.cnblogs.com/dscode St=$(Date+%s) USER="Root"# User name password="Root"# Password Database="Myblogdb"# Database Mail="[email protected]"# Mail Backup_dir=/home/lmj/data_backup/# Backup file storage path LOGFILE=/home/lmj/data_backup/Data_backup.log # log file path DATE=`Date+%y%m%d-%h%M ' # Use date format as filename dumpfile=$DATE. SQL ARCHIVE= $DATE. sql.Tar. GZ OPTIONS="-u$user-p$password $DATABASE"# Determine if the backup file storage directory exists or create the directoryif[ ! -d $BACKUP _dir] Then    mkdir-P"$BACKUP _dir"fi# Before starting the backup, write the backup information header to the journal fileEcho "    ">>$LOGFILEEcho "--------------------">>$LOGFILEEcho "BACKUP DATE:"$(Date+"%y-%m-%d%h:%m:%s") >>$LOGFILEEcho "-------------------">>$LOGFILE # Switch to backup directory CD $BACKUP _dir mysqldump $OPTIONS>$DUMPFILE
# Determine if the database backup was successfulif[[ $? ==0 ]] Then TarCZVF $ARCHIVE $DUMPFILE >> $LOGFILE2>&1 Echo "[$ARCHIVE] Backup successful!">>$LOGFILERM-F $DUMPFILE # Delete the original backup file, just keep the backup package # back up the archive to another machine. SCP-P the$BACKUP _dir$archive [email protected]*.*.*.*:/home/user/data_backup/>> $LOGFILE2>&1 Else Echo "Database Backup fail!">>$LOGFILE # Send alert mail to Manager when backup fails-S"database: $DATABASE Daily Backup fail!"$MAILfi Echo "Backup Process Done"
# Delete backup files for more than 3 days # cleaningFind$BACKUP _dir-type F-mtime +2-name"*.tar.gz"-execRM-f {} \;

Back up the MySQL database

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.