Skip to content
Open
9 changes: 8 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] [--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]
~~~

**Alias:** `dump`
Expand Down Expand Up @@ -460,6 +460,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 @@ -510,6 +513,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
16 changes: 16 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ Feature: Export a WordPress database
PRAGMA foreign_keys=OFF
"""

# 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
29 changes: 29 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option exists from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13 and 10.5.3 (MDEV-22037), not 10.1.0.
I checked 10.4.12 and 10.5.2 (rejected) and 10.4.13 and 10.5.3 (accepted). README.md line 472 needs the same text.

Suggested change
* Note: currently only supported by MariaDB.
* Note: needs a dump client with --ignore-table-data (mariadb-dump from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13, 10.5.3 or later). MySQL's mysqldump does not have it. Cannot be combined with --tables.

*
Comment on lines +687 to +690

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== candidate files =="
git ls-files | rg '(^|/)DB_Command\.php$|(^|/)README\.md$' || true

echo
echo "== relevant mentions of exclude_tables_data and MariaDB minimum/version =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' \
  'exclude_tables_data|MariaDB|10\.1|10|11|version' \
  src/DB_Command.php README.md 2>/dev/null || true

echo
echo "== DB_Command.php context =="
sed -n '660,715p' src/DB_Command.php 2>/dev/null || true

echo
echo "== README.md context =="
sed -n '455,485p' README.md 2>/dev/null || true

Repository: wp-cli/db-command

Length of output: 8057


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== all repository references to MariaDB 10.1/10.1 minimum and export include/exclude =="
rg -n --hidden --glob '!vendor/**' --glob '!node_modules/**' \
  'MariaDB\s*(10\.1|10|11)|10\.1\s*MariaDB|mariadb|MariaDB \d+\.\d+|export|--include_tables|--exclude_tables' .

echo
echo "== DB_Command.php outline around DB export methods =="
ast-grep outline src/DB_Command.php --match 'export' --view expanded 2>/dev/null || true

Repository: wp-cli/db-command

Length of output: 16553


🌐 Web query:

MariaDB 10.1 SET export documentation --set-gtid-purged

💡 Result:

The MariaDB mysqldump utility does not support the --set-gtid-purged flag [1][2][3]. This flag is specific to the MySQL mysqldump implementation [1]. If you attempt to use --set-gtid-purged=OFF (or any other value) with MariaDB's mysqldump, the command will fail with an error stating that it is an "unknown variable" [4][2][3]. This incompatibility arises because MariaDB and MySQL use different, non-compatible implementations for Global Transaction IDs (GTIDs) [1][5][6]. Consequently, the options related to GTIDs in the MySQL version of mysqldump do not exist in the MariaDB version [1]. If you encounter this error in automation scripts or build tools (such as Laravel or custom shell scripts), the correct solution is to remove the --set-gtid-purged flag from your mysqldump command [1][7][2][3].

Citations:


🌐 Web query:

MariaDB server options documentation --set-gtid-purged --skip-set-gtid-purged MariaDB 10.1

💡 Result:

The command-line options --set-gtid-purged and --skip-set-gtid-purged do not exist in the MariaDB version of mysqldump, including in MariaDB 10.1 [1][2][3]. These options are specific to the MySQL implementation of mysqldump [4][1]. Because MariaDB's GTID implementation differs from MySQL's, MariaDB does not support these flags [1]. Attempting to use them with a MariaDB-provided mysqldump client will result in an "unknown variable" error and the command will fail [2][5][3][6]. If you encounter this error (common when using tools designed for MySQL against a MariaDB client environment), the correct solution is to remove these flags from your command-line arguments [1][7][3]. MariaDB uses its own methods and options for managing GTIDs during backups and replication, such as the --gtid option [8][9].

Citations:


🌐 Web query:

site:Mariadb.com mysqldump SET_SESSION_GTID_PRUNE MariaDB mysqldump options MariaDB 10.1 exclude tables data structure only

💡 Result:

The command formerly known as mysqldump is now called mariadb-dump in MariaDB, though mysqldump remains accessible as a symbolic link in many versions [1][2]. Regarding your specific queries: SET_SESSION_GTID_PRUNE There is no standard mysqldump or mariadb-dump option named SET_SESSION_GTID_PRUNE. MariaDB manages GTID-based replication using options like --gtid, which, when combined with --master-data or --dump-slave, automatically includes necessary SQL statements (such as setting gtid_slave_pos and executing CHANGE MASTER) to configure a replica [3][4][5]. If you encounter SET_SESSION_GTID_PRUNE, it is likely a non-standard or custom session variable; standard session variables are managed via the SET statement in SQL [6]. Exclude tables To exclude specific tables when using mariadb-dump (or mysqldump), use the --ignore-table option. You must specify the table in the format db_name.table_name. You can use this option multiple times to exclude multiple tables: mariadb-dump --ignore-table=database_name.table_name1 --ignore-table=database_name.table_name2 database_name > backup.sql Data structure only To export only the database structure (schema) without table data, use the --no-data option [7][8]: mariadb-dump --no-data database_name > schema_backup.sql Combining these, to dump the structure of a database while excluding specific tables: mariadb-dump --no-data --ignore-table=db.table1 --ignore-table=db.table2 db_name > schema_only_exclude_tables.sql

Citations:


🌐 Web query:

"exclude_tables_data" "MariaDB" "10.1" "10" "11"

💡 Result:

In MariaDB, the --ignore-table-data option is the specific command-line argument used to exclude the data (contents) of a table from a backup while still exporting its structure [1][2][3]. Usage Requirements The option requires the full format of database_name.table_name [1][2]. If you need to exclude data for multiple tables, you must repeat the --ignore-table-data option for each table [1][3]. Example To back up a database named my_database while excluding data from tables named table1 and table2: mariadb-dump my_database --ignore-table-data=my_database.table1 --ignore-table-data=my_database.table2 > backup.sql Version Compatibility This option is a feature specific to MariaDB's dump utility and is not available in standard MySQL mysqldump [4]. It was introduced in the following MariaDB releases: - MariaDB 10.1.46 and later [2][3] - MariaDB 10.2.33 and later [2][3] - MariaDB 10.3.24 and later [2][3] - MariaDB 10.4.14 and later [2][3] - MariaDB 10.5.3 and later [2][3] For versions of MariaDB newer than 10.5.3, including 10.11, this option remains available and is the standard way to perform this task [1][5]. If you encounter an error when using this command, ensure you are using the MariaDB-provided version of the dump utility (mariadb-dump or mysqldump) rather than a client bundled with a different MySQL distribution [4].

Citations:


Include the MariaDB 10.1 minimum for --exclude_tables_data.

Both src/DB_Command.php and README.md say the option is available only for MariaDB but omit the minimum version. Add the same MariaDB 10.1 and later wording to both references.

📍 Affects 2 files
  • src/DB_Command.php#L687-L690 (this comment)
  • README.md#L470-L472
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/DB_Command.php` around lines 687 - 690, Update the --exclude_tables_data
documentation in src/DB_Command.php lines 687-690 and README.md lines 470-472 to
state that the option is supported by MariaDB 10.1 and later, using consistent
wording in both references.

Source: Coding guidelines

* [--include-tablespaces]
* : Skips adding the default --no-tablespaces option to mysqldump.
*
Expand Down Expand Up @@ -708,6 +712,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 @@ -807,6 +815,27 @@ public function export( $args, $assoc_args ) {
}
}

$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data' );
if ( isset( $exclude_tables_data ) ) {
unset( $assoc_args['exclude_tables_data'] );
if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data ) ) {
if ( 'mariadb' !== Utils\get_db_type() ) {
WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' );
Comment thread
swissspidy marked this conversation as resolved.
Resolved
}

$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