Updated: 18 October 2026 · Applies to: WordPress 7.1.3 with WP-CLI 2.12.0 (tested on PHP 8.3 with MariaDB 11.8)
To back up a WordPress database with WP-CLI, go to the folder that holds wp-config.php and run wp db export ~/wp-backup-$(date +%F).sql. WP-CLI reads the database name, user and password from wp-config.php and runs mysqldump (or mariadb-dump) for you, so you do not need to type any database password.
Before you start
- You need a shell: SSH, or Terminal in cPanel (in the Advanced section; according to cPanel it only appears when your account has shell access).
- Check that WP-CLI is there with
wp --info. If you get "command not found", install it first: How to install WP-CLI. - Run WP-CLI as the user that owns the site, not as root. As root it stops with
Error: YIKES! It looks like you're running this as root.
Back up the database
- Go to your WordPress folder (on cPanel hosting this is usually
public_html):cd ~/public_html
- Optional: check the tables first, so you do not back up a damaged database:
wp db check
Every table should showOK, followed bySuccess: Database checked. - Export the database to your home folder, outside the website:
wp db export ~/wp-backup-$(date +%F).sql
In our test the output was:Success: Exported to '/home/example/wp-backup-2026-10-08.sql'.
- Make the file readable only by you, because it contains user accounts and password hashes:
chmod 600 ~/wp-backup-*.sql
- Download a copy to your computer with File Manager or SFTP. A backup that lives only on the same server does not protect you if the server fails (see Should I keep a backup copy of my site on my computer?).
Save space with a compressed backup
Use - as the file name to send the dump to standard output and compress it on the fly:
wp db export - | gzip > ~/wp-backup-$(date +%F).sql.gz
On our test site the plain dump was 130 KB and the gzip file 23 KB. SQL text usually compresses this well.
Useful options
- No file name:
wp db exporton its own writes a file such aswpdb-2026-10-08-814bf56.sqlinto the current folder. If you are inpublic_html, that file can be downloaded from the web, so always give a path outside it. - Only some tables:
wp db export --tables=wp_posts,wp_postmeta ~/posts.sql - Skip tables:
wp db export --exclude_tables=wp_comments,wp_commentmeta ~/no-comments.sql - Add DROP TABLE statements, so the file can overwrite existing tables when you import it:
--add-drop-table - Run from anywhere:
wp db export --path=/home/username/public_html ~/backup.sql - Print only the file name (handy in scripts):
--porcelain
Use wp db tables to see your table names; the prefix (here wp_) can be different on your site. WP-CLI adds the --no-tablespaces option to mysqldump by default.
Run the backup automatically with cron
Create a folder for the backups, then add a cron job (in cPanel: Cron Jobs, see Setting a cron job). This line makes a compressed backup every day at 03:00:
0 3 * * * cd /home/username/public_html && /usr/local/bin/wp db export - --quiet | gzip > /home/username/backups/db-$(date +\%F).sql.gz
- Replace
/usr/local/bin/wpwith the path thatcommand -v wpprints on your server; cron does not always have the same PATH as your login shell. - The
\%matters: in a crontab an unescaped%is turned into a new line and cuts the command short. - Remove old copies so the disk does not fill up, for example with a second job:
find /home/username/backups -name 'db-*.sql.gz' -mtime +14 -delete
Restore the backup
Import the file with wp db import ~/wp-backup-2026-10-08.sql, or for a gzip file gunzip < ~/wp-backup-2026-10-08.sql.gz | wp db import -. Both worked in our test (Success: Imported from 'STDIN'.). Importing replaces the current data, so export the current database first. More detail: How to import a database using WP-CLI.
Common problems
Error: This does not seem to be a WordPress installation.You are not in the folder withwp-config.php. Usecdor add--path=.env: can't execute 'mysqldump': No such file or directoryThe MySQL or MariaDB client tools are missing on the server. On your own VPS install the client package; on shared hosting ask support.Got error: 1045: "Access denied for user ..."The database user or password inwp-config.phpis wrong. Your site will usually show Error establishing a database connection as well.
What this backup does not include
wp db export saves only the database: posts, pages, settings, users and comments. Your uploads, themes and plugins are files in wp-content. Back them up as well, for example with tar -czf ~/wp-files-$(date +%F).tar.gz -C ~/public_html wp-content, or make a full account backup in cPanel (How do I backup my site in cPanel?). Without a shell, export the database in phpMyAdmin instead (How can I backup a database using phpMyAdmin?).
Looking for WordPress hosting with cPanel? See Ucartz Linux web hosting.
Official documentation: wp db export (WP-CLI)