Created
December 24, 2015 05:46
-
-
Save cworks/5071fade79099cb9b056 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
usage() { | |
echo "----------------------------------------------------------------" | |
echo "`basename "$0"` -u [username] -p [password] -d [database] -t [target dir]" | |
echo "----------------------------------------------------------------" | |
echo " -u, required, database username" | |
echo " -p, required, database password" | |
echo " -d, required, database name" | |
echo " -t, optional, directory to place backup file" | |
echo " -z, optional, zip the backup file" | |
echo " -a, optional, append datetime to backup file" | |
} | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
case $key in | |
-u|--username) | |
USERNAME="$2" | |
shift # past argument | |
;; | |
-p|--password) | |
PASSWORD="$2" | |
shift # past argument | |
;; | |
-d|--db) | |
DATABASE="$2" | |
shift # past argument | |
;; | |
-t|--target) | |
TARGET="$2" | |
shift # past argument | |
;; | |
--default) | |
DEFAULT=YES | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] || [ -z "$DATABASE" ]; then | |
usage; | |
exit 1; | |
fi | |
if [ -z "$TARGET" ]; then | |
TARGET=. | |
fi | |
BACKUP_FILE="${DATABASE}.backup.sql" | |
mysqldump -u ${USERNAME} --password=${PASSWORD} ${DATABASE} > ${TARGET}/${BACKUP_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment