Skip to main content

wp db export

Overview

Create a SQL dump of the WordPress database — the foundation of any backup or migration workflow. Reads DB credentials from wp-config.php so you never need to type passwords in commands.

What It Does

wp db export runs mysqldump using the credentials from wp-config.php and writes the result to a .sql file. You can optionally filter which tables to export, pass custom mysqldump flags, and include or exclude specific data.

Syntax

wp db export [<file>] [--tables=<tables>] [--exclude_tables=<tables>] [--porcelain] [--add-drop-table] [--dbuser=<value>] [--dbpass=<value>]

Arguments & Options

Argument / FlagDefaultDescription
FILEwordpress-{datetime}.sqlPath and filename for the export
--tables=TABLESAll tablesComma-separated list of tables to export
--exclude_tables=TABLESNoneTables to skip
--add-drop-tableAdd DROP TABLE IF EXISTS before each CREATE TABLE
--porcelainPrint only the filename (useful for scripting)
--dbuser=VALUEFrom configOverride DB user
--dbpass=VALUEFrom configOverride DB password

Additional mysqldump flags can be passed directly (e.g. --single-transaction, --no-tablespaces).

Basic Usage

Export to auto-named file

wp db export

Output:

Success: Exported to 'wordpress-2026-03-13-abc123.sql'.

Export to a specific filename

wp db export backup.sql

Export with timestamp in filename

wp db export "backup_$(date +%Y%m%d_%H%M%S).sql"

Export specific tables only

wp db export backup.sql --tables=wp_posts,wp_postmeta,wp_options

Export excluding large/unnecessary tables

wp db export backup.sql --exclude_tables=wp_redirection_logs,wp_woocommerce_sessions

Export with InnoDB-safe flags

wp db export backup.sql -- --single-transaction --no-tablespaces

Real-World Scenarios

Scenario 1: Pre-update backup workflow

#!/bin/bash
BACKUP_DIR="/var/backups/wordpress"
mkdir -p "${BACKUP_DIR}"
BACKUP_FILE="${BACKUP_DIR}/backup_$(date +%Y%m%d_%H%M%S).sql"

wp db export "${BACKUP_FILE}" -- --single-transaction
echo "Backup saved: ${BACKUP_FILE}"
gzip "${BACKUP_FILE}"
echo "Compressed: ${BACKUP_FILE}.gz"

Scenario 2: Production to staging migration

# On production
wp db export prod_dump.sql
scp prod_dump.sql user@staging:/var/www/staging/

# On staging
wp db import prod_dump.sql
wp search-replace 'https://example.com' 'https://staging.example.com' --skip-columns=guid
wp cache flush

Scenario 3: Export only user tables (GDPR audit)

wp db export users_export.sql --tables=wp_users,wp_usermeta

Scenario 4: Scheduled nightly backup (cron)

# /etc/cron.daily/wp-backup
BACKUP_DIR="/var/backups/wp"
mkdir -p "${BACKUP_DIR}"
wp --path=/var/www/html db export "${BACKUP_DIR}/nightly_$(date +%Y%m%d).sql" -- --single-transaction
# Keep only 30 days
find "${BACKUP_DIR}" -name "*.sql" -mtime +30 -delete

Scenario 5: Capture filename for downstream use

DUMP=$(wp db export --porcelain)
echo "Dump file: ${DUMP}"
aws s3 cp "${DUMP}" s3://my-bucket/backups/

Best Practices

  1. Always use --single-transaction with InnoDB tables to avoid table locks during export.
  2. Use --no-tablespaces if the DB user lacks PROCESS privilege (avoids export errors).
  3. Compress exports with gzip immediately to save disk space.
  4. Rotate old backups — implement automated deletion of backups older than N days.
  5. Exclude volatile/large tables like sessions, logs, and analytics from routine backups.
  6. Test your backups — periodically restore to a test environment to verify integrity.

Troubleshooting

ProblemCauseFix
Access denied during exportDB user lacks export privilegesUse root or a user with SELECT + LOCK TABLES
mysqldump: unknown optionExtra flags passed incorrectlySeparate WP-CLI flags from mysqldump flags with --
Very large export — disk fullNo space leftPipe through gzip: use --no-tablespaces and compress inline
Tables missing from export--tables typoVerify table names with wp db tables

Quick Reference

wp db export                                          # Auto-named file
wp db export backup.sql # Named file
wp db export "backup_$(date +%Y%m%d).sql" # Timestamped
wp db export backup.sql -- --single-transaction # InnoDB safe
wp db export backup.sql --exclude_tables=wp_wc_sessions # Skip table
wp db export --porcelain # Returns filename only

Next Steps