From 69e8b53d4910781d29553da72655106b807940b5 Mon Sep 17 00:00:00 2001 From: Max Wittig Date: Mon, 3 Feb 2020 11:16:23 +0100 Subject: [PATCH] feat(postgres-backup): add prometheus metrics exporter --- postgres-backup-s3/Dockerfile | 4 +++- postgres-backup-s3/README.md | 10 ++++++++++ postgres-backup-s3/backup.sh | 8 +++++++- postgres-backup-s3/write_metrics.sh | 20 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100755 postgres-backup-s3/write_metrics.sh diff --git a/postgres-backup-s3/Dockerfile b/postgres-backup-s3/Dockerfile index af5740d..929f395 100644 --- a/postgres-backup-s3/Dockerfile +++ b/postgres-backup-s3/Dockerfile @@ -2,7 +2,7 @@ FROM alpine:3.9 LABEL maintainer="Johannes Schickling " 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"] diff --git a/postgres-backup-s3/README.md b/postgres-backup-s3/README.md index a5339de..abed83d 100644 --- a/postgres-backup-s3/README.md +++ b/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 +``` diff --git a/postgres-backup-s3/backup.sh b/postgres-backup-s3/backup.sh index 6e5a7f0..1eef85b 100644 --- a/postgres-backup-s3/backup.sh +++ b/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" diff --git a/postgres-backup-s3/write_metrics.sh b/postgres-backup-s3/write_metrics.sh new file mode 100755 index 0000000..040a05b --- /dev/null +++ b/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.$$ <