Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Created September 20, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbachmann/529d049b6342750c10a9 to your computer and use it in GitHub Desktop.
Save marcbachmann/529d049b6342750c10a9 to your computer and use it in GitHub Desktop.
Database backup script
#!/bin/bash
# Based on https://gist.github.com/2206527
echo -e "Backup MySQL Databases to S3"
# Basic variables
host="localhost"
mysqlpass="rootpass"
bucket="s3://bucketname"
accesskey="awsaccesskey"
secretkey="awssecretkey"
# Timestamp (sortable AND readable)
stamp=`date +"%Y-%m-%dT%H:%M:%SZ"`
# List all the databases
databases=`docker exec mariadb mysql -u root -p$mysqlpass -h "$host" -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|performance_schema\|mysql\|test\)"`
# Feedback
echo -e "Dumping to $bucket/$stamp/"
# Loop the databases
for db in $databases; do
# Define our filenames
filename="$db.sql.gz"
tmpfile="/tmp/$filename"
object="$bucket/$stamp/$filename"
# Feedback
echo -e "Begin with backup for $db"
# Dump and zip
echo -e " creating $tmpfile"
docker exec mariadb mysqldump -u root -p$mysqlpass -h "$host" --force --opt --databases "$db" | gzip -c > "$tmpfile"
# Upload
echo -e " uploading..."
s3cmd --access_key="$accesskey" --secret_key="$secretkey" put "$tmpfile" "$object"
# Delete
rm -f "$tmpfile"
done;
echo -e "Successfully completed backup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment