Skip to content

Instantly share code, notes, and snippets.

@thegbomb
Forked from webbj74/drupal-quick-dump.sh
Last active March 26, 2024 15:17
Show Gist options
  • Save thegbomb/b7b528d3a0ecdcc99557e995063f5d38 to your computer and use it in GitHub Desktop.
Save thegbomb/b7b528d3a0ecdcc99557e995063f5d38 to your computer and use it in GitHub Desktop.
Script to dump Drupal database structure, but exclude data from massive/unneeded tables.
#!/bin/bash
# usage: drupal-quick-dump user host database D8 ("D8" is optional)
# remember to chmod u+x drupal-quick-dump.sh
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
echo "Starting to dump the table structure."
TABLES=$(mysql --skip-column-names -e 'show tables' -u ${USER} --password="${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 "Dumping the table structure."
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 "Starting to dump the table data."
if [ "$4" == "D8" ]
then
echo "Dumping D8 tables."
TABLES2=$(echo "$TABLES" | grep -Ev "^(cache.*|watchdog)$")
else
echo "Dumping D7 tables."
TABLES2=$(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} ${TABLES2} >> "${DB}.${DATE}".sql
echo "Starting to gzip dump."
gzip -v "${DB}.${DATE}".sql
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment