Created
September 20, 2015 00:47
-
-
Save marcbachmann/529d049b6342750c10a9 to your computer and use it in GitHub Desktop.
Database backup script
This file contains hidden or 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
#!/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