Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ To confirm the ID for the site you want to query, you can use the `wp site list`
Exports the database to a file or to STDOUT.

~~~
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults]
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults]
~~~

Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
Expand All @@ -456,6 +456,9 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
[--exclude_tables=<tables>]
The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.

[--exclude_tables_data=<tables>]
The comma separated list of specific tables for which only the structure will be exported. Excluding this parameter will export data for all tables in the export.

[--include-tablespaces]
Skips adding the default --no-tablespaces option to mysqldump.

Expand Down Expand Up @@ -506,6 +509,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
$ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
Success: Exported to 'wordpress_dbase-db72bb5.sql'.

# Skip data of certain tables from the exported database
$ wp db export --exclude_tables_data=wp_actionscheduler_logs
Success: Exported to 'wordpress_dbase-db72bb5.sql'.

# Export database to STDOUT.
$ wp db export -
-- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
Expand Down
14 changes: 14 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ Feature: Export a WordPress database
wp_options
"""

Scenario: Exclude data of certain tables when exporting the database
Given a WP install

When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain`
Then the wp_cli_test.sql file should exist
And the wp_cli_test.sql file should contain:
"""
wp_users
"""
And the wp_cli_test.sql file should not contain:
"""
INSERT INTO `wp_users`
"""

Scenario: Export database to STDOUT
Given a WP install

Expand Down
18 changes: 18 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ public function query( $args, $assoc_args ) {
* [--exclude_tables=<tables>]
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
*
* [--exclude_tables_data=<tables>]
* : The comma separated list of specific tables for which only the structure will be exported. Excluding this parameter will export data for all tables in the export.
*
* [--include-tablespaces]
* : Skips adding the default --no-tablespaces option to mysqldump.
*
Expand Down Expand Up @@ -617,6 +620,10 @@ public function query( $args, $assoc_args ) {
* $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
*
* # Skip data of certain tables from the exported database
* $ wp db export --exclude_tables_data=wp_actionscheduler_logs
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
*
* # Export database to STDOUT.
* $ wp db export -
* -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
Expand Down Expand Up @@ -708,6 +715,17 @@ public function export( $args, $assoc_args ) {
}
}

$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data' );
if ( isset( $exclude_tables_data ) ) {
$tables = explode( ',', trim( $assoc_args['exclude_tables_data'], ',' ) );
unset( $assoc_args['exclude_tables_data'] );
foreach ( $tables as $table ) {
$command .= ' --ignore-table-data';
$command .= ' %s';
$command_esc_args[] = trim( DB_NAME . '.' . $table );
}
}

$escaped_command = Utils\esc_cmd( $command, ...$command_esc_args );

// Remove parameters not needed for SQL run.
Expand Down
Loading