Using WP-CLI for database synchronisation

There are a great many tools that duplicate sites and synchronise databases between sites. Some favourites are Duplicator, WP Migrate and WP Migrate Pro, as well as good old phpMyAdmin. However, sometimes all that is needed is to import the database and then do a search and replace. This is easier said than done however as WordPress uses serialised data, meaning the url isn’t a simple string, rather an array represented in the database like so:


This is the sort of use case where WP-CLI shines. One command in particular, wp search-replace, can be a great timesaver. Here are some quick instructions for using wp-cli to perform this oft-required task.

Please note that some of these commands are destructive in nature so make sure you have backups or are at least comfortable with the idea of losing your local database before proceeding. I am assuming that you also have a basic knowledge of wp-cli and have it installed. If not, this article might be helpful. I also have a cheatsheet of useful wp-cli commands for setting up a WordPress site.

First export the database on the live site. I usually just use phpMyAdmin here but you can also use wp db export which will export the file as {dbname}.sql. There are also several arguments you can use to modify the way in which the file is output. If you want to go all-command-line it may be possible using wp db import scp /local/dir but that will depend on your server configuration and I find it faster to download it using (s)ftp.

The new database then needs to replace the old. The easiest way to import the database is to add the downloaded database with the name {dbname}.sql to the site root. Delete the old database using wp db reset. Then use wp db import to import the new database.

Next we use our wp search-replace super-command to synchronise the database in the blink of an eye. For this use

wp search-replace mysite.local

If the online site uses SSL (https), don’t forget to do

 wp search-replace '' ''

You should then have a synchronised version of the live site on your local server. Please let me know in the comments if any clarifications can be made to make this article more understandable.

6 comments on “Using WP-CLI for database synchronisation

  1. Are the curly braces supposed to be around my final command line script or is that just a formatting custom you use to make it clear where we need to stick our own variables?

    • No, the curly braces are (were) just to imply that it was a placeholder. I edited it to make it a bit clearer.

  2. This is not a true synchronization. Synchronization implies that there are changes to both databases, and by syncing, the newest changes to both will be written to both. Is there a way to do that?

Leave a Reply

Your email address will not be published. Required fields are marked *

Click to view allowed tags

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.