Requirements | Installing | Getting started | API | Settings | PDO Settings | TODO | License | Credits
This is a php version of mysqldump cli that comes with MySQL, without dependencies, output compression and sane defaults.
Out of the box, MySQLDump-PHP supports backing up table structures, the data itself, views, triggers and events.
MySQLDump-PHP is the only library that supports:
- output binary blobs as hex.
- resolves view dependencies (using Stand-In tables).
- output compared against original mysqldump. Linked to travis-ci testing system (testing from php 5.3 to 7.1 & hhvm)
- dumps stored procedures.
- dumps events.
- does extended-insert and/or complete-insert.
- supports virtual columns from MySQL 5.7.
- does insert-ignore, like a REPLACE but ignoring errors if a duplicate key exists.
- modifying data from database on-the-fly when dumping, using hooks.
From version 2.0, connections to database are made using the standard DSN, documented in PDO connection string.
- PHP 5.3.0 or newer
- MySQL 4.1.0 or newer
- PDO
Using Composer:
$ composer require ifsnop/mysqldump-php:2.*
Or via json file:
"require": {
"ifsnop/mysqldump-php":"2.*"
}
Using Curl to always download and decompress the latest release:
$ curl --silent --location https://api.github.com/repos/ifsnop/mysqldump-php/releases | grep -i tarball_url | head -n 1 | cut -d '"' -f 4 | xargs curl --location --silent | tar xvz
With Autoloader/Composer:
<?php
use Ifsnop\Mysqldump as IMysqldump;
try {
$dump = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dump->start('storage/work/dump.sql');
} catch (\Exception $e) {
echo 'mysqldump-php error: ' . $e->getMessage();
}
Plain old PHP:
<?php
include_once(dirname(__FILE__) . '/mysqldump-php-2.0.0/src/Ifsnop/Mysqldump/Mysqldump.php');
$dump = new Ifsnop\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dump->start('storage/work/dump.sql');
Refer to the wiki for some examples and a comparision between mysqldump and mysqldump-php dumps.
You can register a callable that will be used to transform values during the export. An example use-case for this is removing sensitive data from database dumps:
$dumper = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
$dumper->setTransformColumnValueHook(function ($tableName, $colName, $colValue) {
if ($colName === 'social_security_number') {
return (string) rand(1000000, 9999999);
}
return $colValue;
});
$dumper->start('storage/work/dump.sql');
/**
* Constructor of Mysqldump. Note that in the case of an SQLite database
* connection, the filename must be in the $db parameter.
*
* @param string $dsn PDO DSN connection string
* @param string $user SQL account username
* @param string $pass SQL account password
* @param array $dumpSettings SQL database settings
* @param array $pdoSettings PDO configured attributes
*/
public function __construct(
$dsn = '',
$user = '',
$pass = '',
$dumpSettings = array(),
$pdoSettings = array()
)
$dumpSettingsDefault = array(
'include-tables' => array(),
'exclude-tables' => array(),
'compress' => Mysqldump::NONE,
'init_commands' => array(),
'no-data' => array(),
'reset-auto-increment' => false,
'add-drop-database' => false,
'add-drop-table' => false,
'add-drop-trigger' => true,
'add-locks' => true,
'complete-insert' => false,
'databases' => false,
'default-character-set' => Mysqldump::UTF8,
'disable-keys' => true,
'extended-insert' => true,
'events' => false,
'hex-blob' => true, /* faster than escaped content */
'insert-ignore' => false,
'net_buffer_length' => self::MAXLINESIZE,
'no-autocommit' => true,
'no-create-info' => false,
'lock-tables' => true,
'routines' => false,
'single-transaction' => true,
'skip-triggers' => false,
'skip-tz-utc' => false,
'skip-comments' => false,
'skip-dump-date' => false,
'skip-definer' => false,
'where' => '',
/* deprecated */
'disable-foreign-keys-check' => true
);
$pdoSettingsDefaults = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false
);
// missing settings in constructor will be replaced by default options
$this->_pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);
$this->_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);
- include-tables
- Only include these tables (array of table names), include all if empty
- exclude-tables
- Exclude these tables (array of table names), include all if empty, supports regexps
- compress
- Gzip, Bzip2, None.
- Could be specified using the declared consts: IMysqldump\Mysqldump::GZIP, IMysqldump\Mysqldump::BZIP2 or IMysqldump\Mysqldump::NONE
- reset-auto-increment
- Removes the AUTO_INCREMENT option from the database definition
- Useful when used with no-data, so when db is recreated, it will start from 1 instead of using an old value
- add-drop-database
- add-drop-table
- add-drop-triggers
- add-locks
- complete-insert
- databases
- default-character-set
- utf8 (default, compatible option), utf8mb4 (for full utf8 compliance)
- Could be specified using the declared consts: IMysqldump\Mysqldump::UTF8 or IMysqldump\Mysqldump::UTF8MB4BZIP2
- http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
- https://mathiasbynens.be/notes/mysql-utf8mb4
- disable-keys
- events
- extended-insert
- hex-blob
- insert-ignore
- lock-tables
- net_buffer_length
- no-autocommit
- Option to disable autocommit (faster inserts, no problems with index keys)
- http://dev.mysql.com/doc/refman/4.1/en/commit.html
- no-create-info
- no-data
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-data
- Do not dump data for these tables (array of table names), support regexps,
true
to ignore all tables
- routines
- single-transaction
- skip-comments
- skip-dump-date
- skip-triggers
- skip-tz-utc
- skip-definer
- where
The following options are now enabled by default, and there is no way to disable them since they should always be used.
- disable-foreign-keys-check
- PDO::ATTR_PERSISTENT
- PDO::ATTR_ERRMODE
- PDO::MYSQL_ATTR_INIT_COMMAND
- PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
To dump a database, you need the following privileges :
- SELECT
- In order to dump table structures and data.
- SHOW VIEW
- If any databases has views, else you will get an error.
- TRIGGER
- If any table has one or more triggers.
- LOCK TABLES
- If "lock tables" option was enabled.
Use SHOW GRANTS FOR user@host; to know what privileges user has. See the following link for more information:
Which are the minimum privileges required to get a backup of a MySQL database schema?
Current code for testing is an ugly hack. Probably there are much better ways of doing them using PHPUnit, so PR's are welcomed. The testing script creates and populates a database using all possible datatypes. Then it exports it using both mysqldump-php and mysqldump, and compares the output. Only if it is identical tests are OK.
Some tests are skipped if mysql server doesn't support them.
A couple of tests are only compared between original sql code and mysqldump-php, because mysqldump doesn't work,
After this bug report, a new one has been introduced. _binary is appended also when hex-blob option is used, if the value is empty.
mysqldump-php is not backwards compatible with php 5.2 because we it uses namespaces. It can be trivially fixed.
Write more tests, test with mariadb also.
Format all code to PHP-FIG standards. http://www.php-fig.org/
This project is open-sourced software licensed under the GPL license
After more than 8 years, there is barely anything left from the original source code, but:
Originally based on James Elliott's script from 2009. http://code.google.com/p/db-mysqldump/
Adapted and extended by Michael J. Calkins. https://github.com/clouddueling
Currently maintained, developed and improved by Diego Torres. https://github.com/ifsnop