Skip to content
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,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] [--defaults]
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
~~~

**Alias:** `dump`
Expand Down Expand Up @@ -460,6 +460,10 @@ 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.
Note: currently only supported by MariaDB.

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

Expand Down Expand Up @@ -507,6 +511,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
27 changes: 27 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ Feature: Export a WordPress database
PRAGMA foreign_keys=OFF
"""

@skip-mariadb @skip-sqlite
Scenario: Exclude data of certain tables is not supported by MySQL
Given a WP install

When I try `wp db export wp_cli_test.sql --exclude_tables_data=wp_users`
Then the return code should be 1
And STDERR should contain:
"""
Error: The --exclude_tables_data option is only supported by MariaDB.
"""

# Only MariaDB currently supports this feature.
@require-mariadb
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`
"""

@skip-sqlite
Scenario: Export database to STDOUT
Given a WP install
Expand Down
28 changes: 28 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ 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.
* Note: currently only supported by MariaDB.
*
* [--include-tablespaces]
* : Skips adding the default --no-tablespaces option to mysqldump.
*
Expand Down Expand Up @@ -706,6 +710,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 @@ -805,6 +813,26 @@ public function export( $args, $assoc_args ) {
}
}

$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' );
if ( ! empty( $exclude_tables_data ) ) {
unset( $assoc_args['exclude_tables_data'] );

if ( 'mariadb' !== Utils\get_db_type() ) {
WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' );
}

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

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

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