MySQL DB Backup: Generate backup files of large MySQL databases

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 56%Total: 1,658 All time: 2,353 This week: 269Up
Version License PHP version Categories
mysql_db_backup 1.1.0GNU General Publi...5.0.0Databases, Systems administration
Description Author

The class can be used to create backup files of MySQL databases in the form of lists of SQL statements that can be executed to restore the database.

This is a new version of a class originally written by Vagharshak Tozalakyan to use much less memory.

It can store the backup data in compressed or uncompressed files directly, instead of concatenating the whole data in a text string, thus avoiding to use too much memory when taking backups of large databases.

It can also return the backup data as a single uncompressed string, but this way it may exceed the PHP memory limit.

Picture of Luis Arturo Aguilar Mendoza
Name: Luis Arturo Aguilar ... <contact>
Classes: 1 package by
Country: Mexico Mexico
Age: 41
All time rank: 171218 in Mexico Mexico
Week rank: 590 Up4 in Mexico Mexico Up

MySQL DB export class, version 1.1.1

- The class now is PHP5 compatible

version 1.0.1

- The class now can make backups from big databases (+16M in SQL code) when 
it creates a backup file or saves the backup file in users computer.
- Added the task MSX_APPEND


1. Create the instance of MySQL_Export class.
2. Define necessary properties.
3. Call Execute() method to create backup.

require_once 'mysql_export.class.php';
$backup_obj = new MySQL_DB_Backup();
$backup_obj->server = 'localhost';
$backup_obj->username = 'username';
$backup_obj->password = 'password';
$backup_obj->database = 'dbname';
$backup_obj->tables = array();
$backup_obj->create_tables = true;
$backup_obj->drop_tables = true;
$backup_obj->struct_only = false;
$backup_obj->locks = true;
$backup_obj->comments = true;
$backup_obj->fname_format = 'd_m_y__H_i_s';
$backup_obj->null_values = array( '0000-00-00', '00:00:00', '0000-00-00 00:00:00');
if (!$backup_obj->Execute(MSX_DOWNLOAD, '', true))


var $server = 'localhost';
The name of MySQL server.

var $port = 3306;
The port of MySQl server.

var $username = 'root';
Database username.

var $password = '';
Database password.

var $database = '';
Name of the database.

var $link_id = -1;
MySQL link identifier of the current connection. You can set this if you
want to connect the MySQL server by your own.

var $connected = false;
Set true if the connection is already established before calling Execute().

var $tables = array();
Tables you want to backup. All tables in the database will be backed up if
this array is empty.

var $create_tables = true;
Backup the structure of tables if true.

var $drop_tables = true;
Add DROP TABLE IF EXISTS queries before CREATE TABLE in backup file.

var $struct_only = false;
Only structure of the tables will be backed up if true.

var $locks = true;
Add LOCK TABLES and UNLOCK TABLES queries before and after each INSERT queries

var $comments = true;
Include comments in backup file if true.

var $backup_dir = '';
Directory on the server where the backup file will be placed. Used if task
parameter equals to MSX_SAVE or MSX_DOWNLOAD in Execute() method.

var $fname_format = 'd_m_y__H_i_s';
Default file name format.

var $error = '';
Error message.

var $null_values = array( '0000-00-00', '00:00:00', '0000-00-00 00:00:00');
Values which must be interpreted as NULL


function Execute($task = MSX_STRING, $fname = '', $compress = false)
$task - operation to perform: MSX_STRING - return SQL commands as a string;
  MSX_SAVE - create the backup file on the server; MSX_DOWNLOAD - save backup
  file in the user's computer; MSX_APPEND - append backup in the file on the
$fname - optional name of backup file.
$compress - use GZip compression?

  Files folder image Files  
File Role Description
Plain text file mysql_db_backup.class.php Class Mysql DB Backup class source
Accessible without login Plain text file execute_db_backup.php Example Example
Accessible without login Plain text file readme.txt Doc. Readme

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:2,353
This week:269Up
 User Ratings  
 All time
For more information send a message to info at phpclasses dot org.