Skip to content

Instantly share code, notes, and snippets.

@and1truong
Last active October 10, 2015 07:27
Show Gist options
  • Save and1truong/3654728 to your computer and use it in GitHub Desktop.
Save and1truong/3654728 to your computer and use it in GitHub Desktop.
# Ref: http://drush.ws/examples/example.drushrc.php
# Ref: http://books.tag1consulting.com/scalability/drupal/start/backups
echo 'Including config file…'
if [ -f ./.config ]; then
. ./.config
fi
echo 'Getting database connection settings…'
CMD='foreach ($GLOBALS['"'"'databases'"'"']['"'"'default'"'"'] as $key => $info)';
CMD=$CMD' $dbs[] = "{$info['"'"'driver'"'"']}://{$info['"'"'username'"'"']}:{$info['"'"'password'"'"']}@{$info['"'"'host'"'"']}/{$info['"'"'database'"'"']}";'
CMD=$CMD' print implode(" ", $dbs);'
DBS=`drush eval "$CMD"`
for DB in $DBS
do
NAME=${DB#*@}
NAME=${NAME#*/}
echo 'Getting table lists for table {'$NAME'}…'
TABLES=`drush sql-query 'SHOW TABLES'`
for TABLE in $TABLES
do
if [[ ! "$TABLE" =~ "cache_" && ! "$TABLE" =~ "Tables_in_" && ! "$TABLE" =~ "search_" && $TABLE != 'watchdog' && $TABLE != "history" ]]
then
LIST[${#LIST[@]}]=$TABLE
fi
done
LIST=$(printf ",%s" "${LIST[@]}")
LIST=${LIST:1}
FILE=$BACKUP_DB_DIR'/'$NAME'.sql'
drush sql-dump --db-url=$DB --tables-list=$LIST > $FILE
# TODO: Git commit
done
read -p "Are you sure? " -n 1
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
LIST=(a b c)
LIST=$(printf ",%s" "${LIST[@]}")
LIST=${LIST:1}
DB='mysql://user:pass@host/db:port'
NAME=${DB#*@}
NAME=${NAME#*/}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment