Overview of automatic MySQL database backup and Email sending

Source: Internet
Author: User

The following articles mainly describe how to automatically back up MySQL Databases and send them to Email, and describe the actual application code for automatically backing up MySQL Databases and sending them to Email, the following is the description of the main content of the article. I hope you will gain some benefits.

A blog, the most important thing for a website is a database, so it is necessary to back up data frequently. although WordPress has a plug-in for regular data backup, It is not flexible enough to back up the current blog. it is suitable for personal and small blogs and is not suitable for some websites. today, many people have multiple websites, and showfom has several websites. installing a plug-in for each website is troublesome. not every website is WordPress.

So I wrote a script to automatically back up the MySQL database, and added the G-level mailbox gmail to back up the data as much as possible. The following code is as follows:

 
 
  1. MySQLdump-uuser-ppassword -- databases db1 db2 db3>/home/website/backups/databackup. SQL
  2. Tar zcf/home/website/backups/databackup. SQL .tar.gz/home/website/backups/
  3. Echo "Topic: Database Backup" | mutt-a/home/website/backups/databackup. SQL .tar.gz-s "content: Database Backup" www@gmail.com
  4. Rm-r/home/website/backups /*

Save the above Code as autoMySQLbackup. sh

Then, use crontab to implement dynamic backup. Under ssh,

Crontab-e

Enter the following content:

 
 
  1. 00 00 * * * /home/website/autoMySQLbackup.sh 

In this way, the MySQL database is automatically backed up at every day and sent to the Email

Let's briefly describe it.

The first sentence is to back up multiple databases at a time. This requires you to use the root permission .. -u is followed by the database user name-p followed by the database password without spaces db1 db2 db3 for the database name you need to back up.
If your MySQL database user name does not have the root permission, you can change it to this way.

 
 
  1. MySQLdump -uuser -ppassword db1 > /home/website/backups/db1.sql  
  2. MySQLdump -uuser -ppassword db2 > /home/website/backups/db1.sql  
  3. MySQLdump -uuser -ppassword db3 > /home/website/backups/db1.sql 

The second sentence is to compress the data file in the backups folder into the file name: databackup. SQL .tar.gz

The third sentence is to send the compressed database file to the specified mailbox .....

The topic is database backup, which is the subject of the email. The content is MySQL database backup, which is used in the email,

/Home/website/backups/databackup. SQL .tar.gz is the attachment

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.