Skip to content

Instantly share code, notes, and snippets.

@otsuarez
Created January 3, 2021 16:47
Show Gist options
  • Save otsuarez/724be71b734c2b9f6f7e71411492066e to your computer and use it in GitHub Desktop.
Save otsuarez/724be71b734c2b9f6f7e71411492066e to your computer and use it in GitHub Desktop.
drupal db backup script
#!/bin/bash
# usage: drupal-quick-dump user host database
usage() {
echo -e "usage:"
echo -e "\tdrupal-db-backup [dir]"
echo -e "params:"
echo -e "\tdir\tdirectory for the settings.php file"
}
if [ "$#" -lt 1 ]; then
usage
exit
fi
DIR="$1"
# DIR=/var/www/html/sites/default
DB=$(grep -F "'database' => '" "${DIR}/settings.php" | cut -f4 -d\')
USER=$(grep -F "'username' => '" "${DIR}/settings.php" | cut -f4 -d\')
PASS=$(grep -F "'password' => '" "${DIR}/settings.php" | cut -f4 -d\')
HOST=$(grep -F "'host' => '" "${DIR}/settings.php" | cut -f4 -d\')
# echo "db: ${DB} user: ${USER} pass: ${PASS} host: ${HOST}"
# exit 0
DATE=`date +%Y%m%d%H%M`
# moving to temp dir
pushd /var/tmp/ 2>&1 >/dev/null
# Dump Structure
echo "Starting to dump the table structure."
TABLES=`mysql --skip-column-names -e 'show tables' -u ${USER} -p${PASS} -h ${HOST} ${DB}`
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."
TABLES2=`echo "$TABLES" | grep -Ev "^(accesslog|cache.*|flood|search_.*|semaphore|sessions|watchdog)$"`
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
popd 2>&1 >/dev/null
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment