PHP backup of a mysql database
Categories: MySQL, Source Code, Web, php
Tags:
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.
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:
- stores all dump in one variable, so if the db is very large Apache will be angry;
- makes a bad dump of primary keys and indexes;
- doesn’t write binary fields in hexadecimal format;
- 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: 1937
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
23 Jun 2007 inandrea

This may be a limitation of your web host, but you can connect to MySQL over TCP and use mysqldump to dump the DB to a local file…
$ mysqldump –host=host_name –user=user_name -p[password] db_name > DUMPFILE
Once you verify that works put it into a script (make sure the permissions don’t allow others to read the file since your password will have to be in it) and run the script from cron. Since this uses mysqldump on your local machine you don’t have to worry about whether the server has it installed.
–
Kevin
http://technogeek.org/
Unfortunately my database server is located behind a firewall, so only webfarm servers can access to it. Also, the servers doesn’t have mysql’s utilities, so the only way I have to get the dump is this php script. In other situations mysqldump is the best way to do a complete backup.
Help me.
I can’t create file backup.
Please send me full code.
Thank so much !
Error occurs with 113th line in MySQLDump.
“Undefined variable: index”
*** 107 ****
! if ( (!is_array($unique)) OR ($unique[$row->Key_name]==”") )
— 107 —-
! if ( (!isset($unique)) OR (!is_array($unique)) OR (!isset($unique[$row->Key_name])) OR ($unique[$row->Key_name]==”") )
***************
*** 113 ****
! if ( (!is_array($index)) OR ($index[$row->Key_name]==”") )
— 113 —-
! if ( (!isset($index)) OR (!is_array($index)) OR (!isset($index[$row->Key_name])) OR ($index[$row->Key_name]==”") )
***************
*** 119 ****
! if ( (!is_array($fulltext)) OR ($fulltext[$row->Key_name]==”") )
— 119 —-
! if ( (!isset($fulltext)) OR (!is_array($fulltext)) OR (!isset($fulltext[$row->Key_name])) OR ($fulltext[$row->Key_name]==”") )
***************
*** 132 ****
! if (is_array($unique)) {
— 132 —-
! if ((isset($unique)) && (is_array($unique))) {
***************
*** 140 ****
! if (is_array($index)) {
— 140 —-
! if ((isset($index)) && (is_array($index))) {
***************
*** 147 ****
! if (is_array($fulltext)) {
— 147 —-
! if ((isset($fulltext)) && (is_array($fulltext))) {
Dear Sir,
I am used in your source of backup of database. This source is running. but drop table and create table format is written the file. But insert command not written the file. What are the problem in your source. if any changes are in class file. please inform to my mail. Immediately reply me.
Thanking you,
With Regards,
GANESH.D
Sorry but I can’t understand what is the problem. Can you try to explain it in a pratical way, please? If you can, write an example of the problem.
Thanks! This script worked great for me!
The idea in itself is good, BUT, the code could use some improvements. I mean, the following bit in the getSqlKeysTable, for example:
if (($row->Key_name != ‘PRIMARY’) AND ($row->Non_unique == ‘0′) AND ($row->Index_type == ‘BTREE’)) {
//snip
}
if (($row->Key_name != ‘PRIMARY’) AND ($row->Non_unique == ‘1′) AND ($row->Index_type == ‘BTREE’)) {
//snip
}
if (($row->Key_name != ‘PRIMARY’) AND ($row->Non_unique == ‘1′) AND ($row->Index_type == ‘FULLTEXT’)) {
//snip
}
would be much more readable and a tad faster as
if ($row->Key_name != ‘PRIMARY’) {
if ($row->Non_unique == ‘1′) {
if ($row->index_type == ‘BTREE’) {
// bla
}
else if ($row->index_type = ‘FULLTEXT’) {
// bla
}
} else if ($row->Non_unique == ‘0′ AND $row->Index_type == ‘BTREE) {
// blaa
}
}
main difference is no more repeating if statements (except for the $row->Index_type one, but that can’t be helped), saving 3 if statements, adding readability, etc.
There’s probably more in there too. It’s nothing that’ll make the script fail or horribly slower or whatnot, just some beauty and efficiency stuff. This is a (relatively) small script, so you’d only have to change 3 instances of a repeating bit of code, but you don’t want to do that if something like this happens with a bit of code that is repeated three dozen times.
(If it is repeated three dozen times though, or even 3 or more times, then there’s already a design problem btw)
Thanks for the suggestions!
Take a look at PHP Doc’s take on string size:
Note: It is no problem for a string to become very large. There is no practical bound to the size of strings imposed by PHP, so there is no reason at all to worry about long strings
Cheers!