How to Migrate a WordPress Site with WP-CLI and rsync

There are lots of ways to migrate a WordPress site. Lots of plugins can do it. You can do it over simple (S)FTP. But the quickest way I know of is via rsync and WP-CLI. But that does come with an important proviso: you need to have access to a shell (via a simple local terminal or SSH) to both ends of the migration. And that’s often harder to do than the plugin or SFTP route. But if you’ve got that, this way is a great deal faster.

There are a lot of prerequisites if you’re trying to find this method safe and easy. Mostly, you need to be comfortable with a command line enough that you already have at least a little understanding of what SSH, rsync, and WP-CLI are. And you need those installed and working on both ends of your migration. If you’ve not aleady got that, unfortunately this Quick Guide is too quick to detail those for you.

If you do already have that set up, then this video should do you:

And if you prefer text, here are the details:

Moving a Site From Remote to Local With rsync and WP-CLI

  1. Connect to the remote site via SSH. This will probably look something like ssh [email protected] if you have SSH keys set up.
  2. On the remote (sending) server, move to the location on the filesystem of your WordPress site. This will vary, but something like cd public_html/sitename is what I’d expect you’d need.
  3. On the remote server, dump the database using WP-CLI. That’ll look something like wp db export db.sql.
  4. Now, on the local server, navigate to the folder you want your WordPress site to be in.
  5. Then you’ll start pulling in the site using rsync. rsync is a utility which (typically) uses SSH to move files between two computers. That’s what you’ll need. The basic command looks something like rsync -avz [email protected]:/home/username/public_html/sitename/* .. (If this local version is a valid WordPress install already, as was the case in the video, you’ll probably want to --exclude wp-config.php as well, which would make the command look like rsync -avz --exclude wp-config.php [email protected]:/home/username/public_html/sitename/* ..
  6. Once rsync has completed (depending on the age and media-library size of your site, it can take some time) you’ll need to finish the migration by loading up the database that you dumped on the remote. Using WP-CLI, that looks like wp db import db.sql.
  7. Finally, you’ll probably need to do a search and replace of the remote URL to the local one. If that’s the case, you’ll do something like, wp search-replace http://www.example.com http://localhost/localname. That should run pretty quick, and then you’ll be set.

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Shane
March 7, 2019 9:14 am

Well, I am trying to migrate from one server to another using SSH command line. I have gotten to the point where I created the zipped folder of my existing file following this tutorial https://www.cloudways.com/blog/migrate-wordpress-via-ssh-and-wp-cli/ but I am unable to import the exported sql file. I can export it but cannot. What is the method to do that?? Thank you!

Henry Vongsavath
June 27, 2018 4:01 pm

How would you do this reversed? From local to say a test site on a subdomain like dev.mydomain.com. In short how would I upload my files to my host using wp cli?

mc0e
March 22, 2018 10:54 pm

Two caveats, which seem quite serious:

* If you’ve got your domain name inside serialised data, and your new domain name has a different length, then this simple-minded search and replace will break that serialised data.
* You may also have file paths embedded in your database which need fixing.

Varun Pangotra
July 11, 2017 8:15 pm

Is it possible to migrate the website from one server to another directly without downloading the files on local system?

Piet
July 10, 2017 9:34 am

I would like to suggest to run the search and replace command twice, once on the URL minus the http: or https: part and one more time on the path.

Colin Mitchell
July 6, 2017 2:23 pm

Great article David. I’ve done custom WordPress development for years, but haven’t gotten into using WP-CLI into my workflow yet. Generally I stick to standard mysql commands for importing/exporting databases. I especially liked the search-replace command for a final step when pushing a site live though.

It’s ideal to always have the correct base urls set in the database, but for local, it can easiest to simply include this script in your wp-config.php to solve that problem. Or at least it prohibits issues with SSL and other things.

“`
$domain = $_SERVER[ ‘SERVER_NAME’ ];
define( ‘WP_HOME’, ‘http://’ . $domain );
define( ‘WP_SITEURL’, ‘http://’ . $domain );
“`