In this article you will learn how to backup your WordPress database using the wp-cli tool. Knowing how to backup your database is a critical skill to have when running a WordPress site. All your posts, pages, and more live in your database; keeping backups is critical.
You should always take a backup before any major changes to your site, just in case. It’s much quicker to take a backup now and do a restore if you need to, than to find a useful backup when you need it.
Pre-flight Check:
- These instructions were created with a cPanel-based server in mind.
- Command line access via SSH will be necessary to follow along.
- The server must have WP-CLI installed.
To follow along with this tutorial you will need to have WP-CLI installed.
- Login to the server via SSH as the cPanel user that owns the domain, or the root user.
ssh wordpress@127.0.0.1
In the example we are using ssh to connect to the server. You can follow the rest of the steps even if you connect via alternative means(TTY, a Windows SSH Gui, etc).If you login as root you will need to use `su` to take on the user which owns the WordPress site before you continue.
- Now logged in (as the cPanel user) change to the WordPress root folder:
cd ~/public_html
We’re using a cPanel server so we know WordPress should be installed in the `public_html` folder. If you are not on a cPanel server it will be in a different location. - Once in the WordPress root folder (where you can find wp-config.php) you can use the wp-cli tool. To backup your database run one of the following commands:
- Backup the database to the current folder:
wp db export
This will use all the defaults for exporting the database as a backup. This means it will be in the current folder and will have the same name as the database. - Backup the database to a specific file:
wp db export ../my_wordpress_db.sql
Using this command will backup the database to a file called “my_wordpress_db.sql” and will put it in the folder above the current one (or `../`). This is done as a security measure to ensure no one can download your database.
- Backup the database to the current folder:
Once you’ve run the export command you’ll see confirmation output like the following:
After you see that confirmation your WordPress database is officially backed up. Now you can continue making changes or updates and will have a way to restore if anything goes wrong.