Skip to content

Instantly share code, notes, and snippets.

@navitronic
Last active September 15, 2017 23:45
Show Gist options
  • Save navitronic/10451484 to your computer and use it in GitHub Desktop.
Save navitronic/10451484 to your computer and use it in GitHub Desktop.
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