All Projects → ifsnop → Mysqldump Php

ifsnop / Mysqldump Php

Licence: gpl-3.0
PHP version of mysqldump cli that comes with MySQL

Projects that are alternatives of or similar to Mysqldump Php

Mysql
Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
Stars: ✭ 11,735 (+1103.59%)
Mutual labels:  sql, database, mysql, mariadb
Pdo
Connecting to MySQL in PHP using PDO.
Stars: ✭ 187 (-80.82%)
Mutual labels:  sql, pdo, database, mysql
Alpine Mariadb
MariaDB running on Alpine Linux [Docker]
Stars: ✭ 117 (-88%)
Mutual labels:  sql, database, mysql, mariadb
Ebean
Ebean ORM
Stars: ✭ 1,172 (+20.21%)
Mutual labels:  sql, database, mysql, mariadb
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+690.97%)
Mutual labels:  sql, database, mysql, mariadb
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+137.44%)
Mutual labels:  sql, database, mysql, mariadb
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+126.77%)
Mutual labels:  sql, database, mysql, mariadb
Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (-61.74%)
Mutual labels:  sql, database, mysql, mariadb
Bitnami Docker Mariadb
Bitnami MariaDB Docker Image
Stars: ✭ 251 (-74.26%)
Mutual labels:  sql, database, mysql, mariadb
Liquibase
Main Liquibase Source
Stars: ✭ 2,910 (+198.46%)
Mutual labels:  sql, database, mysql, mariadb
Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (-67.9%)
Mutual labels:  sql, pdo, database, mysql
Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (-51.08%)
Mutual labels:  sql, database, mysql, mariadb
Dbshield
Database firewall written in Go
Stars: ✭ 620 (-36.41%)
Mutual labels:  database, mysql, mariadb
Manticoresearch
Database for search
Stars: ✭ 610 (-37.44%)
Mutual labels:  sql, database, mysql
Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (-36%)
Mutual labels:  pdo, database, mysql
Sqlancer
Detecting Logic Bugs in DBMS
Stars: ✭ 672 (-31.08%)
Mutual labels:  sql, mysql, mariadb
Beekeeper Studio
Modern and easy to use SQL client for MySQL, Postgres, SQLite, SQL Server, and more. Linux, MacOS, and Windows.
Stars: ✭ 8,053 (+725.95%)
Mutual labels:  sql, database, mysql
Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (-33.85%)
Mutual labels:  pdo, database, mysql
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+2507.38%)
Mutual labels:  sql, mysql, mariadb
Baikaldb
BaikalDB, A Distributed HTAP Database.
Stars: ✭ 707 (-27.49%)
Mutual labels:  sql, database, mysql

MySQLDump - PHP

Requirements | Installing | Getting started | API | Settings | PDO Settings | TODO | License | Credits

Build Status Total Downloads Monthly Downloads Daily Downloads Scrutinizer Quality Score Latest Stable Version

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.3 & hhvm)
  • dumps stored routines (functions and 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.
  • can save directly to google cloud storage over a compressed stream wrapper (GZIPSTREAM).

Important

From version 2.0, connections to database are made using the standard DSN, documented in PDO connection string.

Requirements

  • PHP 5.3.0 or newer
  • MySQL 4.1.0 or newer
  • PDO

Installing

Using Composer:

$ composer require ifsnop/mysqldump-php

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

Getting started

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.

Changing values when exporting

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->setTransformTableRowHook(function ($tableName, array $row) {
    if ($tableName === 'customers') {
        $row['social_security_number'] = (string) rand(1000000, 9999999);
    }

    return $row;
});

$dumper->start('storage/work/dump.sql');

Getting information about the dump

You can register a callable that will be used to report on the progress of the dump

$dumper->setInfoHook(function($object, $info) {
    if ($object === 'table') {
        echo $info['name'], $info['rowCount'];
    });

Table specific export conditions

You can register table specific 'where' clauses to limit data on a per table basis. These override the default where dump setting:

$dumper = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');

$dumper->setTableWheres(array(
    'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0',
    'logs' => 'date_logged > NOW() - INTERVAL 1 DAY',
    'posts' => 'isLive=1'
));

Table specific export limits

You can register table specific 'limits' to limit the returned rows on a per table basis:

$dumper = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');

$dumper->setTableLimits(array(
    'users' => 300,
    'logs' => 50,
    'posts' => 10
));

Constructor and default parameters

/**
 * 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(),
    'if-not-exists' => false,
    '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);

Dump Settings

The following options are now enabled by default, and there is no way to disable them since they should always be used.

PDO Settings

Errors

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 [email protected]; 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?

Tests

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. After this commit, some test are performed using phpunit.

Some tests are skipped if mysql server doesn't support them.

A couple of tests are only comparing between original sql code and mysqldump-php generated sql, because some options are not available in mysqldump.

Bugs (from mysqldump, not from mysqldump-php)

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.

Backporting

mysqldump-php is not backwards compatible with php 5.2 because we it uses namespaces. However, it could be trivially fixed if needed.

Todo

Write more tests, test with mariadb also.

Contributing

Format all code to PHP-FIG standards. https://www.php-fig.org/

License

This project is open-sourced software licensed under the GPL license

Credits

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. https://code.google.com/archive/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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].