Skip to content

Instantly share code, notes, and snippets.

@RavenXce
Last active March 21, 2016 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RavenXce/2bb8f7c6b6ab86de2e9f to your computer and use it in GitHub Desktop.
Save RavenXce/2bb8f7c6b6ab86de2e9f to your computer and use it in GitHub Desktop.
Hourly 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-hourly"
HOST_DIR="/var/backups/docker-postgres-hourly"
TIME=$(date "+%Y%m%d-%H%M%S")
DATABASES=(kloudsec)
# 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/$DB-db_$TIME.sql
done
# delete backup files older than 2 days
OLD=$(find $HOST_DIR -type d -mtime +2)
if [ -n "$OLD" ] ; then
echo deleting old backup files: $OLD
echo $OLD | xargs rm -rfv
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment