BitterSweetJaVa

March 5, 2009

MySql database Backup & E-Mail (1)

Filed under: Linux — Tags: , , , , , , , , — .|2ic|K @ 7:05 PM

I’m maintaining a system for a notary’s office and I needed to make a weekly backup of the MySql database. I’m sharing the first version of the backup script. Basically the script does:

* Backup an entire database into a file (text file: creation scripts, data), using mysqldump.
* Compress the previous file using bzip2.
* Send the compressed file to an email address, using mutt: http://www.mutt.org/.

Here’s the code, below you will find some comments / requirements to execute the bash script:

echo - DB Backup Script v1.0
echo -
DATE_FILE=`date +%Y%m%d_%H%M%S`
DATE_MAIL=`date +%d/%m/%Y`
SUBJECT="DB Backup = "$DATE_MAIL
DESTINATION1="{Main EMail address}"
DESTINATION2="{CC EMail address}"
FILENAME=/DBBackups/Backup.$DATE_FILE.bz2
DB_USER={DB User Name}
DB_PSWD={DB User Password}
DB_NAME={DB Name}
echo - Creating database backup file $FILENAME ...
mysqldump -u $DB_USER -p$DB_PSWD $DB_NAME | bzip2 -c > $FILENAME
echo - Sending email to $DESTINATION, $DESTINATION2 ...
mutt -s "$SUBJECT" -c $DESTINATION2 -a $FILENAME $DESTINATION1 < /dev/null
echo -
echo - Finished

Now, to run the script first you will need to install & configure mutt. You can do this (ubuntu) by executing:

sudo apt-get install mutt

Once you have mutt installed in your system, you’ll need to configure it. In order to do that, you’ll need to create the file .muttrc in your home directory. In my particular case, I’m using the smtp configuration from my gmail account to send the email. So here’s my .muttrc file as an example:

set from = "{email address}"
set realname = "{name to be displayed in the email}"
set imap_user = "{username}@gmail.com"
set imap_pass = "{password}"
set smtp_url = "smtp://{username}@smtp.gmail.com:587/"
set smtp_pass = "{password}"

NOTE: Don’t forget to protect the file (you’re putting your account password there!). You can do that using the following command:

chmod 600 .muttrc

This will allow you and only you to have access to that file.
Once you’ve done that and if there’s no error in the configuration, you should be able to run the script.

A few notes:

* Every parameter is setup at the beginning of the script.
* In this case I have 2 destination email addresses (main & cc). If you want only one, you can delete the “-c $DESTINATION2″ section from the script. For more info on mutt: http://www.mutt.org/
* DATE_FILE and DATE_EMAIL just store the date in a particular format. For more info on this: man date (on the linux CLI)
* If you want to change the body of the email (include some text, etc) just replace “/dev/null” for a file that contains the text of the email’s body you desire.

It took me a few hours to figure out all of this (and correct many errors) so I hope you enjoy it.
I’ll post tomorrow how to setup a cron job to execute this script on a regular basis.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.