Create a gist now

Instantly share code, notes, and snippets.

A short bash script that exports all databases into separate gzipped files in a chosen location.
#!/bin/bash
# Example usage
# --
#
# ./db-backup.sh -u username -p password -t /target/
while getopts u:p:t: option
do
case "${option}"
in
u) USER=${OPTARG};;
p) PASSWORD=${OPTARG};;
t) TARGET=${OPTARG};;
esac
done
for D in `mysql -u $USER -p$PASSWORD -N -B -e 'show databases'`;
do
echo "Backing up $D to $TARGET"
mysqldump --skip-comments --compact -u $USER -p$PASSWORD $D | gzip -9 > $TARGET/$D.sql.gz
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment