From 408d2d2cc3e31c6b8907b0c9990588d959f6ed1d Mon Sep 17 00:00:00 2001 From: Paulino Padial Date: Tue, 23 Jan 2018 18:44:03 +0100 Subject: [PATCH] Added option for scheduled cron --- README.md | 3 ++- docker-entrypoint.sh | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c6252f8..9fa226e 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ Remember to map your /etc/localtime to the /etc/localtime of the container (good | NAME | VALUES | DEFAULT | DESCRIPTION | | :-------------- | :-------------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| ENV LOG_LEVEL | 1 to 8 | 8 | Level of verbosite. Most verbose is 0, less verbose is 8 | +| CRON_LOG_LEVEL | 1 to 8 | 8 | Level of verbosite. Most verbose is 0, less verbose is 8 | +| CRON_SCHEDULE | a valid cron specification | empty | By default the app uses cron.daily schedule, but you can't crontrol the hour, so, is a ramdon momment during the day. If you want to schedule a fix time to run the backups define this environment variable with a valid cron_schedule. | | DBHOST | hostname | localhost | name of the db host to connect. | | USERNAME | string | postgres | user used to connects to the db. | | PASSWORD | string | empty | password for the user to connects to the db. Remember doing this you have the password in an environment variable. If you prefer to use Docker Secrets (I recommend this) don't define this env var or leave it blank, and go to the PASSWORD_SECRET environment variable. | diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 6ef71af..fe235ab 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -8,9 +8,9 @@ # hostname:*:*:dbuser:dbpass # replace hostname with the value of DBHOST and postgres with # the value of USERNAME - PASSPHRASE="" if [ "${PASSWORD_SECRET}" ]; then + echo "Using docker secrets..." if [ -f "/run/secrets/${PASSWORD_SECRET}" ]; then PASSPHRASE=$(cat /run/secrets/${PASSWORD_SECRET}) else @@ -19,13 +19,32 @@ if [ "${PASSWORD_SECRET}" ]; then exit 1 fi else + echo "Using environment password..." PASSPHRASE=${PASSWORD} fi +# Logic for the CRON schedule +# If CRON_SCHEDULE is defined, delete the script under cron.daily and copy this one to crontab +# If CRON_SCHEDULE is not defined, don't do anything, use default cron.daily behaviour +if [ "${CRON_SCHEDULE}" ]; then + echo "Configuring a CUSTOM SCHEDULE in /etc/crontab for ${CRON_SCHEDULE} ..." + # Create the crontab file + cat <<-EOF > /etc/crontab +SHELL=/bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +# m h dom mon dow user command +${CRON_SCHEDULE} /usr/sbin/autopostgresqlbackup +EOF + # the file should have the correct content +fi + # Create the file +echo "Creating the password file..." cat <<-EOF > ${HOME}/.pgpass ${DBHOST}:*:*:${USERNAME:-postgres}:${PASSPHRASE} EOF # Execute cron with parameters (autopostgresql script is under /etc/cron.daily) -exec cron -f -l ${LOG_LEVEL:-8} +echo "Execute cron service..." +exec cron -f -l ${CRON_LOG_LEVEL:-8}