I’ve been bad. Until recently I haven’t been backing up my MySQL database. Bad things could have happened.
Today I finally hacked up a simple script to run from cron that will automatically dump, zip, and mail backups of my MySQL tables to an email address I specify. I set up a Gmail account to send the dumps to because they’re about 8 meg each. The dumps are also encrypted using my PGP 1024-bit public key to give me a bit of security.
Since I didn’t manage to find anything similar in my Googling, I decided to post it.
#!/bin/sh
# Performs a backup of MySQL, encrypts it using PGP, and mails it to a particular user
# The user to mail the backup to. Should have a public key in the above user's keyring...
MAIL_TO="your-backup-account@gmail.com"
MAIL_TO_KEY="Your PGP key ID"
# Subject of the mail. A date stamp will be appended to it
MAIL_SUBJECT="[mysqldump] MySQL Backup for ”
######################################
DATESTAMP=`date %Y-%m-%d`
FILENAME=”/tmp/mysqldump-$DATESTAMP.gz”
# Generate the dump
mysqldump –all-databases | gzip > “$FILENAME”
# Encrypt it
gpg –encrypt –recipient “$MAIL_TO_KEY” “$FILENAME”
# Mail it!
mpack -s “$MAIL_SUBJECT $DATESTAMP” -c application/octet-stream $FILENAME.gpg” “$MAIL_TO”
# Remove the backups
rm -f $FILENAME $FILENAME.gpg
Tags: code, gmail, hacks, linux, tools, web-design, wordpress

3 comments
Comments feed for this article
Trackback link
http://chrismetcalf.net/blog/archives/2005/04/16/automated-pgp-encrypted-mysql-backups/trackback/
May 4, 2005 at 7:19 am
Kyle
You’ve got a backup of your keypair somewhere, then, right?
May 8, 2005 at 11:18 pm
chris
Of course.
December 2, 2005 at 5:54 am
Anthony
I am trying to set this up in coldfusion (not looking for scripting help), and I just wanted to know what I might have to do/configure/install in order to be able to use that command line.