Created
September 22, 2019 17:14
-
-
Save rubenvarela/71101aa93d9fc161e91fdc75414d6c88 to your computer and use it in GitHub Desktop.
Drupal/Drush - Dump database from alias to file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## | |
# Print usage instructions | |
## | |
usage() { | |
echo "Usage:" | |
echo " ac-dump.sh @alias" | |
echo " It will copy the database from the alias to a local file named as the alias in gzip format" | |
echo "" | |
echo " ac-dump.sh -h" | |
echo " Display this message." | |
echo "" | |
} | |
## | |
# If database backup failed. We can add more work here | |
## | |
failed_db_dump() { | |
echo "There was an issue with the dump." | |
# try to delete file | |
rm "$1" | |
if [[ $? -eq 0 ]]; then | |
echo "File $1 has been deleted." | |
else | |
echo "Couldn't delete file - $1" | |
fi | |
exit 1; | |
} | |
## | |
# If database export worked, print message | |
## | |
success_db_dump() { | |
echo "Database exported to $1" | |
} | |
## | |
# Export database and check if valid file | |
## | |
dump_db() { | |
#generate name | |
local file_name | |
file_name="$1-$(date '+%F_%H.%M.%S').sql.gz" | |
# remove first character, "@" | |
# man bash, search for "parameter:offset:length" | |
file_name=${file_name:1} | |
#dump database to file | |
drush "$1" sql-dump --gzip > "$file_name" | |
# Check if file is a gzipped file. | |
type=$(file "$file_name") | |
if [[ "$type" != *" gzip compressed data,"* ]]; then | |
failed_db_dump "$file_name" | |
else | |
success_db_dump "$file_name" | |
fi | |
} | |
##################### | |
# Main logic starts here | |
##################### | |
## If there are no arguments, show usage and exit. | |
if [[ $# -eq 0 ]]; then | |
echo "No arguments supplied" | |
usage | |
exit 1 | |
fi | |
## | |
# If there's more than 1 argument | |
# @TODO: should we add support to check and iterate over all? | |
# Something to consider for the future... | |
## | |
if [[ $# -gt 1 ]]; then | |
echo "Too many arguments supplied" | |
usage | |
exit 1 | |
fi | |
## | |
# If there are arguments and they're exactly 1 | |
## | |
case $1 in | |
-h) usage; exit;; | |
--help) usage; exit;; | |
-*) echo "Invalid arguments"; usage; exit 1;; | |
@*) dump_db $1;; | |
*) dump_db "@$1";; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment