Skip to content

Commit

Permalink
Metal 2665/add monitoring (#3)
Browse files Browse the repository at this point in the history
* METAL-2665 add monitoring

* METAL-2665 small fixes

* METAL-2665 add Readme
  • Loading branch information
dabde authored Jun 24, 2021
1 parent 2c260d2 commit 48288aa
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM postgres:11-alpine
ARG CLOUD_SDK_VERSION=276.0.0
ARG CLOUD_SDK_VERSION=339.0.0
ENV CLOUD_SDK_VERSION=$CLOUD_SDK_VERSION

ENV PATH /google-cloud-sdk/bin:$PATH
RUN apk --no-cache add \
curl \
python \
python3 \
py-crcmod \
bash \
libc6-compat \
Expand Down
37 changes: 37 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# pgdump-gcs

Small docker container for creating a backup of a psql database and put the dump file on a google cloud storage bucket.

## how to use

```bash
docker run \
-v ./cred:/cred \
-e DB_HOST={db host addess} \
-e DB_NAME={database-name} \
-e DB_PASSWORD_FILE=/cred/my_db_pass_as_file \
-e DB_USERNAME_FILE=/cred/my_db_user_as_file \
-e CREDENTIALFILE=/cred/credential.json \
-e GCS_BUCKET={bucket_name} \
-e PROM_NAMESPACE=kci \
kloeckneri/pgdump-gcs:postgres-11
```

## tipps

- create a lifecycle rule to keep your gcs bucket small
- we create also a `_latest` file, so able to access the latest backup with another script

## monitoring

Simple curl pushing some basic parameter to a prometheus push gateway.

### metrics
* timestamp
* duration
* size

### labels
* job = pgdump-gcs
* source_type = postgresql
* source_name = `${DB_NAME}`
23 changes: 19 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ set -e

echo "Prepare configuration for script"
TIMESTAMP=$(date +%F_%R)
START_TIMESTAMP=$(date +%s)
BACKUP_FILE=${DB_NAME}-${TIMESTAMP}.sql.gz
BACKUP_FILE_LATEST=${DB_NAME}-latest.sql.gz
DB_HOST=${DB_HOST:-localhost}
DB_PASSWORD=$(cat ${DB_PASSWORD_FILE})
DB_USER=$(cat ${DB_USERNAME_FILE})
CREDENTIALFILE=${CREDENTIALFILE:-/srv/gcloud/credentials.json}
PROM_NAMESPACE=${PROM_NAMESPACE:-kci}

if [ ! -f ${CREDENTIALFILE} ]
then
Expand All @@ -21,18 +23,31 @@ echo "login to gcloud with SA"
gcloud auth activate-service-account --key-file=/srv/gcloud/credentials.json

# create login credential file
echo *:5432:*:${DB_USER}:${DB_PASSWORD} >> ~/.pgpass
chmod 0600 ~/.pgpass
(umask 377 && echo *:5432:*:${DB_USER}:${DB_PASSWORD} >> ~/.pgpass)

echo "Start create backup"
pg_dump -F c -Z 9 -h ${DB_HOST} -p 5432 -U ${DB_USER} ${DB_NAME} -f ${BACKUP_FILE}
BACKUP_SIZE=$(du ${BACKUP_FILE} | awk '{print $1}')
echo "End backup"

## copy to destination
echo "Copy to gcs"
gsutil cp ${BACKUP_FILE} gs://${GCS_BUCKET}/${DB_NAME}/${BACKUP_FILE} && gsutil cp ${BACKUP_FILE} gs://${GCS_BUCKET}/${DB_NAME}/${BACKUP_FILE_LATEST}

if test $? -ne 0
END_TIMESTAMP=$(date +%s)
BACKUP_DURATION=$((END_TIMESTAMP - START_TIMESTAMP))
if [[ ! -z "$PROMETHEUS_PUSH_GATEWAY" ]];
then
exit 1;
echo "sending monitoring metrics to ${PROMETHEUS_PUSH_GATEWAY}"
cat <<EOF | curl -s --data-binary @- http://${PROMETHEUS_PUSH_GATEWAY}/metrics/job/pgdump-gcs/source_type/postgresql/source_name/${DB_NAME}
# TYPE ${PROM_NAMESPACE}_backup_timestamp counter
# HELP ${PROM_NAMESPACE}_backup_timestamp Timestamp of last backup run
${PROM_NAMESPACE}_backup_timestamp $END_TIMESTAMP
# TYPE ${PROM_NAMESPACE}_backup_duration gauge
# HELP ${PROM_NAMESPACE}_backup_duration Time the backup run take until finished
${PROM_NAMESPACE}_backup_duration $BACKUP_DURATION
# TYPE ${PROM_NAMESPACE}_backup_size gauge
# HELP ${PROM_NAMESPACE}_backup_size Backup Size in bytes
${PROM_NAMESPACE}_backup_size $BACKUP_SIZE
EOF
fi

0 comments on commit 48288aa

Please sign in to comment.