diff --git a/Dockerfile b/Dockerfile index 3aac76c..7a7bfd5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,10 +39,13 @@ ENV POSTGRES_DB="**None**" \ COPY backup.sh /backup.sh RUN chmod +x backup.sh +COPY init_script.sh /init_script.sh +RUN chmod +x init_script.sh + COPY hooks /hooks ENTRYPOINT ["/bin/sh", "-c"] -CMD ["exec /usr/local/bin/go-cron -s \"$SCHEDULE\" -p \"$HEALTHCHECK_PORT\" -- /backup.sh"] +CMD ["./init_script.sh"] HEALTHCHECK --interval=5m --timeout=3s \ CMD curl -f "http://localhost:$HEALTHCHECK_PORT/" || exit 1 diff --git a/README.md b/README.md index 9f23126..bb94455 100644 --- a/README.md +++ b/README.md @@ -70,20 +70,20 @@ docker run -d \ |TELEGRAM_BOT_TOKEN | Only used to call Telegram's public API | ### Environmental variables -| Name | Default value | Description | -|------------------------| :------ |------------------------------------------------| -| POSTGRES_DB | - | Database name | -| POSTGRES_HOST | - | PostgreSQL IP address or hostname | -| POSTGRES_PORT | 5432 | Connection TCP port | -| POSTGRES_USER | - | Database user | -| POSTGRES_PASSWORD | - | Database user password | -| POSTGRES_EXTRA_OPTS | --blobs | Extra options `pg_dump` run | -| SCHEDULE | @daily | `go-cron` schedule. See [this](#backup-using-go-cron) | -| HEALTHCHECK_PORT | 8080 | Port listening for cron-schedule health check. | -| S3_ACCESS_KEY_ID | - | Key or username with RW access to bucket | -| S3_SECRET_ACCESS_KEY | - | Secret or password for `S3_ACCESS_KEY_ID` | -| S3_BUCKET | - | Name of bucket created for backups | -| S3_ENDPOINT | - | URL of S3 storage | +| Name | Default value | Is mandatory | Description | +|------------------------| :------ | :------: |------------------------------------------------| +| POSTGRES_DB | - | YES | Database name | +| POSTGRES_HOST | - | YES | PostgreSQL IP address or hostname | +| POSTGRES_PORT | 5432 | - | Connection TCP port | +| POSTGRES_USER | - | YES | Database user | +| POSTGRES_PASSWORD | - | YES | Database user password | +| POSTGRES_EXTRA_OPTS | --blobs | - | Extra options `pg_dump` run | +| SCHEDULE | @daily | - | `go-cron` schedule. See [this](#backup-using-go-cron) | +| HEALTHCHECK_PORT | 8080 | - | Port listening for cron-schedule health check. | +| S3_ACCESS_KEY_ID | - | YES | Key or username with RW access to bucket | +| S3_SECRET_ACCESS_KEY | - | YES | Secret or password for `S3_ACCESS_KEY_ID` | +| S3_BUCKET | - | YES | Name of bucket created for backups | +| S3_ENDPOINT | - | YES | URL of S3 storage | ### Notification selection diff --git a/backup.sh b/backup.sh index 02897b9..3717add 100644 --- a/backup.sh +++ b/backup.sh @@ -3,7 +3,7 @@ # Script made for backup PostgreSQL database from local (${POSTGRES_HOST}=127.0.0.1) # or remote host. Created backup stores in S3 storage. On completion script calls # notification scripts from hooks/ directory to send report to given Telegram Chat -# based on variables set private or public notification method will be selected +# based on variables set private or public notification method will be selected set -euo pipefail IFS=$'\n\t' diff --git a/init_script.sh b/init_script.sh new file mode 100755 index 0000000..67311e9 --- /dev/null +++ b/init_script.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# +# Script checks if mandatory variables are set and starts cron job for DB backup + +set -euo pipefail +IFS=$'\n\t' + + +# Begin values check +if [[ ${POSTGRES_DB} == "**None**" ]] || + [[ ${S3_ACCESS_KEY_ID} == "**None**" ]] || + [[ ${S3_SECRET_ACCESS_KEY} == "**None**" ]] || + [[ ${S3_BUCKET} == "**None**" ]] || + [[ ${S3_ENDPOINT} == "**None**" ]] || + [[ ${POSTGRES_HOST} == "**None**" ]] || + [[ ${POSTGRES_USER} == "**None**" ]] || + [[ ${POSTGRES_PASSWORD} == "**None**" ]] || + [[ ${S3_ACCESS_KEY_ID} == "**None**" ]]; then + echo "One or more mandatory values is missing. Check your configuration..." >&2 + exit 0 +else +# Will create base backup + echo "All needed variables seems set. Starting main procedure" + exec /usr/local/bin/go-cron -s $SCHEDULE -p $HEALTHCHECK_PORT -- /backup.sh +fi