Last active
March 21, 2016 11:54
-
-
Save RavenXce/8ef2ad6a52a5a2ebfbec to your computer and use it in GitHub Desktop.
Daily postgres backup for docker
This file contains 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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses S3 cli to sync backups. Lifecycle management is recommended.