Skip to content

Commit 70c92c1

Browse files
author
Matthew Fisher
committed
feat(postgres): run periodic backups in the background
backup retention and frequency of backups are configurable through environment variables.
1 parent ac4b163 commit 70c92c1

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

contrib/ci/test.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ puts-step() {
66
echo "-----> $@"
77
}
88

9+
puts-error() {
10+
echo "!!! $@"
11+
}
12+
913
# make sure we are in this dir
1014
CURRENT_DIR=$(cd $(dirname $0); pwd)
1115

@@ -43,11 +47,11 @@ echo "cert" > $CURRENT_DIR/tmp/k8s/ca.crt
4347
MINIO_JOB=$(docker run -dv $CURRENT_DIR/tmp/aws-admin:/var/run/secrets/deis/minio/admin -v $CURRENT_DIR/tmp/aws-user:/var/run/secrets/deis/minio/user -v $CURRENT_DIR/tmp/k8s:/var/run/secrets/kubernetes.io/serviceaccount quay.io/deisci/minio:v2-beta boot server /home/minio/)
4448

4549
# boot postgres, linking the minio container and setting DEIS_MINIO_SERVICE_HOST and DEIS_MINIO_SERVICE_PORT
46-
PG_JOB=$(docker run -d --link $MINIO_JOB:minio -e DEIS_MINIO_SERVICE_HOST=minio -e DEIS_MINIO_SERVICE_PORT=9000 -v $CURRENT_DIR/tmp/creds:/var/run/secrets/deis/database/creds -v $CURRENT_DIR/tmp/aws-user:/etc/wal-e.d/env $1)
50+
PG_JOB=$(docker run -d --link $MINIO_JOB:minio -e BACKUP_FREQUENCY=1s -e DEIS_MINIO_SERVICE_HOST=minio -e DEIS_MINIO_SERVICE_PORT=9000 -v $CURRENT_DIR/tmp/creds:/var/run/secrets/deis/database/creds -v $CURRENT_DIR/tmp/aws-user:/etc/wal-e.d/env $1)
4751

4852
# wait for postgres to boot
49-
puts-step "sleeping for 1 minute while postgres is booting..."
50-
sleep 1m
53+
puts-step "sleeping for 90s while postgres is booting..."
54+
sleep 90s
5155

5256
# display logs for debugging purposes
5357
puts-step "displaying minio logs"
@@ -59,6 +63,14 @@ docker logs $PG_JOB
5963
puts-step "checking if database is running"
6064
docker exec $PG_JOB is_running
6165

66+
# check if minio has the 5 backups
67+
puts-step "checking if minio has 5 backups"
68+
NUM_BACKUPS="$(docker exec $MINIO_JOB ls /home/minio/dbwal/basebackups_005/ | grep json | wc -l)"
69+
if [[ ! "$NUM_BACKUPS" -eq "5" ]]; then
70+
puts-error "did not find 5 base backups, which is the default (found $NUM_BACKUPS)"
71+
exit 1
72+
fi
73+
6274
# success, kill off jobs
6375
puts-step "destroying containers"
6476
docker rm -f $MINIO_JOB

rootfs/bin/backup

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
export BACKUP_FREQUENCY=${BACKUP_FREQUENCY:-12h}
4+
export BACKUPS_TO_RETAIN=${BACKUPS_TO_RETAIN:-5}
5+
6+
while true; do
7+
sleep "$BACKUP_FREQUENCY"
8+
echo "Performing a base backup..."
9+
if [[ -f "$PGDATA/recovery.conf" ]] ; then
10+
echo "Database is currently recovering from a backup. Will try again next loop..."
11+
else
12+
# perform a backup
13+
envdir "$WALE_ENVDIR" wal-e backup-push "$PGDATA"
14+
# only retain the latest BACKUPS_TO_RETAIN backups
15+
envdir "$WALE_ENVDIR" wal-e delete --confirm retain "$BACKUPS_TO_RETAIN"
16+
echo "Backup has been completed."
17+
fi
18+
done
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
# Run periodic backups in the background
4+
gosu postgres backup &

0 commit comments

Comments
 (0)