PHPackages                             dimtrovich/db-dumper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. dimtrovich/db-dumper

ActiveLibrary[Database &amp; ORM](/categories/database)

dimtrovich/db-dumper
====================

Dump and restore the contents of a database

1.0.0(1y ago)016[1 PRs](https://github.com/dimtrovich/php-db-dumper/pulls)MITPHPPHP &gt;=7.4CI passing

Since Feb 4Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/dimtrovich/php-db-dumper)[ Packagist](https://packagist.org/packages/dimtrovich/db-dumper)[ Docs](https://github.com/dimtrovich/php-db-dumper)[ RSS](/packages/dimtrovich-db-dumper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (4)Used By (0)

EN | [FR](README-fr.md)

Backup and restore database content
===================================

[](#backup-and-restore-database-content)

[![Tests](https://github.com/dimtrovich/php-db-dumper/actions/workflows/run-tests.yml/badge.svg)](https://github.com/dimtrovich/php-db-dumper/actions/workflows/run-tests.yml)[![Code Coverage](https://camo.githubusercontent.com/40ad53f3cf7cc755970adfee6835615cf7c38c65c3e80c8758abe390cf2dbc9e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64696d74726f766963682f7068702d64622d64756d7065722f6261646765732f636f7665726167652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/dimtrovich/php-db-dumper/?branch=main)[![Coding Standards](https://github.com/dimtrovich/php-db-dumper/actions/workflows/test-coding-standards.yml/badge.svg)](https://github.com/dimtrovich/php-db-dumper/actions/workflows/test-coding-standards.yml)[![Build Status](https://camo.githubusercontent.com/7a93d09369978ee3aeecdc4b21eaffa141577e99637821b14f8e29040b6fa84a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64696d74726f766963682f7068702d64622d64756d7065722f6261646765732f6275696c642e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/dimtrovich/php-db-dumper/build-status/main)[![Code Intelligence Status](https://camo.githubusercontent.com/2fca3474e8ac80f9400ed22f893f7cf7e813d464e977cf2339f37ab6e0c466c6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64696d74726f766963682f7068702d64622d64756d7065722f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d61696e)](https://scrutinizer-ci.com/code-intelligence)[![Quality Score](https://camo.githubusercontent.com/e702ed15569b19a16a95f7f4c63c8d22b0dfee58de01ef60abf4edcb0636613f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f64696d74726f766963682f7068702d64622d64756d7065722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/dimtrovich/php-db-dumper)[![PHPStan](https://github.com/dimtrovich/php-db-dumper/actions/workflows/test-phpstan.yml/badge.svg)](https://github.com/dimtrovich/php-db-dumper/actions/workflows/test-phpstan.yml)[![PHPStan level](https://camo.githubusercontent.com/bbb4b88ceed026f420c1d76bc64e1ce076ed7382a9f5bac1cd59d85d807e7af4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230362d627269676874677265656e)](phpstan.neon.dist)[![Total Downloads](https://camo.githubusercontent.com/6c4a3c5fd1e06dfdb1329122e727daded4e515097be90380eadab53a7bade4bd/68747470733a2f2f706f7365722e707567782e6f72672f64696d74726f766963682f64622d64756d7065722f646f776e6c6f616473)](https://packagist.org/packages/dimtrovich/db-dumper)[![Latest Version](https://camo.githubusercontent.com/147b3d78a678e5bbce76b24054e98499b6376270ca43f44c2a891f3dad5da43e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64696d74726f766963682f64622d64756d7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dimtrovich/db-dumper)[![PHP](https://camo.githubusercontent.com/d08ccd35c79d52d5bc06e23fda23ae42616c2fc5169119b335588083eac6bcad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d2533453d372e342d626c7565)](https://camo.githubusercontent.com/d08ccd35c79d52d5bc06e23fda23ae42616c2fc5169119b335588083eac6bcad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d2533453d372e342d626c7565)[![Software License](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](LICENSE)

**Db Dumper** is a tool that offers you a simple and efficient way to **export** and **import** your database in PHP. It is somewhat of a PHP version of the command-line tool `mysqldump` that comes with MySQL, without dependencies, with output compression and reasonable default parameters.

**Db Dumper** supports backing up table structures, the data itself, views, triggers, and events.

Features
--------

[](#features)

Db Dumper supports:

- outputting binary blobs in hexadecimal form.
- resolving view dependencies (using substitute tables).
- backing up stored routines (functions and procedures).
- backing up events.
- extended and/or complete insertion.
- MySQL 5.7 virtual columns.
- `insert-ignore`, like a `REPLACE` but ignoring errors if a duplicate key exists.
- modifying database data on the fly during backup, using `hooks`.
- direct backup to Google Cloud storage via a compressed stream wrapper (GZIPSTREAM).

Db Dumper is designed to work with the main current database management systems. The list below outlines their support:

- MySQL (supported)
- SQLite (supported)
- PostgreSQL (in progress)
- Oracle (not supported)

Prerequisites
-------------

[](#prerequisites)

- PHP 7.4+
- \*MySQL 5+
- \*SQLite 3+
- [PDO](https://secure.php.net/pdo)

Installation
------------

[](#installation)

Using [Composer](https://getcomposer.org/):

```
$ composer require dimtrovich/db-dumper

```

After installing this package, you must first ensure that you have access to a `PDO` instance as the export and import systems need it.

```
use PDO;

$pdo = new PDO('mysql:host=localhost;port=3307;dbname=database', 'username', 'password');
```

Data Export (backup)
--------------------

[](#data-export-backup)

Data export is the main functionality of this package. **Db Dumper** offers you a simple API to backup your database with the same options offered by native MySQL or PostgreSQL commands (`mysqldump` / `pgrestore`)

```
use Dimtrovich\DbDumper\Exporter;
use Exception;

try {
    $exporter = new Exporter($pdo, 'database');

    $exporter->process('storage/work/dump.sql');
} catch (Exception $e) {
    echo 'db-dumper error: ' . $e->getMessage();
}
```

### Modifying values during export

[](#modifying-values-during-export)

You can register a callable that will be used to transform values during export. A typical use case is removing sensitive data from database backups:

```
$exporter = new Exporter($pdo, 'database');

$exporter->transformTableRow(function (string $tableName, array $row) {
    if ($tableName === 'customers') {
        $row['social_security_number'] = (string) rand(1000000, 9999999);
    }

    return $row;
});

$exporter->process('storage/work/dump.sql');
```

### Getting information about table export

[](#getting-information-about-table-export)

You can register a callable that will be used to report the progress of the backup:

```
$exporter->onTableExport(function($tableName, $rowCount) {
    echo "Exporting table $tableName with $rowCount rows\n";
});
```

### Table-specific export conditions

[](#table-specific-export-conditions)

You can define table-specific `WHERE` clauses to limit the data of tables that can be exported. These clauses override the default `where` parameter:

```
$exporter->setTableWheres([
    'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0',
    'logs' => 'date_logged > NOW() - INTERVAL 1 DAY',
    'posts' => 'active=1'
]);
```

### Table-specific export limits

[](#table-specific-export-limits)

You can also define table-specific limits to limit the number of records that will be saved for each table:

```
$exporter->setTableLimits([
    'users' => 300,
    'logs' => 50,
    'posts' => 10
]);
```

### Exporter configuration options

[](#exporter-configuration-options)

The constructor of the `Exporter` class accepts a third parameter which is an array designating the data export options.

OptionTypeDefaultDescription`include-tables``array``[]`Include only these tables (array of table names), include all if empty.`exclude-tables``array``[]`Exclude these tables (array of table names), include all if empty, supports regular expressions.`include-views``array``[]`Include only these views (array of view names), include all if empty. By default, all views named in the `include-tables` array are included.`compress``Gzip`, `Bzip2`, `None`, `GzipStream``None``init_commands``array``[]``no-data``array``[]`Do not save data for these tables (array of table names), supports regular expressions.`if-not-exists``bool``false`Create a new table only if a table of the same name does not already exist. No error message is generated if the table already exists.`reset-auto-increment``bool``false`Removes the `AUTO_INCREMENT` option from the database definition. Useful when used with `no-data`, so that when the database is recreated, it starts at 1 instead of using an old value.`add-drop-database``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-database)`add-drop-table``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-table)`add-drop-trigger``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-trigger)`add-locks``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-locks)`complete-insert``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_complete-insert)`databases``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_databases)`default-character-set``utf8`, `utf8mb4`, `binary``utf8``disable-keys``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_disable-keys)`extended-insert``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert)`events``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_events)`hex-blob``bool``true`(Faster than escaped content). [MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_hex-blob)`insert-ignore``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_insert-ignore)`lock-tables``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_lock-tables)`net_buffer_length``int``1000000`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_net_buffer_length)`no-autocommit``bool``true`Option to disable autocommit (faster inserts, no problems with index keys). [MySQL Documentation](https://dev.mysql.com/doc/refman/4.1/en/commit.html)`no-create-db``bool``false`Option to disable backing up database creation instructions. [MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-db)`no-create-info``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-info)`routines``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines)`single-transaction``bool``true`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction)`skip-triggers``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_triggers)`skip-tz-utc``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc)`skip-comments``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments)`skip-dump-date``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_dump-date)`skip-definer``bool``false`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/mysqlpump.html#option_mysqlpump_skip-definer)`where``string``''`[MySQL Documentation](https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where)

Data Import (Restore)
---------------------

[](#data-import-restore)

Just like with data export, **Db Dumper** provides you with a simple API to restore your database from a backup file (`.sql`, `.gz`, `.gzip`, `.bz2`, `.bzip2`).

```
use Dimtrovich\DbDumper\Importer;
use Exception;

try {
    $importer = new Importer($pdo, 'database');

    $importer->process('storage/work/dump.sql');
} catch (Exception $e) {
    echo 'db-dumper error: ' . $e->getMessage();
}
```

The file extension of the restore file determines the type of compression to use:

- `.sql` No compression, it's a plain SQL file.
- `.gz`, `.gzip` GZIP compression, the importer will decompress the file before proceeding with the database restoration. **Make sure your PHP installation has the `Zlib` extension enabled before using such a dump.**
- `.bz2`, `.bzip2` BZIP2 compression, the importer will decompress the file before proceeding with the database restoration. **Make sure your PHP installation has the `Bzlib2` extension enabled before using such a dump.**

### Get Information on Table Import

[](#get-information-on-table-import)

You can register a callable that will be used to report the progress of the restoration:

```
$importer->onTableCreate(function($tableName) {
    echo "Creating table $tableName\n";
});
$importer->onTableInsert(function($tableName, $rowCount) {
    echo "Inserting $rowCount rows into table $tableName\n";
});
```

Errors
------

[](#errors)

To back up a database, you need the following privileges:

- **SELECT**
    - To back up table structures and data.
- **SHOW VIEW**
    - If a database contains views, otherwise you will get an error.
- **TRIGGER**
    - If a table contains one or more triggers.
- **LOCK TABLES**
    - If the "lock tables" option is enabled.

Use **SHOW GRANTS FOR user@host;** to check the user's privileges. See the following link for more information:

[What are the minimum privileges required to get a backup of a MySQL database schema?](https://dba.stackexchange.com/questions/55546/which-are-the-minimum-privileges-required-to-get-a-backup-of-a-mysql-database-sc/55572#55572)

To restore a database, you need the following privileges:

- **ALTER**
    - Required if your backup file contains table alteration instructions.
- **CREATE**
    - Required if your backup file contains table creation instructions.
- **CREATE ROUTINE**
    - Required if your backup file contains routine creation instructions.
- **CREATE VIEW**
    - Required if your backup file contains view creation instructions.
- **DELETE**
    - Required if your backup file contains data deletion instructions.
- **DROP**
    - Required if your backup file contains table or view deletion instructions.
- **INSERT**
    - Required if your backup file contains data insertion instructions into tables.
- **UPDATE**
    - Required if your backup file contains data modification instructions in tables.

Tests
-----

[](#tests)

The unit tests for this package are written using the Kahlan library. The tests cover SQLite, but tests for MySQL have not been written, although tests have been conducted in a real environment. PRs in this direction are welcome.

Todo
----

[](#todo)

- Write more tests, also test with MariaDB.
- Support for other database drivers (PostgreSQL, Oracle, MS Server, MongoDB).

Contribution
------------

[](#contribution)

Please see [CONTRIBUTING](CONTRIBUTING.md) for more details.

License
-------

[](#license)

This project is open-source software licensed under the [MIT](https://opensource.org/license/MIT) license. Please see the [License File](LICENSE.md) for more information.

Credits
-------

[](#credits)

Although significantly modified, the code for **DB Dumper**'s exporter was inspired by [MySQLDump - PHP](https://github.com/ifsnop/mysqldump-php) maintained by [Diego Torres](https://github.com/ifsnop). We would like to thank him.

That said, note that this package was created by [Dimitri Sitchet Tomkeu](https://github.com/dimtrovich) and is maintained by [All Contributors](../../contributors).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance59

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

462d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/828d8411a847755435fbb92104f01daebfc9fa4ac34d3b2e0a3c2a85d05d7480?d=identicon)[Dimtrov](/maintainers/Dimtrov)

---

Top Contributors

[![dimtrovich](https://avatars.githubusercontent.com/u/37987162?v=4)](https://github.com/dimtrovich "dimtrovich (41 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

databasedumpmysqldumpdb-dumperdimtrovich

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dimtrovich-db-dumper/health.svg)

```
[![Health](https://phpackages.com/badges/dimtrovich-db-dumper/health.svg)](https://phpackages.com/packages/dimtrovich-db-dumper)
```

###  Alternatives

[spatie/db-dumper

Dump databases

1.2k25.9M69](/packages/spatie-db-dumper)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[rah/danpu

Zero-dependency MySQL dump library for easily exporting and importing databases

64401.8k10](/packages/rah-danpu)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k22.9k](/packages/clouddueling-mysqldump-php)[lulco/phoenix

Database Migrations for PHP

180329.4k4](/packages/lulco-phoenix)[cytopia/mysqldump-secure

Secure mysqldump script with encryption, compression, logging, blacklisting and Nagios monitoring integration

1474.7k1](/packages/cytopia-mysqldump-secure)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
