Backup script for MySQL databases

Since I've spent quite a few hours over the past week getting a hacked Wordpress site back up and running it seems like a good time to go and look over my backup routines.  I've got a script that backs up the database for this forum, and every other database on the server, nightly.  But, since the site that got hacked was on another server, using an account that was donated, I hadn't been backing up like I was supposed to.  On the up side, I was able to fix the database problems from the hack in about 2 minutes. 

To run the script you'll need PHP and MySQL on your computer, both of which can be installed on pretty much any operating system.  If you try to run it on a Windows machine you will have to change the paths from /home/whatever to c:\whatever or where ever you want the files stored. 

Code:
$tempdate = Date(“Ymd-Hi”);
$conn = mysql_connect(“host”, “username”, “password”) or die(“Error:n”.mysql_error($conn));
$dblist = mysql_list_dbs($conn);
$zipname = “/directory/”.Date(“Y”).“/”.Date(“m”).“-”.Date(“F”).“/full-”.$tempdate.“.zip”;
/*  Make sure subdirectories exist  */
if (!file_exists(“/directory/”.Date(“Y”)))
{
mkdir(“/directory/”.Date(“Y”), 0777);
}
if (!file_exists(“/directory/”.Date(“Y”).“/”.Date(“m”).“-”.Date(“F”)))
{
mkdir(“/directory/”.Date(“Y”).“/”.Date(“m”).“-”.Date(“F”));
}
while ($row = mysql_fetch_object($dblist))
{
echo $row->Database.“n”;
$temp = shell_exec(“mysqldump -uUserName -pPassword -hHost “.$row->Database.“>/directory/”.$row->Database.“.sql”);
$temp = shell_exec(“zip -D -m “.$zipname.” /directory/*.sql”);
}

What it does is connect to the MySQL server using the account name listed in information in the line that starts $conn and gets a list of each database.  Each of those databases is dumped to a separate text file and then those text files are moved to a zip file. 

I've got my Linux file server setup to run this script every night at about 2:15am through a cron job.  Windows has a task scheduler that should do something similar.  I'm sure Mac has something that'll do the same, it's just been about 15 years since I've used a Mac. 
Logged


script looks good!
couple of things to add.
1. The backups are text so basically small.
2. the ZIP files should be downloaded and archived to CD every month or so.

The trick is to find out when a problem happened and go to the day before to keep from loosing too much data. And to be able to do the recovery thing in as short a time as possible.

and for those of you that have NO IDEA what either of us just said, use your favorite FTP program and download your entire site or go to the "golden" copy of the files and burn them to CD every  month or two. Copies on the web and hard drive are good but a backup you could send to someone to work on if lightning hit your PC or something is always a safe bet.

**steps off soap box**

Logged

-----------------------------------
"I get up every morning determined to both change the world and have one heck of a good time. Sometimes this makes planning my day difficult. "- EB White

Chattanooga Photographer www.BobEdens.com

Quote
**steps off soap box**
It's a soap box I find myself on right now too. 

Quote
The backups are text so basically small.
Depends on how many databases you're backing up Cheesy Grin  This backup from this server is 14mb zipped.  But you're right, a database from a single Wordpress site or something similar should be pretty small.  It would be even smaller if you dumped a CSV instead of the full set of SQL commands to rebuild the databases but that's a much more complicated task. 

Quote
and for those of you that have NO IDEA what either of us just said, use your favorite FTP program and download your entire site or go to the "golden" copy of the files and burn them to CD every  month or two.
That's a good idea, with one catch.  Most hosting accounts don't have the databases in a folder where the user can get to them.  Take this forum as an example.  All of the posts are stored in the database away from any folder that I can access with the user account. 
Logged



Quote
and for those of you that have NO IDEA what either of us just said, use your favorite FTP program and download your entire site or go to the "golden" copy of the files and burn them to CD every  month or two.
That's a good idea, with one catch.  Most hosting accounts don't have the databases in a folder where the user can get to them.  Take this forum as an example.  All of the posts are stored in the database away from any folder that I can access with the user account. 

My point, if you have a site that uses a database, you probably know what we're talking about.
Logged

-----------------------------------
"I get up every morning determined to both change the world and have one heck of a good time. Sometimes this makes planning my day difficult. "- EB White

Chattanooga Photographer www.BobEdens.com