brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Guide: back up WordPress with WP-CLI before every update, and test that it restores

Guide: back up WordPress with WP-CLI before every update, and test that it restores

Guide wordpress

9 steps 11 min read

My blog runs on WordPress, so this is a routine I care about: before any update I make a copy of the database and the files, and I make sure I could actually get them back. WP-CLI turns that into a handful of commands. This guide takes you from a site that is about to update to a backup you have tested. It also covers what to do when the update goes wrong anyway, how to automate a weekly dump with cron, and how to check afterwards that nothing in your files changed that should not have.

Time
11 min
Steps
9
Updated
22 Nov 2026

You will learn

  • See what is waiting
  • Back up the database first
  • Back up the files
  • Update
  • Test the restore, once

What you need

You need shell access to the server (SSH) and WP-CLI installed. Most decent hosts have it; check with wp --info. The commands run from the folder that holds wp-config.php, because WP-CLI reads the database credentials from that file. From anywhere else you add --path=/path/to/your/site. If your host only gives you a control panel, the same idea works there: export the database (the handbook describes a simple backup with phpMyAdmin), copy the files, and test the copy.

The WordPress handbook says it in one sentence: back up your database regularly, and always before an upgrade. It also says a site is two parts, files and database, and that downloading the files does not get you the database. That second part is where people get caught, because the database lives in MySQL or MariaDB, outside the WordPress folder. Your posts and comments are in there, and so is most of your plugin configuration.

Your host probably makes backups of the whole server too. The handbook is polite about that and then points out the catch: asking a host for a copy takes time, and a fast recovery is the whole point. A backup you can make and restore yourself is the one you can count on on a bad afternoon.

Step 1 of 9: See what is waiting

Before I back up anything I want to know what the update is going to touch. WP-CLI can list it without changing a thing.

wp core version
wp core check-update
wp plugin list --update=available
wp theme list --update=available

The plugin command also has a dry run, which is the one I would use as a final check. It shows which plugins would be updated and does nothing else.

wp plugin update --all --dry-run

A short list is a calm update. A list with one plugin that jumped a whole major version is the one I’d update alone, after the backup, and not as part of --all. WP-CLI has the flags for it: --minor only takes minor releases and --exclude=name keeps one plugin out of the batch.

Screenshot of the WP-CLI documentation for wp plugin update, showing the options such as --all, --exclude, --minor and --dry-run
The options of wp plugin update

Step 2 of 9: Back up the database first

The handbook’s order is database first, files second. The reason is simple: a backup set should be one moment in time, and the database is the part that changes while you work. Before you dump, look at how big it is, because that tells you what to expect from the file:

wp db size --human-readable
wp db size --tables

The second command lists every table with its size. A wp_options table or a log table of a plugin that grew to hundreds of megabytes explains a slow dump, and it is worth a look on its own.

wp db export ~/backups/before-update-$(date +%F).sql

wp db export runs mysqldump with the credentials from wp-config.php, so you do not type a password. If you leave the file name out, WP-CLI makes one from the database name, the date and a random hash. Give it your own name, as above, so you can find it in six months. --tables=wp_posts exports only some tables, and --add-drop-table makes a dump that overwrites existing tables when you import it. For a full site backup I just take everything.

Compress it if the site is big. A dump is text and shrinks a lot, and gzip -t tells you afterwards whether the compressed file is intact:

gzip ~/backups/before-update-$(date +%F).sql
gzip -t ~/backups/before-update-$(date +%F).sql.gz && echo intact

A dump contains your users, their password hashes and often their email addresses. Treat the file like the database itself. If you make backups by hand, run umask 077 first so only your own user can read what you create, and do not leave dumps in a folder the web server serves.

Screenshot of the WP-CLI documentation for wp db export, showing the file argument, --tables, --exclude_tables and --porcelain
The options of wp db export

Step 3 of 9: Back up the files

Files means core, themes, plugins, uploads, wp-config.php and .htaccess. The uploads folder is usually the biggest part and the one you cannot get back from anywhere else.

tar -czf ~/backups/files-$(date +%F).tar.gz -C /path/to/your/site .

Put the database dump and the file archive in the same folder, with the same date in the name. The handbook suggests keeping them together as one backup set, and I agree: a dump from Tuesday with files from Friday restores into a site that does not quite make sense. The picture below shows the order for backing up and for restoring, which is the reverse.

Then get a copy off that server. The handbook recommends keeping three to five recent backups in different places, for example on the host, in cloud storage and on your own computer. A backup that sits next to the site it protects goes down with the site. From my own computer, one line pulls it over:

scp "user@your-server:backups/*$(date +%F)*" ~/Backups/wordpress/

Where that copy ends up matters in the EU. A backup holds your users, your comments and probably some email addresses, so it is personal data under the GDPR. Cloud storage that sits in the EU, or that you encrypt before it leaves your hands, saves you a question later. I wrote a longer guide on that part, backups with Duplicati to a European provider, and the same thinking applies here.

Diagram of the backup order (database, files, copy off the server, update) and the reverse restore order (files, credentials, database, check)
Backup order and restore order

Step 4 of 9: Update

Now the update itself. Core first, then plugins, then themes, and look at the site in between.

wp core update
wp plugin update --all
wp theme update --all

If you want to be careful, wp core update --minor only takes minor releases, and wp plugin update --all --exclude=name keeps the one plugin you are nervous about out of the batch. WP-CLI has a lock, and if an update stops halfway you can meet “Another update is currently in progress.” The WP-CLI docs say to check that no other update is really running and then clear the lock with wp option delete core_updater.lock. Do the check first.

Open the front page, one post, the login and the admin. Two minutes of clicking tells you more than any command. If you run a caching plugin, clear its cache first, or you are looking at the old site.

Step 5 of 9: Test the restore, once

A backup you have never restored might not work. The handbook even says to back up your automatic backups by hand now and then, just to find out whether the automation still works. Do the restore test once on a copy of the site: a second install on your own machine, or a staging folder with its own empty database.

The order for a restore is the reverse of the backup: files first, database second. If the database credentials on the new place are different, change them in wp-config.php to match.

tar -xzf ~/backups/files-2026-11-19.tar.gz -C /path/to/staging
gunzip ~/backups/before-update-2026-11-19.sql.gz
wp db import ~/backups/before-update-2026-11-19.sql

wp db import does not create a database. It runs the SQL file against the one in wp-config.php, so create an empty database and point wp-config.php at it first. When the import finishes, load the staging site. If your posts and your uploaded pictures are both there, the backup works. If the pictures are missing you backed up the database and forgot the files, which is exactly the mistake the handbook warns about, and it is far better to find it now.

The site address often differs between live and staging. wp search-replace fixes that, and it has a dry run too, and there is a tip about it on this site, linked below.

After you have done this once, write down how long it took. That number is your real recovery time, and it is the one to remember when you decide how often to back up.

Screenshot of the WordPress handbook section on the recommended backup and restore order
The handbook's backup and restore order

Step 6 of 9: When an update breaks the site

If a plugin update gives you a white page, the quick move is to turn that one plugin off and look again.

wp plugin deactivate name-of-the-plugin

That does not delete it and it does not touch your data. You can switch it on again after you have read what the plugin changed. If a broken site locks you out of the admin, WP-CLI still works, because it runs outside the web server. That is the main reason I prefer it for updates.

If switching one plugin off is not enough, you have your backup set from step 2 and 3, and you know it restores. Restoring means files back first, then wp db import on the live database. Before you import over a live site, make one more dump of the broken state with wp db export. It takes ten seconds.

Step 7 of 9: Automate the weekly dump with cron

The handbook suggests a backup once a week for a small site and daily for a busy one. Cron does that for you. wp db export - writes to standard output when you give it a dash, so the dump can go straight into gzip without a temporary file:

# crontab -e: every Sunday at 03:15
15 3 * * 0 /usr/local/bin/wp --path=/path/to/your/site db export - | gzip > /home/you/backups/weekly-$(date +\%F).sql.gz

Two details that bite. Cron has a minimal PATH, so use the full path to wp (find it with which wp). And a literal % means something special in a crontab, so it has to be written as \%, as above.

Old dumps pile up. The second line below removes weekly dumps older than 35 days. Run the first line before it to see what it would remove, and keep -delete as the last argument:

find /home/you/backups -name 'weekly-*.sql.gz' -mtime +35 -print
find /home/you/backups -name 'weekly-*.sql.gz' -mtime +35 -delete

Then do what the handbook says about automation: check it by hand once in a while. Look at the size of the newest file and run gzip -t on it. A dump that is suddenly a few bytes means the export failed and nobody told you. The copy off the server has to be automated too, or the weekly dump just sits next to the site.

Step 8 of 9: After the update: has anything changed that should not have?

A backup is for when things break. A checksum check is for when something has been changed without you. WP-CLI can compare your core files and your plugins to the official copies on WordPress.org:

wp core verify-checksums
wp plugin verify-checksums --all

Both commands list files that differ or that should not be there. Expect some noise: a plugin that is not on WordPress.org, a premium plugin for example, cannot be checked. But a PHP file in the uploads folder or a modified core file is a reason to stop and look. Both commands are good habits on their own, which is why they come back in the tips linked below.

Step 9 of 9: How often, and how many

The handbook says weekly for a small site and daily for a busy one, always before an upgrade, and keep three to five recent copies in different places. I’d set the cron job for the weekly dump and keep the manual routine for update days. Keep the last few and delete the older ones, so the folder does not slowly eat the disk.

My own rule is boring: no update without a fresh dump and a copy off the server. I’d rather type a few commands too many than explain to myself later why I skipped them.

Cheat sheet with the six steps of a WordPress update day and the commands for each
The whole routine on one screen

Further reading

Link