Tuesday, December 16, 2008

Redundant MySQL backup with Python

The Story:
Ok so we lost our entire company wiki the day of launch... luckily I was able to recover over 60% of it from the cache of MANY people's computers (I just wrote a script that filtered their cache and spit out the wiki content pages, then a bunch of us went through the pages)... but that is a whole different subject. (Little Note: I was not responsible for backing up or transferring the database)

The Solution:
Since then I have been pretty paranoid about running backups and have volunteered for the task (because I have done a LOT of work on the wiki). I understand MySQL Replication, which is very powerful, but sometimes you just want the sql... so I created a redundant mysqldump script that will create zipped backups in the following structure:

<root>
<year>
January.zip
February.zip
...
Week1.zip
Week2.zip
Monday.zip
Tuesday.zip
...


The Script:
The script is pretty short (under 50 lines long). I put the parameters right into the script, but you could easily add them as options. I also have it backing up all databases. This could be run as a cron job/Scheduled Task and should run on Linux, Mac and Windows.
sql_backup.pyFurther Explanation:
The script is five parts:
  1. Parameters: This is really all you need to alter for the script to work for you. You will need to specify:
    • Target- The directory for all redundant backups
    • File Name- The name for all .sql files
    • Username/password- The authentication data used for signing into mysql
    • Dump command- ok this is just the command used to call mysqldump. You may need to include the full path to the executable if the mysql bin directory is not on your environment path variable
    • Host- this is your host- example.com
  2. The command for the dump
  3. Name Resolver: This just resolves the name of the backup (Monday, Tuesday, Week 1, Week 2, etc.)
  4. Writing the Zip file
  5. Cleanup- remove uncompressed sql file and move the zip file to the specified target

2 comments:

Greg Doermann said...

I didn't really do any checking to make sure that there weren't any connection problems. Also, as a reminder, if you are connecting to a remote database make sure the user's host is set to 'Any Host' or '%'

Greg Doermann said...

Oh yea... it does a redundant backup but keeps all months from all years. If you want to cut down on backups, you could have it keep only one copy of the database each year, but I like the extra backups...

Post a Comment