MySQLDump 2.0
Categories: MySQL, Source Code, php
Tags:
This is a guest post from Daniele
After a lot of work, I released the new version (2.0) of the MySQLDump class, already introduced by this article.
MySQLDump is a class that allows to do a complete MySQL database backup with php pages.
After the inandrea’s good work there wasn’t too much space for improvements, but I think that these addition can be usefull: now it’s possible to export structures and/or data not only for the selected database but also for a single table.
The following example will explain how to export the structure and the data for the table mytable in the db mydb (the interface to the class is slightly changed from the previous version).
//Include the library
@include_once('lib_dump.php');
//Db connection
$connection = @mysql_connect('127.0.0.1','username','password');
//Create the MySQLDump class instance
//1° parameter: db name
//2° parameter: the exported file that will contain the dump
//3° parameter: create zipped file (true = zipped, false = normal)
//4° parameter: data encode (true = hexadecimal, false = plain text)
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
//Structure export of the table 'mytable'
$dumper->getTableStructure('mytable');
//Data export of the table 'mytable'
$dumper->getTableData('mytable');
In this way we will obtain the complete dump of the tumble mytable in the file dumpfile.sql (with the term dump we refers to a sql instruction that can recreate the table and the data). Of course, if for some reasons we don’t need both structure and data, we can always create the dump only for the table structure or for the data.
Instead, if we want to do the complete backup of the database, we must use these instructions:
@include_once('lib_dump.php');
$connection = @mysql_connect('127.0.0.1','username','password');
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
$dumper->doDump();
Or, to obtain the structure and the data separately:
@include_once('lib_dump.php');
$connection = @mysql_connect('127.0.0.1','username','password');
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
$dumper->getDatabaseStructure();
$dumper->getDatabaseData();
I hope that this class can be usefull to you, and I invite you to visit the specific section of my site (http://www.creativefactory.it/lab/) where you can find the complete documentation generated with phpdoc.
Download MySQLDump. Downloads: 3441
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
19 Oct 2007 admin
Hello
I see one little difference between a dump created with MySQLDump class and a dump exported from phpmyadmin!
For example I created the table:
CREATE TABLE `jos_poll_data` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`pollid` int( 4 ) NOT NULL ,
`text` text NOT NULL ,
`hits` int( 11 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `pollid` ( `pollid` , `text` ( 1 ) )
);
Then exported this table with MySQLDump. The outpoot looks like this:
CREATE TABLE `jos_poll_data` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`pollid` int( 4 ) NOT NULL ,
`text` text NOT NULL ,
`hits` int( 11 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `pollid` ( `pollid` , `text`)
);
The problem is here:
KEY `pollid` ( `pollid` , `text`).
When try to use this dump from phpmyadmin it returns an error:
#1170 - BLOB/TEXT column ‘text’ used in key specification without a key length
Do you know how to update the class in order to solve this issue ?
I have this issue on:
> Server version: 5.0.45-community-nt
> phpMyAdmin - 2.11.1
Have a nice day !
Hi.
I think this will run correctly (for Joomla):
http://vnt.sytes.net/dobakpeter/phpscripts/lib_dump.php
Hi Syrius
I think you have to put the lib_dump.php file into a zip archive for others to be able to download it
Hi folks,
Usandothis dump script ignores null values. Here’s a fix:
@@ -205,10 +205,14 @@ class MySQLDump {
$data .= ‘ (’;
for ($j = 0; $j 0) )
- $data .= “0x”.$record[$field_name];
- else
- $data .= ‘\”.@str_replace(’\”‘,’”‘,@mysql_escape_string($record[$field_name])).’\”;
+ if (is_null($record[$field_name])) {
+ $data .= “NULL”;
+ } else {
+ if ( $hexField[$j] && (@strlen($record[$field_name]) > 0) )
+ $data .= “0x”.$record[$field_name];
+ else
+ $data .= ‘\”.@str_replace(’\”‘,’”‘,@mysql_escape_string($record[$field_name])).’\”;
+ }
$data .= ‘,’;
}
$data = @substr($data,0,-1).”);\n”;
One more thing:
A long row of dashes will cause phpMyAdmin to fail to import the dump. It requires a space after two dashes:
@@ -155,7 +155,7 @@ class MySQLDump {
// Save all Column Indexes
$structure .= $this->getSqlKeysTable($table);
- $structure .= “\n);\n\n———————————————————-\n\n”;
+ $structure .= “\n);\n\n– ——————————————————–\n\n”;
$this->saveToFile($this->file,$structure);
}
@@ -218,7 +222,7 @@ class MySQLDump {
Usando$data = ”;
}
}
- $data .= “\n———————————————————-\n\n”;
+ $data .= “\n– ——————————————————–\n\n”;
$this->saveToFile($this->file,$data);
}
very useful for me thanks!!!
UsandoHi there.
I changed the script a little. not all of the changes are pretty to look at.
Code changes:
* Petr ‘PePa’ Pavel - dash-bug
* Petr ‘PePa’ Pavel - null not saved
* Dump sql to stream
* Hacked getDatabaseStructureSimple (encoding of table and fields are returned!)
I found out that strucureSQL did not have encoding spesifications with it, so I made a simple hacked version of that. With out that information every scandinavian chars I had stored in the database got wonkey.
http://home.no.net/thor918/php-exp/lib_dump2.01.zip
UsandoThanks for the bug fixes
I will replace the file soon
UsandoHi there, very useful script indeed. Here’s another one that does similar job http://www.codeplex.com/phpMyAdminToolkit using a different philosophy (uses the phpMyAdmin API).
Usandohai Daniele,
Thank u so much.
Usando