Skip to content

Instantly share code, notes, and snippets.

@davemac
Last active July 10, 2021 03:42
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemac/8890e1fd7bffa79862f9234ad963aa35 to your computer and use it in GitHub Desktop.
Save davemac/8890e1fd7bffa79862f9234ad963aa35 to your computer and use it in GitHub Desktop.
bash alias using WP-CLI to pull a remote WP database into a local site
# pull a production WP database to an existing local site
# uses current directory as theme path and ssh alias
pullprod() {
START=$(date +%s)
# get current directory name, used for database and URL
# TODO: use echo get_template_directory() and get characters from right to first /
current=${PWD##*/}
cd ~/Sites/$current
# make a backup of the current local database
wp db export _db.sql
wp db reset --yes
# connect to remote site and export the remote database down to our local directory
wp @prod db export - > $current.sql
echo "rsync of remote database to $current directory complete."
wp db import
# database is now imported so delete it
rm -rf $current.sql
# update everything
wp plugin update --all
wp theme update --all
wp core update
# deactivate plugins that aren't needed
wp plugin deactivate google-universal-analytics worker wp-rocket
# activate plugins used for development
wp plugin activate debug-bar query-monitor acf-theme-code-pro
# get the remote site URL, remove the http:// for our search replace
production_url=$(wp @prod eval '$full_url=get_site_url();$trimmed_url=str_replace("https://", "", $full_url); echo $trimmed_url;')
wp search-replace "$production_url" "$current.localhost" --all-tables-with-prefix
# Discourage search engines from indexing this site
wp option update blog_public 0
# udpate local admin user for easy access
dmcweb
END=$(date +%s)
DIFF=$(( $END - $START ))
echo -e "\n$production_url database now in use on $current.localhost site.\nIt took $DIFF seconds, enjoy!\n"
}
Copy link

ghost commented Aug 13, 2017

Great stuff here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment