Skip to content

Instantly share code, notes, and snippets.

@kyleskrinak
Forked from timmillwood/drupal-quick-dump.sh
Last active March 22, 2016 12:12
Show Gist options
  • Save kyleskrinak/8175724 to your computer and use it in GitHub Desktop.
Save kyleskrinak/8175724 to your computer and use it in GitHub Desktop.
Adding hours/mins to the date variable, as I may run this many times during theme development
#!/bin/bash
# usage: drupal-quick-dump user host database D8 ("D8" is optional)
# remember to chmod u+x drupal-quick-dump.sh
BOLD="$(tput bold)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
MAG="$(tput setaf 5)"
RESET="$(tput sgr0)"
USER="$1"
HOST="$2"
DB="$3"
# the fourth parameter might include "D8" as a compatibility flag
DATE=$(date +%Y%m%d-%H%M)
# trap read debug
if [ -z "$1" ]
then
echo "Please provide the database user, host and database name, after the command. Add \"D8\" for a compatible dumpfile."
for f in /var/www/drupal/sites/*/settings*; do
if [ -e "$f" ]
then
# print out connection string info for compatible deployments
grep -E "^ *('username'|'password'|'database'|'host')" /var/www/drupal/sites/*/settings*
break
fi
done
exit 1
fi
# Get User Password
echo "Please provide the password for ${USER} on db ${DB} hosted at ${HOST}:"
read -rse PASS
# Dump Structure
TABLES=$(mysql --skip-column-names -e 'show tables' -u ${USER} -p${PASS} -h ${HOST} ${DB})
# test if connection was unsuccessful. If so, bail.
if [[ ${TABLES} == 0 ]]
then
echo "bad mysql connection info"
exit 1
fi
# Continue of connection retrieved data, here, the schema
echo "${RED}Starting to dump the table structure.${RESET}"
mysqldump --complete-insert --disable-keys --single-transaction --no-data -u ${USER} --password=${PASS} -h ${HOST} ${DB} ${TABLES} > "${DB}.${DATE}".sql
# Dump Data, Excluding Certain Tables
echo "${MAG}Starting to dump the table data.${RESET}"
if [ "$4" == "D8" ]
then
echo "${RED}Dumping D8 tables.${RESET}"
XTABLES=$(echo "$TABLES" | grep -Ev "^(cache.*|watchdog)$")
else
echo "${RED}Dumping D7 tables.${RESET}"
XTABLES=$(echo "$TABLES" | grep -Ev "^(accesslog|cache.*|flood|search_.*|semaphore|sessions|feeds_log|watchdog)$")
fi
mysqldump --complete-insert --disable-keys --single-transaction --no-create-info -u ${USER} --password=${PASS} -h ${HOST} ${DB} ${XTABLES} >> "${DB}.${DATE}".sql
echo "${MAG}Starting to gzip dump.${RESET}"
echo "${GREEN}"
gzip -v "${DB}.${DATE}".sql
echo "${RESET}"
echo "${BOLD}Done!${RESET}"
@kyleskrinak
Copy link
Author

Removing the porcine "feeds_log" table

@kyleskrinak
Copy link
Author

Print connection string info

@kyleskrinak
Copy link
Author

Note: this is incompatible with zsh. Run with Bash.

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