PHP Classes

MySQL Backup: Backup MySQL databases to files with SQL commands

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (15)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 5,188 All time: 510 This week: 488Up
Version License Categories
mysql-backup 1.0.0GNU General Publi...Databases, Systems administration


This class can be used to backup MySQL database to files that contain SQL statements to recreate the database.

It can backup whole databases or individial tables. Optionaly it may backup only the structure of tables without data.

It can create files that may optionally be compressed with the gzip algorithm.

The generated backup files can be saved to a server side file, returned as a string of SQL statements or served directly for download.

Picture of Vagharshak Tozalakyan
Name: Vagharshak Tozalakyan <contact>
Classes: 22 packages by
Country: United States United States
Age: 44
All time rank: 61 in United States United States
Week rank: 76 Down8 in United States United States Down
Innovation award
Innovation award
Nominee: 7x


MySQL database backup class, version 1.0.0 HOW TO USE 1. Create the instance of MySQL_Backup class. 2. Define necessary properties. 3. Call Execute() method to create backup. require_once 'mysql_backup.class.php'; $backup_obj = new MySQL_Backup(); $backup_obj->server = 'localhost'; $backup_obj->username = 'username'; $backup_obj->password = 'password'; $backup_obj->database = 'dbname'; $backup_obj->tables = array(); $backup_obj->drop_tables = true; $backup_obj->struct_only = false; $backup_obj->comments = true; $backup_obj->fname_format = 'd_m_y__H_i_s'; if (!$backup_obj->Execute(MSB_DOWNLOAD, '', true)) { die($backup_obj->error); } PUBLIC PROPERTIES 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 $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 $comments = true; Include comments in backup file if true. var $backup_dir = ''; Directory on the server where the backup file will be placed. Used only if task parameter equals to MSB_SAVE in Execute() method. var $fname_format = 'd_m_y__H_i_s'; Default file name format. var $error = ''; Error message. PUBLIC METHODS function Execute($task = MSB_STRING, $fname = '', $compress = false) $task - operation to perform: MSB_STRING - return SQL commands as a string; MSB_SAVE - create the backup file on the server; MSB_DOWNLOAD - save backup file in the user's computer. $fname - optional name of backup file. $compress - use GZip compression?

  Files folder image Files  
File Role Description
Accessible without login Plain text file execute_db_backup.php Example Example of usage
Plain text file mysql_backup.class.php Class Source
Accessible without login Plain text file readme.txt Doc. Readme

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:510
This week:488Up
User Ratings User Comments (8)
 All time
Does not save triggers or stored procedures.
12 years ago (Fabian)
Thank you very much.
14 years ago (nikeman)
Just great class, and great support from its author.
15 years ago (Sin Kronix)
Excelent class.
16 years ago (Nando)
Very interesting and useful script.
16 years ago (Supieb Klibdaeng)
This is the exact class i was looking for.
16 years ago (poomalairaj)
Excellent work, very good class, is extremely easy to use and...
16 years ago (Alfredo Cueto)
Thanks for this great class, I want ask you why you use adsla...
16 years ago (Martin Alurralde)