MySQLDump class is now at version 2.0 and this article contains obsolete informations about the version 1.0. For information about the new version check this article

I searched a way to do a daily backup of my wordpress blog in my home pc but I didn’t find nothing that completely satisfy me, so I decided to code it.
I separated the problem in two scripts:
The first, that is written in php and runs on the web server, makes the dump of mysql databases from my hosting provider and leaves the dump on the web server.
The other one (written in bash, and running locally) downloads the site and the mysql dump from the web server, via FTP. Unfortunately I discovered that my hosting provider (Aruba.it) didn’t supply with mysqldump its server.

Lamp

First of all, I searched for a php script that makes the direct dump of the database.

I found a class that does the dump of mysql database directly, but it haves some bugs:

  1. stores all dump in one variable, so if the db is very large Apache will be angry;
  2. makes a bad dump of primary keys and indexes;
  3. doesn’t write binary fields in hexadecimal format;
  4. hasn’t no way to dump the db structure or data.

In my free time I corrected these bugs, and now the class is ready to make the full backup of a mysql database, and can compress the dump in gzip format!
This example shows how to use the class.

<?php

//if t=1 dumps the data, otherwise the structure
$data=$_GET['t'];
require("class_mysqldump.php");

//Instantiate the class: host name, user name, and password
$dump = new MySQLDump("localhost", "root", "");

//If you want to compress the output uncomment the follow line
//$dump = new MySQLDump("localhost", "root", "", False);

if ($data=="1") {
	$dump->dumpDatabaseData("dbname", $filename, 100);
	//If you don't want binary fields saved in hexadecimal
	//format uncomment the follow line
	//$dump->dumpDatabaseData("nomedb", $filename, 100, False);
}
else {
	//dump the structure
	$dump->dumpDatabaseStructure("nomedb", $filename);
}

//send file to standard output
header ('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$file=fopen($filename,"r");
fpassthru($file);
fclose($file);

//delete temporary files
unlink($filename);

?>

Download MySQLDump. Downloads: 4321


Se sei interessato a questo post, potresti anche provare a leggere:

  • No related posts