Restore a Fresh Local WordPress Database from Production

Situation: You’ve got your local codebase for a site in place, but the database got wiped, intentionally or otherwise. Maybe you destroyed your Vagrant box.

To quickly pull the production database, I’ll often use a tool like WP Migrate DB Pro. Of course, using that tool (or any other plugin-based database migration tool) depends on the local database being configured, and we don’t have anything.

So to get back going quickly, I like to use WP-CLI to quickly install a database and a placeholder user and activate the plugin so that all I have to do is log in and paste WP Migrate DB’s production connection info. I’ve abstracted that process a bit via a bash function. Feel free to drop this in your local .bash_profile or the equivalent.

function wptempinstall() {
	wp core install --url=$1 --title=Example --admin_user=temp [email protected]
        wp plugin activate wp-migrate-db-pro
}Code language: JavaScript (javascript)

This sets you up to just run wptempinstall from any terminal window. Then you can navigate to your locally configured dev URL, log in with the password that command spits out, paste in WP Migrate DB’s connection info and pull. You’re off to the races.

This assumes you have a config file. If you don’t have that, you might also find this useful if you often have the same, non-default database connection details:

function wpconfig() {
	wp core config --dbuser=homestead --dbpass=secret --dbhost= --dbname=${1}
}Code language: JavaScript (javascript)

This let’s you run wpconfig database_name, also from the terminal window.


Leave a Reply

Your email address will not be published. Required fields are marked *