20160427 using wp cli in moving wordpress sites - plembo/onemoretech GitHub Wiki

title: Using wp-cli in moving WordPress sites link: https://onemoretech.wordpress.com/?p=11170 author: phil2nc description: post_id: 11170 created: 2016/04/27 15:28:56 created_gmt: 2016/04/27 19:28:56 comment_status: closed post_name: using-wp-cli-in-moving-wordpress-sites status: private post_type: post

Using wp-cli in moving WordPress sites

Sven Hoffman has written a good, solid tutorial on How to migrate a WordPress multisite with WP-CLI over at WPKrauts. Yeah, I didn't know that wp-cli included a database search and replace function either.

This command will searches through all rows in a selection of tables and replaces appearances of the first string with the second string. By default, the command uses tables registered to the $wpdb object. On multisite, this will just be the tables for the current site unless --network is specified. Search/replace intelligently handles PHP serialized data, and does not change primary key values.

Pretty much puts Interconnect IT's DB Search and Replace Tool out of business. Thanks for the memories! Sven's article covers all the bases, but in the interests of preserving them for posterity here are the essentials:

1. Export the existing database

wp-cli db export - | gzip > ~/tmp/wpdb.sql.gz

Note that when I install wp-cli the script gets named "wp-cli" rather than the (often) recommended "wp". That's just a matter of personal preference, and an effort give less well-informed admins something to Google if they want to figure out what it does.

2. Import the database into the target.

wp-cli db import wpdb.sql

3. Test search and replace of domain values.

wp-cli search-replace dev.example.com prod.example.com --network --dry-run

4. Search and replace domain values.

wp-cli search-replace dev.example.com prod.example.com --network

In light of what I wrote in my earlier post on WordPress Migration to a New Server, I would of course search and replace on a couple of other values: primarily the file system path to the WordPress installation directory (like "/var/www/html/devsite" to "/var/www/html/prodsite") and the hostname of the machine where things will get uploaded by the dashboard (e.g. "web1.example.com"). The latter value gets set when you do any kind of update for the first time and if not changed will cause future updates on the new server to fail.

Copyright 2004-2019 Phil Lembo