WP Engine has a built-in staging site which is awesome, however sometimes troubleshooting requires more direct access. For those times, I like to take a WP Engine backup snapshot and restore locally on my Macbook Pro. For local development I manually configure my stack (apache, mysql, php, etc) following this blog post by Grav. One additional prerequisite is having WP-CLI installed. With that I can automate a restore using the following script.
To prevent having to type in common things like local database user and password I decided to define them in a new file called .bash_anchorhost
. I put this file under my home directory.
vhostsfile="/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf"
sites_path="/Users/austinginder/Sites/"
db_user="root"
db_password="************"
Next download a WP Engine backup snapshot and run the following script, inputting the snapshot url, local database name, production site url and local site url.
## Pull in static configs
source ~/.bash_anchorhost
function wpengine_local_restore {
## Prompt for dynamic configs
read -p $'\e[31mWP Engine snapshot url\e[0m: ' wpengine_snapshot
read -p $'\e[31mLocal database name (will create)\e[0m: ' db_name
read -p $'\e[31mProduction site url ( ex: anchor.host )\e[0m: ' siteurl_production
read -p $'\e[31mLocal site url ( ex: anchor.dev )\e[0m: ' siteurl_local
## Download and extract snapshot
cd $sites_path
rm -r $siteurl_local
mkdir $siteurl_local
cd $siteurl_local
wget $wpengine_snapshot
file_count=`ls -1 | wc -l`
if [ $file_count -eq 1 ]
then
downloaded_zip=`ls`
unzip $downloaded_zip
rm -f $downloaded_zip
fi
## WP Engine cleanup
rm -rf wp-content/mu-plugins
rm -f wp-content/advanced-cache.php
rm -f wp-content/object-cache.php
## Database Prep
mysql -u $db_user -p$db_password -e "create database $db_name;"
mysql -u $db_user -p$db_password $db_name < wp-content/mysql.sql
## Loads tables prefix
db_prefix=$(cat wp-config.php | grep "\$table_prefix" | cut -d \' -f 2)
## Generates new wp-config.php
wp config create --dbname=$db_name --dbuser=$db_user --dbpass=$db_password --dbprefix=$db_prefix --dbhost=127.0.0.1 --force --extra-php <<PHP
define( 'JETPACK_DEV_DEBUG', true );
PHP
## Updates urls
wp search-replace https://www.$siteurl_production http://$siteurl_local --all-tables
wp search-replace http://www.$siteurl_production http://$siteurl_local --all-tables
wp search-replace https://$siteurl_production http://$siteurl_local --all-tables
wp search-replace http://$siteurl_production http://$siteurl_local --all-tables
## Adds to hosts file
echo -e "127.0.0.1\t${siteurl_local}" | sudo tee -a /etc/hosts
## Add new configs to apache virtual server
vhostspath=`pwd`
echo "<VirtualHost *:80>" >> $vhostsfile
echo -e "\tDocumentRoot \"${vhostspath}\"" >> $vhostsfile
echo -e "\tServerName ${siteurl_local}" >> $vhostsfile
echo '</VirtualHost>' >> $vhostsfile
# Restarts apache
sudo apachectl -k restart
## Launch in browser
open http://$siteurl_local
}
wpengine_local_restore
Here is an example of what it looks like in action.
