Skip to content

Instantly share code, notes, and snippets.

@bosko
Created April 30, 2016 22:19
Show Gist options
  • Save bosko/0f1c74b71163ef87e2e60d70b3713e4d to your computer and use it in GitHub Desktop.
Save bosko/0f1c74b71163ef87e2e60d70b3713e4d to your computer and use it in GitHub Desktop.
Back up all MySQL databases - one file per database
#!/bin/sh
Host=localhost
BDir=~/backup/mysql
read -p "User: " user
read -s -p "Pass: " pass
Dump="/usr/local/bin/mysqldump --skip-extended-insert --force"
MySQL=/usr/local/bin/mysql
Today=$(date "+%a")
# Get a list of all databases
Databases=$(echo "SHOW DATABASES" | $MySQL -h $Host -u $user -p$pass)
for db in $Databases; do
date=`date`
file="$BDir/$Host-$db-$Today.sql.gz"
echo "Backing up '$db' from '$Host' on '$date' to: "
echo " $file"
$Dump -u $user -p$pass -h $Host $db | gzip > $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment