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
- Connect to the remote site via SSH. This will probably look something like
ssh firstname.lastname@example.org you have SSH keys set up.
- 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/sitenameis what I’d expect you’d need.
- On the remote server, dump the database using WP-CLI. That’ll look something like
wp db export db.sql.
- Now, on the local server, navigate to the folder you want your WordPress site to be in.
- 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@example.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.phpas well, which would make the command look like
rsync -avz --exclude wp-config.php firstname.lastname@example.org:/home/username/public_html/sitename/* ..
- 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.
- 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.