When doing custom WordPress theme and plugin development for a site that’s already launched, I find it is essential to get my local development environment set up as close as possible to the live environment where the site is hosted. This minimizes headaches and unexpected problems that come when making any updated code live.
Here are the steps I use to clone the database, theme, plugin and media/uploads from a site hosted on WP Engine into a locally-hosted development environment powered by VVV. This method allows me to develop and test new functionality against recent site content before I deploy it to a staging environment for final testing.
The usual warning applies: you should make sure you know what each of these steps is actually doing and adjust them to fit your specific setup. Some of these actions could result in data loss, so please use them at your own risk.
Okay, here we go:
- Make sure any custom theme and plugin code you’ll be working with is under source control, probably using these instructions from WP Engine, and up to date locally. Make sure that those directories are not accidentally included in the
.gitignorefile for your repo. (I handle this by default ignoring everything in, say, the plugins directory with
/wp-content/plugins/*and then adding exclusion rules for each directory I’m working on, e.g.
- Initiate a backup checkpoint on the environment you want to clone. If you’re okay using the slightly out of date one that WP Engine automatically makes every morning, that’s fine.
- Initiate a download of the backup checkpoint, choosing the “partial” option and checking “Entire database,” “Plugins” and “Themes.” Leave “Media uploads” and “Everything else” unchecked.
- Use the WP Engine SSH gateway to rsync the media folder from the WP Engine environment to your local environment. Here’s an example command for a “mystaging” environment…you will want to try your version with the dry run
-nflag first to make sure it is going to do what you want!
rsync -n -rlD -vz --size-only --delete mystaging:sites/mystaging/wp-content/uploads/ /Users/chris/vvv/www/mysite/public_html/wp-content/uploads/
This should delete any local media files not in the WP Engine environment, and copy everything else down that isn’t already there. It could take a while if it’s the first time you’re running it, but subsequent runs should only grab new uploads to the site.
- Unzip the downloaded backup checkpoint on your local system.
- Move the
mysql.sqlfile into a directory that will be accessible from your VVV Vagrant shell:
mv ~/Downloads/wp-content/mysql.sql /Users/chris/vvv/www/mysite/
- Update your local plugins directory from the downloaded backup. Again, please use the
-nflag with your version of this command first to preview the results:
rsync -rlD -vz --size-only --delete --exclude=query-monitor ~/Downloads/wp-content/plugins/ /Users/chris/vvv/www/mysite/public_html/wp-content/plugins/
Note that this command also excludes removing the
query-monitorplugin, which I have installed locally but not in the WP Engine environment. That way it can stay ready to activate without reinstalling.
- Repeat the above step for the themes directory if need be.
- Resolve any discrepancies, things git notes as changes that need to be committed, missing files, etc. There shouldn’t be any but it’s worth checking.
- SSH into your VVV box:
- Make sure your WP dev site and the WP Engine site are running the same version of WordPress core.
- Go to the directory where the site lives, e.g.
- Clear out the existing database (again, assuming you don’t have any locally staged changed that you would care about):
wp db reset.
- Import the database from the WP Engine environment:
wp db import ../mysql.sql
- Update any content or configuration references to the site’s hostname, noting that you may want to run this with the
wp search-replace '//mystaging.com' '//mysite.test' --precise --recurse-objects --all-tables --skip-columns=guid.
- If you need to put any of your plugins in local development mode, now’s the time, e.g.
wp config set JETPACK_DEV_DEBUG true --raw. And, if you were using a local development plugin like Query Monitor, reactivate it:
wp plugin install query-monitor --activate
That’s it! In only 16 steps you should have a fully up to date copy of your site’s data available for local development and testing. Obviously much of the above could be scripted (with appropriate safeguards) to save time and reduce human error in the process.
I hope you find this helpful. If you have improvements to suggest or your own fun methods of cloning environments, please share in the comments.