Skip to content

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 username@domain.com 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 username@domain.com:/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 username@domain.com:/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.
David Hayes

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
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?

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

25 Best Free WordPress Themes (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...