Automatic MySQL Backups
After setting up several sites with a linux based shared hosting provider I felt the need to add a little more reliability and regularity to the backup process that was in place. The hosting provider simply ran a full disk backup once a week. Knowing that my data could potentially be lost after 6 days and 23 hours of work did not give me what you might call a warm fuzzy feeling. Because of that I set out to design a more frequent backup procedure that I could setup once and be done with. Here’s what I did…
First, I created a simple script called dbbackup to backup and compress my database:
#!/usr/bin/bashmysqldump -uusername -ppassword yourdbname > yourdbname.sqltar -zcvf sqldata.tgz *.sqlperl /home/emaxx/sqlbk/dbmail.cgi- Download this code: /code/dbbackup.txt
If you have multiple databases you can repeat line 2 as many times as necessary. Line 3 will simply gzip any .sql files in the directory. You can find full documentation on the mysqldump command on the MySQL.com wesbite.
Next I created another script called dbmail.cgi to email my gzipped mysqldump results to an address of my choice. In order for this to work, you must have MIME::Lite installed and available to you. Just ask your hosting provider to enable it if it isn’t already. Now on to the email script:
#!/usr/bin/perl -wuse MIME::Lite;$msg = MIME::Lite->new(From => 'some@address.com',To => 'you@address.com',Subject => 'your db backup',Type => 'text/plain',Data => "Attached you will find your database backups for today.");$msg->attach(Type=>'application/x-tar',Path =>"sqlbackup.tgz",Filename =>"sqlbackup.tgz");$msg->send;- Download this code: /code/dbmail.txt
All you’ll have to do is replace the From and To addresses in lines 6 and 7 and you’re set. If you’d like you could always send to multiple addresses or customize the text in your message.
Now that these two scripts are complete and in place, verify that they work by running the dbbackup script from the shell. If you don’t have shell access to your account, either get it, or skip this step and read on.
Finally, I setup a cron job to run the dbbackup script on a schedule. I schedule mine to run nightly at somwehere between the hours 3am and 5am EST when I expect there to be minimal (if any) traffic on my sites. Here’s the setup for one of my cron jobs that runs nightly at 3am:
0 3 * * * /full/path/to/dbbackup
That should about do it. You’ll be greeted each morning with an email containing your latest database that evokes feelings both warm and fuzzy.
Comments
13. June 2005
OK I’m trying to set this up, but at several points it assumes you have some technical knowledge I dont have. Simple stuff, I think, but only if you know it.
– where do you save these files? They appear to be cgi scripts (?) so do they have to be in your cgi bin?
– I’m not sure about the syntax of the 1st file:
mysqldump -uusername -ppassword yourdbname > yourdbname.sql
uusername confuses me: why not username? is it significant?
– same for password
– it says yourdbname twice: are you supposed to delete one of those?
– the ‘from’ part of the mail script: what do you enter there? I dont understand where its coming from.
– whats the code to run the script from SSH? Is it
run dbbackup?
– I dont know if this requires DOS, Linux, or something else.
– the cron job code completely confuses me, because apart from the server pathway I dont understand those other characters.
Finally, I cant log in to my server using SSH and a client called Putty, and have no idea why. I’ve only got one name and password to access all the admin functions, so is there anything I need to consider that is maybe obvious, but only after you do actually know it, if you see what I mean?
Appears to be very simple stuff, so it will be nice to resolve it and get this working.
Thanks!
J
15. June 2005
Here is what I use (very similar)
backallup:
#!/bin/bash
for i in `cat dbs`; do ./backitup $i; done@
tar -zcf sql-backups_`date +%Y_%m_%d`.tar.gz *.sql
rm *.sql
backitup:
#!/bin/sh
mysqldump $1 > $1.sql
dbs is just a list of all my databases…
just makes the whole multiple database thing a little smoother, but all in all, the same idea.
16. December 2005
One of our clients requested and we coded a similar script for her. I hope someone may find it useful http://kionic.com/tools/dbbackup.sh.txt
Add a comment
You may use textile in your comment. Gravatars are enabled. Your email will not be displayed and will remain private. I reserve the right to edit or delete comments.
