Skip to content

Instantly share code, notes, and snippets.

@panbanda
Last active April 9, 2018 13:00
Show Gist options
  • Save panbanda/addc459076e8dceddfa2e8e5f5712052 to your computer and use it in GitHub Desktop.
Save panbanda/addc459076e8dceddfa2e8e5f5712052 to your computer and use it in GitHub Desktop.
I had some personal docker compose'd containers that I wanted to backup without creating a cronjob for each. This backs up all db containers to S3 assuming they're named *_db_1
#!/bin/bash
S3KEY=""
S3SECRET=""
function putS3
{
path=$1
file=$2
aws_path=$3
bucket='bucket-name'
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
string="PUT\n\n$content_type\n$date\n$acl\n/$bucket$aws_path$file"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
curl -X PUT -T "$path/$file" \
-H "Host: $bucket.s3.amazonaws.com" \
-H "Date: $date" \
-H "Content-Type: $content_type" \
-H "$acl" \
-H "Authorization: AWS ${S3KEY}:$signature" \
"https://$bucket.s3.amazonaws.com$aws_path$file"
}
# Selects the composer started *_db_1 containers
NAME="_db_1"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BASEDIR=$(dirname "$0")
mkdir -p $BASEDIR/db-backups
for container_name in $(docker ps --filter="name=$NAME" --format '{{.Names}}')
do
output="$BASEDIR/db-backups/$container_name.sql"
docker exec $container_name /usr/bin/mysqldump -uroot --all-databases > $output;
done
TARFILE=db-backup-$TIMESTAMP.tgz
tar -czf $BASEDIR/$TARFILE $BASEDIR/db-backups/
putS3 $BASEDIR $TARFILE "/backups/docker/database/"
# Cleanup
rm -rf $BASEDIR/db-backups $BASEDIR/*.tgz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment