From 10043a3d68b8c25262dd8dc7d2282ed203d365b5 Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Fri, 22 Mar 2019 14:49:32 +0100 Subject: [PATCH] Add _FILE Support For User And Databases --- README.md | 4 +++- backup.sh | 28 ++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0211560..389c8fe 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,14 @@ Most variables are the same as in the [official postgres image](https://hub.dock | BACKUP_KEEP_MONTHS | Number of monthly backups to keep before removal. Defaults to `6`. | | HEALTHCHECK_PORT | Port listening for cron-schedule health check. Defaults to `8080`. | | POSTGRES_DB | Comma or space separated list of postgres databases to backup. Required. | +| POSTGRES_DB_FILE | Alternative to POSTGRES_DB, for usage with docker secrets. | | POSTGRES_EXTRA_OPTS | Additional options for `pg_dump`. Defaults to `-Z9`. | | POSTGRES_HOST | Postgres connection parameter; postgres host to connect to. Required. | | POSTGRES_PASSWORD | Postgres connection parameter; postgres password to connect with. Required. | -| POSTGRES_PASSWORD_FILE | Alternative to POSTGRES_PASSWORD, useful with docker secrets. | +| POSTGRES_PASSWORD_FILE | Alternative to POSTGRES_PASSWORD, for usage with docker secrets. | | POSTGRES_PORT | Postgres connection parameter; postgres port to connect to. Defaults to `5432`. | | POSTGRES_USER | Postgres connection parameter; postgres user to connect with. Required. | +| POSTGRES_USER_FILE | Alternative to POSTGRES_USER, for usage with docker secrets. | | SCHEDULE | [Cron-schedule](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules) specifying the interval between postgres backups. Defaults to `@daily`. | ### Manual Backups diff --git a/backup.sh b/backup.sh index ebdf5b4..c6c1637 100755 --- a/backup.sh +++ b/backup.sh @@ -2,8 +2,8 @@ set -e -if [ "${POSTGRES_DB}" = "**None**" ]; then - echo "You need to set the POSTGRES_DB environment variable." +if [ "${POSTGRES_DB}" = "**None**" -a "${POSTGRES_DB_FILE}" = "**None**" ]; then + echo "You need to set the POSTGRES_DB or POSTGRES_DB_FILE environment variable." exit 1 fi @@ -17,17 +17,25 @@ if [ "${POSTGRES_HOST}" = "**None**" ]; then fi fi -if [ "${POSTGRES_USER}" = "**None**" ]; then - echo "You need to set the POSTGRES_USER environment variable." +if [ "${POSTGRES_USER}" = "**None**" -a "${POSTGRES_USER_FILE}" = "**None**" ]; then + echo "You need to set the POSTGRES_USER or POSTGRES_USER_FILE environment variable." exit 1 fi if [ "${POSTGRES_PASSWORD}" = "**None**" -a "${POSTGRES_PASSWORD_FILE}" = "**None**" ]; then - echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES." + echo "You need to set the POSTGRES_PASSWORD or POSTGRES_PASSWORD_FILE environment variable or link to a container named POSTGRES." exit 1 fi -#Proces vars +#Process vars +if [ "${POSTGRES_DB_FILE}" = "**None**" ]; then + export POSTGRES_DB=$POSTGRES_DB +elif [ -r "${POSTGRES_DB_FILE}" ]; then + export POSTGRES_DB=$(cat ${POSTGRES_DB_FILE}) +else + echo "Missing POSTGRES_DB_FILE file." + exit 1 +fi if [ "${POSTGRES_PASSWORD_FILE}" = "**None**" ]; then export PGPASSWORD=$POSTGRES_PASSWORD elif [ -r "${POSTGRES_PASSWORD_FILE}" ]; then @@ -36,6 +44,14 @@ else echo "Missing POSTGRES_PASSWORD_FILE file." exit 1 fi +if [ "${POSTGRES_USER_FILE}" = "**None**" ]; then + export POSTGRES_USER=$POSTGRES_USER +elif [ -r "${POSTGRES_USER_FILE}" ]; then + export POSTGRES_USER=$(cat ${POSTGRES_USER_FILE}) +else + echo "Missing POSTGRES_USER_FILE file." + exit 1 +fi POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS" KEEP_DAYS=$BACKUP_KEEP_DAYS KEEP_WEEKS=`expr $((($BACKUP_KEEP_WEEKS * 7) + 1))`