Last active
March 27, 2018 08:14
-
-
Save davemac/ef0c7d576a5a5ad5119fdd8018d8885d to your computer and use it in GitHub Desktop.
bash script to pull a database from a staging server
This file contains hidden or 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 staging WP database to an existing local site | |
# uses current directory as theme path and ssh alias | |
pullstage() { | |
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 @stage 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 | |
# 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 | |
pull_staging_url=$(wp @stage eval '$full_url=get_site_url();$trimmed_url=str_replace("https://", "", $full_url); echo $trimmed_url;') | |
wp search-replace "$pull_staging_url" "$current.localhost" --all-tables-with-prefix | |
# udpate local admin user for easy access | |
# dmcweb | |
END=$(date +%s) | |
DIFF=$(( $END - $START )) | |
echo -e "\n$pull_staging_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