db-dumper editorconfig scrutinizer styleci travis-ci Packagist phpunit

Dump the contents of a database

Dump the contents of a database

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

This repo contains an easy to use class to dump a database using PHP. Currently MySQL and PostgreSQL are supported. Behind the scenes mysqldump and pg_dump are used.

Here’s a simple example of how to create a dump of MySQL-database:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->dumpToFile('dump.sql');

And here’s the PostgreSQL version of that:

Spatie\DbDumper\Databases\PostgreSql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->dumpToFile('dump.sql');

Spatie is a webdesign agency based in Antwerp, Belgium. You’ll find an overview of all our open source projects on our website.

Requirements

For dumping MySQL-db’s mysqldump should be installed.

For dumping PostgreSQL-db’s pg_dump should be installed.

Postcardware

You’re free to use this package (it’s MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

$ composer require spatie/db-dumper

Usage

This is the simplest way to create a dump of a MySql db:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->dumpToFile('dump.sql');

If you’re working with PostgreSQL just use that dumper, most methods are available on both the MySql. and PostgreSql-dumper.

Spatie\DbDumper\Databases\PostgreSql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->dumpToFile('dump.sql');

If the mysqldump (or pg_dump) binary is installed in a non default location you can let the package know by using thesetDumpBinaryPath()-function:

Spatie\DbDumper\Databases\MySql::create()
    ->setDumpBinaryPath('/custom/location')
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->dumpToFile('dump.sql');

Dump specific tables

Using an array:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->includeTables(['table1', 'table2', 'table3'])
    ->dumpToFile('dump.sql');

Using a string:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->includeTables('table1, table2, table3')
    ->dumpToFile('dump.sql');

Excluding tables from the dump

Using an array:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->excludeTables(['table1', 'table2', 'table3'])
    ->dumpToFile('dump.sql');

Using a string:

Spatie\DbDumper\Databases\MySql::create()
    ->setDbName($databaseName)
    ->setUserName($userName)
    ->setPassword($password)
    ->excludeTables('table1, table2, table3')
    ->dumpToFile('dump.sql');

Adding extra options

If you want to add an arbitrary option to the dump command you can use addOption

$dumpCommand = MySql::create()
    ->setDbName('dbname')
    ->setUserName('username')
    ->setPassword('password')
    ->addExtraOption('--xml')
    ->getDumpCommand('dump.sql', 'credentials.txt');

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Initial PostgreSQL support was contributed by Adriano Machado.

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You’ll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

Related Repositories

db-dumper

db-dumper

Dump the contents of a database ...

ydd

ydd

ydd - yaml db dumper. Fork of yaml_db to provide a nice middle ground between it and tabs for backup purposes etc. ...

rewolf-superfetch-dumper

rewolf-superfetch-dumper

Dumper for Windows SuperFetch files (Ag*.db) ...

dump_tweets.py

dump_tweets.py

Incremental Dumper (to DB) of tweets from Twitters ...

php-db-dumper

php-db-dumper

A PHP database dumper ...


Top Contributors

freekmurze ammachado Rhincodon tylercd100 mikechernev pmatseykanets sebastiandedeyne trevorgehman oriceon

Releases

-   2.3.0 zip tar
-   2.2.1 zip tar
-   2.1.0 zip tar
-   2.0.1 zip tar
-   2.0.0 zip tar
-   1.5.1 zip tar
-   1.5.0 zip tar
-   1.4.0 zip tar
-   1.3.0 zip tar
-   1.2.4 zip tar
-   1.2.3 zip tar
-   1.2.2 zip tar
-   1.2.1 zip tar
-   1.2.0 zip tar
-   1.1.0 zip tar
-   1.0.4 zip tar
-   1.0.3 zip tar
-   1.0.2 zip tar
-   1.0.1 zip tar
-   1.0.0 zip tar
-   0.0.2 zip tar
-   0.0.1 zip tar