Migrating A WordPress Site With wp-cli

El Chorro Margarita

This is … crazy simple. I wanted to move a site from ipstenu.org to ipstenu.com (yes, I own that too). While ipstenu.org is a Multisite network, ipstenu.com is where I put a ton of add-on domains. I was moving a site over and, as it was WordPress, did it in a matter of minutes.

Add the domain to the … domain

Since I don’t care to have multiple hosting accounts, and I’m the only one with SSH/FTP access, it’s safe enough for me to do this. There is a risk when you share multiple domains in one hosting account, that if one gets hacked they’re all vulnerable, but I consider it low in my situation. Every plugin is vetted, every file is checked, and then I went and gave each add-on it’s own FTP account. Neurotic? Thy name is me.

Anyway, I add the new domain to my hosting where I want it.

Create the new DB

I have to make a new database, and generally a new DB user, on the server too.

Export!

On existing hosting, I do this:

wp db export

That gives me my SQL file, thanks to WP-CLI. It’ll be named example_com.sql and will sit in my folder with .htaccess and everything else.

Copy!

I do it via SSH. I go to the new location and run this:

scp -r olduser@example.com:path/to/files/ .

Since I have ssh keys set up, it’s easy. If I don’t, I’ll put in the password, but that’s straightforward.

Edit wp-config.php

Now I have to point to the new DB. Sometimes I name it the same, but usually I don’t, so I’ll edit the DB name, DB user, and password.

Import the old DB

Ready?

wp db import example_com.sql

Boom. It’s all dumped in! Only two steps left!

Search & Replace

I love this one.

wp search-replace example.com newexample.com --dry-run

I ALWAYS dryrun test it. This is a serialization safe search, so rarely is it ever going to be an issue to just run, but it lets me make sure I don’t get any wonky results. I never have, so re-run without dry-run.

Cleanup!

Delete the SQL file, delete the old files on the old server.

Drink

El Chorro Margarita

I moved code. That’s shipping, right? Or is it margaritas are for migrations?

8 responses to “Migrating A WordPress Site With wp-cli”

  1. Japh Avatar

    What funny timing! I did a presentation on something similar just last month at the WordPress Melbourne developer meet up.

    I also packaged up and compressed the files and database export to make the transfer quicker too.

    1. Ipstenu (Mika Epstein) Avatar

      @Japh: Oh yes, do that if you have a lot of files 😀

      It was a fairly small site, but if it’s a big one, I’ll zip it before moving, because watching rsync copy each file over is about as fun as cleaning cat poop at 1am.

  2. Glenn Avatar
    Glenn

    Margaritas are good but I prefer Caipirinha’s 🙂

  3. Deepak Kundu Avatar

    Mika,

    WP-CLI is a very powerful tool. Really good demonstration of the same in your post. I wonder how many are still using the ftp download and upload way for website migrations.

    I also did a recent post on how to install WP-CLI on your server. Here is a link for the readers.

    Regards,
    Deepak Kundu

  4. Glenn Avatar
    Glenn

    Do you need to have wp-cli installed on your hosting server to do this? or can wp-cli run locally and ‘point’ at a site on the hosting server?

    1. Ipstenu (Mika Epstein) Avatar

      @Glenn: You’d need to have WP-cli run wherever you have WP. So if you develop locally, it’d be faster to have WP-cli, though in a pinch you could FTP up the files and just run it remotely.

      Since I’m using VVV these days, WP-cli is built in, and I always install it in my servers. Makes my life easier.

    2. Glenn Avatar
      Glenn

      @Ipstenu (Mika Epstein): ty 🙂 and there is something new I learned … VVV sounds great, just reading about it on the GitHub site