Created
November 6, 2015 04:35
-
-
Save cworks/0c0e28184e7c1c7013a5 to your computer and use it in GitHub Desktop.
MySQL backup script
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
#!/usr/bin/env 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" | |
} | |
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