{"id":13548966,"url":"https://github.com/spatie/db-dumper","last_synced_at":"2025-05-12T15:34:26.911Z","repository":{"id":3516486,"uuid":"49829051","full_name":"spatie/db-dumper","owner":"spatie","description":"Dump the contents of a database","archived":false,"fork":false,"pushed_at":"2025-05-12T08:35:36.000Z","size":333,"stargazers_count":1141,"open_issues_count":0,"forks_count":151,"subscribers_count":24,"default_branch":"main","last_synced_at":"2025-05-12T09:57:57.824Z","etag":null,"topics":["database","dump","mysql","php","postgresql","sql"],"latest_commit_sha":null,"homepage":"https://freek.dev/389-a-package-to-dump-your-database","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2016-01-17T18:09:55.000Z","updated_at":"2025-05-12T08:35:33.000Z","dependencies_parsed_at":"2023-02-19T17:25:22.319Z","dependency_job_id":"6cd9f32d-a930-47d5-a43f-f36e3c3ee7cf","html_url":"https://github.com/spatie/db-dumper","commit_stats":{"total_commits":271,"total_committers":63,"mean_commits":4.301587301587301,"dds":0.4391143911439115,"last_synced_commit":"2fa7c751dd335b6c80ad6fb3ed97e3fed475814f"},"previous_names":[],"tags_count":79,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdb-dumper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdb-dumper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdb-dumper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdb-dumper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/db-dumper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253766272,"owners_count":21960881,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","dump","mysql","php","postgresql","sql"],"created_at":"2024-08-01T12:01:16.610Z","updated_at":"2025-05-12T15:34:26.883Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"# Dump the contents of a database\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/db-dumper.svg?style=flat-square)](https://packagist.org/packages/spatie/db-dumper)\n![run-tests](https://github.com/spatie/db-dumper/workflows/run-tests/badge.svg)\n[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/db-dumper.svg?style=flat-square)](https://packagist.org/packages/spatie/db-dumper)\n\nThis repo contains an easy to use class to dump a database using PHP. Currently MySQL, PostgreSQL, SQLite and MongoDB are supported. Behind the scenes `mysqldump`, `pg_dump`, `sqlite3` and `mongodump` are used.\n\nHere are simple examples of how to create a database dump with different drivers:\n\n**MySQL**\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.sql');\n```\n\n**PostgreSQL**\n\n```php\nSpatie\\DbDumper\\Databases\\PostgreSql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.sql');\n```\n\n**SQLite**\n\n```php\nSpatie\\DbDumper\\Databases\\Sqlite::create()\n    -\u003esetDbName($pathToDatabaseFile)\n    -\u003edumpToFile('dump.sql');\n```\n\n⚠️ Sqlite version 3.32.0 is required when using the `includeTables` option.\n\n**MongoDB**\n\n```php\nSpatie\\DbDumper\\Databases\\MongoDb::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.gz');\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/db-dumper.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/db-dumper)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Requirements\n\nFor dumping MySQL-db's `mysqldump` should be installed.\n\nFor dumping PostgreSQL-db's `pg_dump` should be installed.\n\nFor dumping SQLite-db's `sqlite3` should be installed.\n\nFor dumping MongoDB-db's `mongodump` should be installed.\n\nFor compressing dump files, `gzip` and/or `bzip2` should be installed.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/db-dumper\n```\n\n## Usage\n\nThis is the simplest way to create a dump of a MySql db:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.sql');\n```\n\nIf you're working with PostgreSQL just use that dumper, most methods are available on both the MySql. and PostgreSql-dumper.\n\n```php\nSpatie\\DbDumper\\Databases\\PostgreSql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.sql');\n```\n\nIf the `mysqldump` (or `pg_dump`) binary is installed in a non default location you can let the package know by using the`setDumpBinaryPath()`-function:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDumpBinaryPath('/custom/location')\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edumpToFile('dump.sql');\n```\n\nIf your application is deployed and you need to change the host (default is 127.0.0.1), you can add the `setHost()`-function:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003esetHost($host)\n    -\u003edumpToFile('dump.sql');\n```\n\n### Handling AUTO_INCREMENT Values in Dumps\n\nWhen creating a database dump, you might need to control the inclusion of AUTO_INCREMENT values. This can be crucial for avoiding primary key conflicts or for maintaining ID consistency when transferring data across environments.\n\n#### Skipping AUTO_INCREMENT Values\n\nTo omit the AUTO_INCREMENT values from the tables in your dump, use the skipAutoIncrement method. This is particularly useful to prevent conflicts when importing the dump into another database where those specific AUTO_INCREMENT values might already exist, or when the exact values are not relevant.\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003eskipAutoIncrement()\n    -\u003edumpToFile('dump.sql');\n```\n\n### Including AUTO_INCREMENT values in the dump\n\nBy default, the AUTO_INCREMENT values are included in the dump. However, if you previously used the skipAutoIncrement method and wish to ensure that the AUTO_INCREMENT values are included in a subsequent dump, use the dontSkipAutoIncrement method to explicitly include them.\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003edontSkipAutoIncrement()\n    -\u003edumpToFile('dump.sql');\n```\n\n### Use a Database URL\n\nIn some applications or environments, database credentials are provided as URLs instead of individual components. In this case, you can use the `setDatabaseUrl` method instead of the individual methods.\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDatabaseUrl($databaseUrl)\n    -\u003edumpToFile('dump.sql');\n```\n\nWhen providing a URL, the package will automatically parse it and provide the individual components to the applicable dumper.\n\nFor example, if you provide the URL `mysql://username:password@hostname:3306/dbname`, the dumper will use the `hostname` host, running on port `3306`, and will connect to `dbname` with `username` and `password`.\n\n### Dump specific tables\n\nUsing an array:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003eincludeTables(['table1', 'table2', 'table3'])\n    -\u003edumpToFile('dump.sql');\n```\n\nUsing a string:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003eincludeTables('table1, table2, table3')\n    -\u003edumpToFile('dump.sql');\n```\n\n### Don't use column_statics table with some old version of MySql service.\n\nIn order to use \"_--column-statistics=0_\" as option in mysqldump command you can use _doNotUseColumnStatistics()_ method.\n\nIf you have installed _mysqldump 8_, it queries by default _column_statics_ table in _information_schema_ database.\nIn some old version of MySql (service) like 5.7, this table doesn't exist. So you could have an exception during the execution of mysqldump. To avoid this, you could use _doNotUseColumnStatistics()_ method.\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003edoNotUseColumnStatistics()\n    -\u003edumpToFile('dump.sql');\n```\n\n### Excluding tables from the dump\n\nYou can exclude tables from the dump by using an array:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003eexcludeTables(['table1', 'table2', 'table3'])\n    -\u003edumpToFile('dump.sql');\n```\n\nOr by using a string:\n\n```php\nSpatie\\DbDumper\\Databases\\MySql::create()\n    -\u003esetDbName($databaseName)\n    -\u003esetUserName($userName)\n    -\u003esetPassword($password)\n    -\u003eexcludeTables('table1, table2, table3')\n    -\u003edumpToFile('dump.sql');\n```\n\n### Do not write CREATE TABLE statements that create each dumped table\n\nYou can use `doNotCreateTables` to prevent writing create statements.\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003edoNotCreateTables()\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\n### Do not write row data\n\nYou can use `doNotDumpData` to prevent writing row data.\n\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003edoNotDumpData()\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\n### Append instead of overwriting a dump file\n\nYou can use `useAppendMode` with MySQL to append to the file instead of overwriting it. \nThis is useful for two-step dumps when you want to dump the whole schema but only some of the data \n(for example: only migrations, or only product but not customer data).\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003euseAppendMode()\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\n### Adding extra options\n\nIf you want to add an arbitrary option to the dump command you can use `addExtraOption`\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003eaddExtraOption('--xml')\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\nIf you're working with MySql you can set the database name using `--databases` as an extra option. This is particularly useful when used in conjunction with the `--add-drop-database` `mysqldump` option (see the [mysqldump docs](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database)).\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003eaddExtraOption('--databases dbname')\n    -\u003eaddExtraOption('--add-drop-database')\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\nWith MySql, you also have the option to use the `--all-databases` extra option. This is useful when you want to run a full backup of all the databases in the specified MySQL connection.\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003eaddExtraOption('--all-databases')\n    -\u003egetDumpCommand('dump.sql', 'credentials.txt');\n```\n\nPlease note that using the `-\u003eaddExtraOption('--databases dbname')` or `-\u003eaddExtraOption('--all-databases')` will override the database name set on a previous `-\u003esetDbName()` call.\n\n### Using compression\n\nIf you want the output file to be compressed, you can use a compressor class.\n\nThere are two compressors that come out of the box:\n\n-   `GzipCompressor` - This will compress your db dump with `gzip`. Make sure `gzip` is installed on your system before using this.\n-   `Bzip2Compressor` - This will compress your db dump with `bzip2`. Make sure `bzip2` is installed on your system before using this.\n\n```php\n$dumpCommand = MySql::create()\n    -\u003esetDbName('dbname')\n    -\u003esetUserName('username')\n    -\u003esetPassword('password')\n    -\u003euseCompressor(new GzipCompressor()) // or `new Bzip2Compressor()`\n    -\u003edumpToFile('dump.sql.gz');\n```\n\n### Creating your own compressor\n\nYou can create you own compressor implementing the `Compressor` interface. Here's how that interface looks like:\n\n```php\nnamespace Spatie\\DbDumper\\Compressors;\n\ninterface Compressor\n{\n    public function useCommand(): string;\n\n    public function useExtension(): string;\n}\n```\n\nThe `useCommand` should simply return the compression command the db dump will get pumped to. Here's the implementation of `GzipCompression`.\n\n```php\nnamespace Spatie\\DbDumper\\Compressors;\n\nclass GzipCompressor implements Compressor\n{\n    public function useCommand(): string\n    {\n        return 'gzip';\n    }\n\n    public function useExtension(): string\n    {\n        return 'gz';\n    }\n}\n```\n\n## Testing\n\n```bash\n$ composer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n-   [Freek Van der Herten](https://github.com/freekmurze)\n-   [All Contributors](../../contributors)\n\nInitial PostgreSQL support was contributed by [Adriano Machado](https://github.com/ammachado). SQlite support was contributed by [Peter Matseykanets](https://twitter.com/pmatseykanets).\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fdb-dumper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fdb-dumper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fdb-dumper/lists"}