Last active
July 10, 2021 03:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great stuff here!