Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dump a Docker-ized database to Amazon S3
#!/bin/bash
# @see http://www.jonathan-petitcolas.com/2015/09/21/dump-docker-ized-database-to-amazon-s3.html
# Configuration
FILENAME="awesomeproject-`date +%Y-%m-%d-%H:%M:%S`.sql"
CONTAINER_NAME="awesomeproject_pgsql"
DUMPS_FOLDER="/home/awesomeproject/dumps"
BUCKET_NAME="awesomeproject-private"
# Backup from docker container
docker exec $CONTAINER_NAME sh -c "PGPASSWORD=\"\$POSTGRES_PASSWORD\" pg_dump --username=\$POSTGRES_USER \$POSTGRES_USER > /tmp/$FILENAME"
docker cp $CONTAINER_NAME:/tmp/$FILENAME ~/dumps/
docker exec $CONTAINER_NAME sh -c "rm /tmp/apale-*.sql"
# Keep only 7 most recent backups
cd $DUMPS_FOLDER && (ls -t | head -n 7 ; ls) | sort | uniq -u | xargs --no-run-if-empty rm
cd $DUMPS_FOLDER && bzip2 --best $FILENAME
# Upload on S3
aws s3 sync --delete $DUMPS_FOLDER s3://$BUCKET_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.