Skip to content

Instantly share code, notes, and snippets.

@Stephen-Cronin
Created July 16, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stephen-Cronin/54089254111aa3c98e812e7bc36a7926 to your computer and use it in GitHub Desktop.
Save Stephen-Cronin/54089254111aa3c98e812e7bc36a7926 to your computer and use it in GitHub Desktop.
Bash Script to change database using wp-cli
#!/bin/bash
# Bash Script to change database using wp-cli
# Finds any previously exported .sql file and asks the user which one to change to (ie import)
# Recommended usage: Change your system to multiple ideal states and export the db for each (into main wp folder), then use this to switch between states.
# Example: I have db exports for empty installation, theme unit test data, woocommerce demo data, etc.
# Requires wp cli
# Needed on my Windows installation. YOU PROBABLY NEED TO REMOVE THIS IF NOT USING WINDOWS.
shopt -s expand_aliases
alias wp="wp.bat"
# Read the sql files in this folder and prompt user which one to restore
echo ""
echo "*****************************2"
echo ""
echo "Select database to restore"
echo ""
select opt in *.sql;
do
echo
if [ "${opt}" = "" ]; then
echo "ERROR: Please enter one of the numbers presented"
echo
exit
fi
case $opt in
$opt)
echo "Dropping tables ..."
wp db reset --yes #
echo "Restoring $opt ..."
wp db import $opt #
echo "Updating database"
wp core update-db #
break
;;
esac
done
@Stephen-Cronin
Copy link
Author

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