Import a .sql.gz backup with WP-CLI

Import a gzipped database backup with WP-CLI in one line. gunzip unpacks it on the fly and pipes it to wp db import, so no large .sql file lands on the disk.

gunzip < backup.sql.gz | wp db import -

The - tells wp db import to read from the pipe instead of a file.

Save the current database first

The import overwrites every table that is in the backup. Export what you have now, so you can go back:

wp db export before-import.sql

If the domain changed

Replace the old address everywhere. Run it once with --dry-run to see the counts, then for real, then clear the cache:

wp search-replace 'https://old.example.com' 'https://new.example.com' --all-tables --precise --dry-run
wp search-replace 'https://old.example.com' 'https://new.example.com' --all-tables --precise
wp cache flush

Common problems

Unknown collation utf8mb4_0900_ai_ci

The backup came from MySQL 8, and the new server runs MariaDB or an older MySQL. Swap the collation as you import:

gunzip < backup.sql.gz | sed 's/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g' | wp db import -
zcat fails on a Mac

On macOS, zcat looks for .Z files. Use gunzip < backup.sql.gz or gzcat backup.sql.gz instead.

The site shows the install screen after the import

The backup uses a different table prefix. Set $table_prefix in wp-config.php to the prefix in the backup, for example wp_.

Moving one site out of a multisite? Here’s the whole process. A move you can’t afford to break? I can help.

Let’s talk.

Send me your site and what you need. You’ll get a plain answer.

Start a project hello@sunnymorgan.com

828.414.8090