Scripts
Posted on Jan 07, 2011 at 22:15
Backups - MySQL Dump/Email Mash-Up
Download File
backup_mysql_dbs.tgz
Explanation
This backup process dumps specified MySQL databases to individual files and then creates a tarball of them all on a seperate drive. It also emails out a results report upon completion. To do this I called upon several technologies to mash-up a practical solution. It's a little involved but worked out rather well.
The required software involved is as follows:
Bash
PHP
PHP PEAR: Mail package
PHP PEAR: Mail_Mime package
Python
PyODConverter: OpenDocument Converter
OpenOffice 3.0
Preliminaries
Some things need to be in place before running the script. All of the above software needs to be installed. Install them where you wish and make the necessary path adjustments in the scripts. You should be able to find those rather easily.
Files
The download package includes 4 files total. I will explain the process flow. As you look at the scripts keep in mind that it was setup to run on a Windows box under Cygwin. The pathings should give that away but those can be changed to suit your needs.
backup_dbs.sh
This is the main executable that is executed directly. It basically just performs a dump of the specified MySQL databases, gzips them, creates a tarball of them all and copies them to another drive. Then it initiates the summary report it sends out via email. You could just use thise script for the backups and be done with it. The rest is required if you want to email a summary report.
Email/do_email.php
This is a PHP script that actually creates the email body and emails out the report using SMTP. Here we need the two PHP PEAR packages mentioned above to perform the emailing of the report.
Email/EmailHTML.php
This is another PHP script that holds a function that creates the actual email body from an OpenOffice odt template file. It takes the template file (mentioned next) and converts it to an HTML file that ends up being the email body. It does this conversion by using the Python PyODConverter tool. After doing this it does various variable subsitutions to fill in the blanks with the correct information about the backup.
Email/Master_backups_email_body.odt
This is the OpenOffice Writer file that contains the body of the summary report email that gets sent out. This is nice because you can easily format how you want the email body to look using an OpenOffice document.
Example run:
[steved@blackbox]$ ./backup_mysql_dbs.sh
Backing up to directory /cygdrive/c/Storage/BACKUPS/ ...
Generating db backup file 20110107000657_blackbox_mybiz-1.sql.gz ...
Generating db backup file 20110107000657_blackbox_yourbiz_205.sql.gz ...
Generating db backup file 20110107000657_blackbox_yourbiz_999.sql.gz ...
Generating db backup file 20110107000657_blackbox_hisbiz_33.sql.gz ...
Generating db backup file 20110107000657_blackbox_hisbiz_7.sql.gz ...
Generating tarball backup file 20110107000657_blackbox_backup_dbs.tar ...
Email successfully sent to the following recipients:
To: <someone@domain.com>
CC: <someone_else@domain.com>
Here the script is setup to put the backups files in /cygdrive/c/Storage/BACKUPS/. Using Cygwin this is the equivalent of saying C:\Storage\BACKUPS on the Windows system. It generates the five individual database backup files first. Then it tars them all together and puts the file in the aforementioned directory. It finally emails out the summary report to the people specified in the script.