Skip to content

Instantly share code, notes, and snippets.

@theodorosploumis
Forked from kyleskrinak/drupal-quick-dump.sh
Last active March 22, 2016 13:21
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 theodorosploumis/7c4b2a369d16ac1af379 to your computer and use it in GitHub Desktop.
Save theodorosploumis/7c4b2a369d16ac1af379 to your computer and use it in GitHub Desktop.
Database dump for Drupal. Use "drupal-quick-dump <db_user> <db_host> <database_name> D8"
#!/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.*|session|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}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment