Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Stephen-Cronin
Last active July 16, 2017 05:53
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/a3346a866a125891bf7611ef017ff824 to your computer and use it in GitHub Desktop.
Save Stephen-Cronin/a3346a866a125891bf7611ef017ff824 to your computer and use it in GitHub Desktop.
Bash script to reset a WordPress database
#!/bin/bash
# Bash script to reset a WordPress database once you've messed the data up
# Does not delete themes, plugins, or media, just restores WordPress to an emtpy system, activate plugins, imports data, etc
# Requires wp cli
# Login details. EDIT THESE AS APPROPRIATE.
ADMIN_USER="admin"
ADMIN_PASSWORD="password"
ADMIN_EMAIL="person@domain.com"
# Space separated list of plugins that will be activated if the `--plugins=reset` attribute is used. EDIT THIS TO INCLUDE THE PLUGINS YOU NEED.
DEFAULT_PLUGIN_LIST="theme-check wordpress-importer sjc-site-functionality query-monitor"
# Needed on my Windows installation. YOU PROBABLY NEED TO REMOVE THIS IF NOT USING WINDOWS.
shopt -s expand_aliases
alias wp="wp.bat"
echo "========== Resetting the database =========="
echo
# Process the command line parameters
for i in "$@"
do
case $i in
--debug)
DEBUG_MODE=1
shift
;;
-t=*|--title=*)
SITE_TITLE="${i#*=}"
shift
;;
--desc=*)
SITE_DESCRIPTION="${i#*=}"
shift
;;
--dataset=*)
if [ "${i#*=}" = "tutd" ] || [ "${i#*=}" = "theme-unit-test-data" ]; then
DATASET="theme-unit-test-data"
elif [ "${i#*=}" = "wc" ] || [ "${i#*=}" = "woocommerce" ]; then
DATASET="woocommerce"
fi
shift
;;
--plugins=*)
if [ "${i#*=}" = "reset" ]; then
PLUGIN_LIST=${DEFAULT_PLUGIN_LIST}
elif [ "${i#*=}" = "none" ]; then
PLUGIN_LIST=""
fi
shift
;;
*) # unknown option
echo 'Correct Usage: resetwp.sh [--title="Site Title"] [--desc="Site Description"] [--dataset="tutd"] [--plugins="reset"]'
echo
echo ' --title (or -t): The site title / blog name'
echo ' - if not specified: the current site title will be kept'
echo
echo ' --desc: The site description'
echo ' - if not specified: the current site description will be kept'
echo
echo ' --dataset: The data to be loaded'
echo ' - if not specified: no data is loaded'
echo ' - "theme-unit-test-data" or "tutd": loads the wp.org Theme Unit Test Data'
echo ' - "woocommerce" or "wc": loads the official WooCommerce demo data'
echo
echo ' --plugins: The plugins to be activated (must already be installed)'
echo ' - if not specified: the currently active plugins will be set as active'
echo ' - "reset": activates those plugins listed above in DEFAULT_PLUGIN_LIST'
echo ' - "none": no plugins are activated'
echo
exit
;;
esac
done
# If parameters not provided, get the current data (before we reset the DB)
echo "Getting defaults for parameters not provided"
if [ -z "${SITE_TITLE}" ]; then SITE_TITLE=$(wp option get blogname|tr -d '\n'); fi
if [ -z "${SITE_DESCRIPTION}" ]; then SITE_DESCRIPTION=$(wp option get blogdescription|tr -d '\n'); fi
if [ -z "${SITE_URL}" ]; then SITE_URL=$(wp option get siteurl|tr -d '\n'); fi
if [ -z "${PLUGIN_LIST+x}" ]; then PLUGIN_LIST=$(wp plugin list --field=name --status=active | tr '\n' ' '); fi #+x to so it doesn't catch ""
# If debug mode is set, print out what the values are and exit
if [ "${DEBUG_MODE}" = 1 ]; then
echo "- SITE_TITLE = ${SITE_TITLE}"
echo "- SITE_DESCRIPTION = ${SITE_DESCRIPTION}"
echo "- SITE_URL = ${SITE_URL}"
echo "- PLUGIN_LIST = ${PLUGIN_LIST}"
echo "- DATASET = ${DATASET}"
exit
fi
# Reset the DB and install it again
wp db reset --yes
wp core install --url="${SITE_URL}" --title="${SITE_TITLE}" --admin_user="${ADMIN_USER}" --admin_password="${ADMIN_PASSWORD}" --admin_email="${ADMIN_EMAIL}"
wp core update
# Activate the plugins desired (presumes they are already installed)
if [ ! "${PLUGIN_LIST}" = "" ]; then
wp plugin activate $PLUGIN_LIST #no quote marks for this one, as the commend needs to be run without quotes, ie space separated
fi
# Set some options.
wp option update blogdescription "$SITE_DESCRIPTION"
wp option update posts_per_page 5
wp option update thread_comments 1
wp option update thread_comments_depth 3
wp option update page_comments 1
wp option update comments_per_page 5
wp option update large_size_w ''
wp option update large_size_h ''
# Install a dataset (if one was specified in the parameters).
if [ "${DATASET}" = "theme-unit-test-data" ]; then
curl -O https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml
wp import ./theme-unit-test-data.xml --authors=create
rm theme-unit-test-data.xml
wp media regenerate --yes
elif [ "${DATASET}" = "woocommerce" ]; then
wp plugin activate woocommerce #assume this is needed given we have WooCommerce data being loaded
wp theme activate storefront #likewise
curl -O https://plugins.svn.wordpress.org/woocommerce/tags/3.0.0/dummy-data/dummy-data.xml
wp import ./dummy-data.xml --authors=create
rm dummy-data.xml
wp media regenerate --yes
echo "PLEASE RUN THE WOOCOMMERCE SETUP WIZARD IN WORDPRESS" #maybe one day see if I can do this via wp/wc cli
fi
# Set the permalink structure.
wp rewrite structure '%year%/%monthnum%/%postname%' --hard
wp rewrite flush --hard # needed for windows as per https://github.com/wp-cli/wp-cli/issues/1981
@Stephen-Cronin
Copy link
Author

Usage: resetwp.sh [--title="Site Title"] [--desc="Site Description"] [--dataset="tutd"] [--plugins="reset"]

 --title (or -t): The site title / blog name
   - if not specified: the current site title will be kept

 --desc: The site description
   - if not specified: the current site description will be kept

 --dataset: The data to be loaded
   - if not specified: no data is loaded
   - "theme-unit-test-data" or "tutd": loads the wp.org Theme Unit Test Data
   - "woocommerce" or "wc": loads the official WooCommerce demo data

 --plugins: The plugins to be activated (must already be installed)
   - if not specified: the currently active plugins will be set as active
   - "reset": activates those plugins listed above in DEFAULT_PLUGIN_LIST
   - "none": no plugins are activated

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