Browse Source

feat(postgres-backup): add prometheus metrics exporter

pull/126/head
Max Wittig 4 years ago
parent
commit
69e8b53d49
  1. 4
      postgres-backup-s3/Dockerfile
  2. 10
      postgres-backup-s3/README.md
  3. 8
      postgres-backup-s3/backup.sh
  4. 20
      postgres-backup-s3/write_metrics.sh

4
postgres-backup-s3/Dockerfile

@ -2,7 +2,7 @@ FROM alpine:3.9
LABEL maintainer="Johannes Schickling <schickling.j@gmail.com>"
ADD install.sh install.sh
RUN sh install.sh && rm install.sh
RUN sh install.sh && rm install.sh && mkdir -p /metrics
ENV POSTGRES_DATABASE **None**
ENV POSTGRES_HOST **None**
@ -18,8 +18,10 @@ ENV S3_PATH 'backup'
ENV S3_ENDPOINT **None**
ENV S3_S3V4 no
ENV SCHEDULE **None**
ENV ENABLE_METRICS false
ADD run.sh run.sh
ADD backup.sh backup.sh
ADD write_metrics.sh write_metrics.sh
CMD ["sh", "run.sh"]

10
postgres-backup-s3/README.md

@ -32,6 +32,7 @@ pgbackups3:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_EXTRA_OPTS: '--schema=public --blobs'
ENABLE_METRICS: true
```
### Automatic Periodic Backups
@ -40,3 +41,12 @@ You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="
More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).
### Metrics exporter
Optionally you can also export backup metrics, e.g. size, start time in Prometheus
file format. To read the metrics, you'll have to mount the metrics folder to your host at `/metrics`.
The file is called `metrics.txt`.
```sh
$ docker run -v $(pwd)/metrics:/metrics -e ENABLE_METRICS=true -e ... schickling/postgres-backup-s3
```

8
postgres-backup-s3/backup.sh

@ -58,11 +58,17 @@ export PGPASSWORD=$POSTGRES_PASSWORD
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
START=$(date +%s)
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | gzip > dump.sql.gz
SIZE=$(ls -nlt dump.sql.gz | head -n1 | awk '{print $5}')
echo "Uploading dump to $S3_BUCKET"
cat dump.sql.gz | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz || exit 2
END=$(date +%s)
if [ "$ENABLE_METRICS" = true ]; then
write_metrics.sh "${START}" "${END}" "${SIZE}"
fi
echo "SQL backup uploaded successfully"

20
postgres-backup-s3/write_metrics.sh

@ -0,0 +1,20 @@
METRICS_LOCATION=$1
START=$2
END=$3
SIZE=$4
# Write to a temporary file invisible to prometheus
cat > $(pwd)/postgres_backup.prom.$$ <<EOT
# HELP postgres backup start time, in unixtime.
# TYPE postgres_backup_start gauge
postgres_backup_backup_start ${START}
# HELP postgres_backup_end backup end time, in unixtime.
# TYPE postgres_backup_end gauge
postgres_backup_end ${END}
# HELP postgres_backup_size backup size, in bytes.
# TYPE postgres_backup_size gauge
postgres_backup_size ${SIZE}
EOT
# Atomic move so prometheus can see it
mv $(pwd)/postgres_backup.prom.$$ "/metrics/metrics.txt"
Loading…
Cancel
Save