Skip to content

Instantly share code, notes, and snippets.

@RavenXce
Last active March 21, 2016 11:54
Show Gist options
  • Save RavenXce/8ef2ad6a52a5a2ebfbec to your computer and use it in GitHub Desktop.
Save RavenXce/8ef2ad6a52a5a2ebfbec to your computer and use it in GitHub Desktop.
Daily postgres backup for docker
#! /bin/bash
# directories to save backups in, must be mounted as a volume on docker: `-v $HOST_DIR:$DOCKER_DIR`
DOCKER_DIR="/tmp/backups-daily"
HOST_DIR="/var/backups/docker-postgres-daily"
S3_DIR="s3://docker-postgres-backups"
DATABASES=(kloudsec)
YMD=$(date "+%Y-%m-%d")
# make dir for date if it doesn't exist
mkdir -p $HOST_DIR/$YMD
# make database backup for all dbs
CONTAINER_ID=$(docker ps | grep postgres | awk '{ print $1 }')
for DB in "${DATABASES[@]}"
do
docker exec -it $CONTAINER_ID pg_dump -U $DB -f $DOCKER_DIR/$YMD/$DB-db.sql
done
# delete backup files older than 14 days
OLD=$(find $HOST_DIR -type d -mtime +14)
if [ -n "$OLD" ] ; then
echo deleting old backup files: $OLD
echo $OLD | xargs rm -rfv
fi
# sync to s3
aws s3 sync $HOST_DIR $S3_DIR --sse
@RavenXce
Copy link
Author

Uses S3 cli to sync backups. Lifecycle management is recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment