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 / Flag | Default | Description |
|---|---|---|
FILE | wordpress-{datetime}.sql | Path and filename for the export |
--tables=TABLES | All tables | Comma-separated list of tables to export |
--exclude_tables=TABLES | None | Tables to skip |
--add-drop-table | — | Add DROP TABLE IF EXISTS before each CREATE TABLE |
--porcelain | — | Print only the filename (useful for scripting) |
--dbuser=VALUE | From config | Override DB user |
--dbpass=VALUE | From config | Override 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
- Always use
--single-transactionwith InnoDB tables to avoid table locks during export. - Use
--no-tablespacesif the DB user lacks PROCESS privilege (avoids export errors). - Compress exports with
gzipimmediately to save disk space. - Rotate old backups — implement automated deletion of backups older than N days.
- Exclude volatile/large tables like sessions, logs, and analytics from routine backups.
- Test your backups — periodically restore to a test environment to verify integrity.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Access denied during export | DB user lacks export privileges | Use root or a user with SELECT + LOCK TABLES |
mysqldump: unknown option | Extra flags passed incorrectly | Separate WP-CLI flags from mysqldump flags with -- |
| Very large export — disk full | No space left | Pipe through gzip: use --no-tablespaces and compress inline |
| Tables missing from export | --tables typo | Verify 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
wp db import— restore a database from a dump.wp search-replace— update URLs after importing to a new environment.wp db tables— list tables to plan what to include or exclude.