Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment