Automatic mysql database backup and email notification

Source: Internet
Author: User
A prerequisite system environment: centos6.1 Script: backs up the specified database on the specified server, transfers it to another backup host through scp, and then sends a backup success email to the specified mailbox: first, in the backup script

A prerequisite system environment: centos6.1 Script: backs up the specified database on the specified server, transfers it to another backup host through scp, and then sends a backup success email to the specified mailbox: first, in the backup script

Prerequisites

System Environment: centos6.1

Script: backs up the specified database on the specified server and sends it to another backup host through scp. Then, it sends an email indicating whether the backup is successful to the specified mailbox.

Idea: First, export the backup report as a db_backup.log file in the backup script, and then the mail script will send the log content to the specified user as the mail content, so as to implement mail Notification.

Note: To back up data to a remote host, you must first perform ssh key authentication. scp must be used


Backup script

Backup script usage: to back up a database, add a Backup command after the script, as shown below:

Backup_db 10.90.5.50 dbname mysql 123456 innodb
Function Name mysql IP address database name to be backed up Connection database Username Password Database Engine type

#! /Bin/bash # name: backup_db.sh # Purpose: backup mysql database # Author: xxx # Date: 2014-08-26 # useage: backup_db IP databasename user password dbtype # example: backup_db 10.90.5.50 dbname mysql 123456 innodb # backup time DATE = 'date-d "today" + % Y % m % d _ % H % M % s' # previous time DATE2 =' date-d "1 month ago" + % Y % m % d _ % H % M % s' # Backup Directory HOMEDIR =/data/dbdata_backupMYSQLDUMP = '/usr/local/mysql /bin/mysqldump # Delete the previous backup report rm-f $ HOMEDIR/db_backup.log ## ######################################## ####################### Function ################ ######################################## #################### backup functionbackup_db () {if [$ #-ne 5]; thenecho "Usage: backupdb IP dbname user password dbtype "echo" Please try again "exit 1 fiIP = $1 DBNAME = $2 USER = $3 PASSWORD = $4 DBTYPE = $5 echo" ------------------------ start backup $ IP $ DBNAME db -------------------- ----------------- ">>$ HOMEDIR/db_backup.logecho" start date: 'date + % Y % m % d _ % H % M % S' "> $ HOMEDIR/db_backup.logif [$ DBTYPE =" innodb "]; then $ MYSQLDUMP-u $ USER-p $ PASSWORD-h $ IP -- single-transaction -- flush-logs -- routines -- events -- master-data = 2 "$ DBNAME"> $ HOMEDIR /$ DBNAME/$ {DBNAME} _ $ DATE. sqlif [$? -Ne 0]; thenecho "$ IP $ DBNAME $ DBTYPE db backup is not success, please check it out! ">>$ HOMEDIR/db_backup.logelseecho" $ IP $ DBNAME $ DBTYPE db backup is success! ">>$ HOMEDIR/db_backup.logfielse $ MYSQLDUMP-u $ USER-p $ PASSWORD-h $ IP-l -- flush-logs -- routines -- events -- master-data = 2" $ DBNAME "> $ HOMEDIR/$ DBNAME/$ {DBNAME} _ $ DATE. SQL if [$? -Ne 0]; then echo "$ IP $ DBNAME $ DBTYPE db backup is not success, please check it out! ">>$ HOMEDIR/db_backup.log else echo" $ IP $ DBNAME $ DBTYPE db backup is success! ">>$ HOMEDIR/db_backup.log incluiecho" End date: 'date + % Y % m % d _ % H % M % S' "> $ HOMEDIR/db_backup.loggzip $ HOMEDIR/$ DBNAME/$ {DBNAME} _ $ date. sqlSIZE = 'du-sh $ HOMEDIR/$ DBNAME/${dbname1__. SQL .gz | awk '{print $1} 'echo "the file size is: $ SIZE ">>$ HOMEDIR/db_backup.logecho" -------------------------- end backup $ IP $ DBNAME db success ">>$ HOMEDIR/db_backup.log # delete the bac Kup files which were create two month agorm-f $ HOMEDIR/$ DBNAME/${dbname1__1_date2. SQL .gz} # scp function: scp the backup file to another server as different place backupscp_function () {if [$ #-ne 1]; then echo "Usage: scp_function dbname" echo "Please try again" exit 1 fiDBNAME = $ 1scp-P52814-I/home/lj /. ssh/id_rsa $ HOMEDIR/$ DBNAME/mongomongodbname1__mongodate. SQL .gz lj@10.90.5.50:/data/dbbackup/if [$? -Ne 0]; thenecho "$ DBNAME db backup data transfer is not success, please check it out! ">>$ HOMEDIR/db_backup.logelseecho" $ DBNAME db backup data transfer is success! ">> }################################ ######################################## ### Function ##################################### ############################ run the email script using the perl Command ,, to send the backup report, where sendmail. pl sends the following email script perl/home/scripts/sendmail. pl


Send email script

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.