Added option for scheduled cron

This commit is contained in:
Paulino Padial 2018-01-23 18:44:03 +01:00
parent 7b96dd80d5
commit 408d2d2cc3
2 changed files with 23 additions and 3 deletions

View File

@ -25,7 +25,8 @@ Remember to map your /etc/localtime to the /etc/localtime of the container (good
| NAME | VALUES | DEFAULT | DESCRIPTION | | 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. | | DBHOST | hostname | localhost | name of the db host to connect. |
| USERNAME | string | postgres | user used to connects to the db. | | 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. | | 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. |

View File

@ -8,9 +8,9 @@
# hostname:*:*:dbuser:dbpass # hostname:*:*:dbuser:dbpass
# replace hostname with the value of DBHOST and postgres with # replace hostname with the value of DBHOST and postgres with
# the value of USERNAME # the value of USERNAME
PASSPHRASE="" PASSPHRASE=""
if [ "${PASSWORD_SECRET}" ]; then if [ "${PASSWORD_SECRET}" ]; then
echo "Using docker secrets..."
if [ -f "/run/secrets/${PASSWORD_SECRET}" ]; then if [ -f "/run/secrets/${PASSWORD_SECRET}" ]; then
PASSPHRASE=$(cat /run/secrets/${PASSWORD_SECRET}) PASSPHRASE=$(cat /run/secrets/${PASSWORD_SECRET})
else else
@ -19,13 +19,32 @@ if [ "${PASSWORD_SECRET}" ]; then
exit 1 exit 1
fi fi
else else
echo "Using environment password..."
PASSPHRASE=${PASSWORD} PASSPHRASE=${PASSWORD}
fi 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 # Create the file
echo "Creating the password file..."
cat <<-EOF > ${HOME}/.pgpass cat <<-EOF > ${HOME}/.pgpass
${DBHOST}:*:*:${USERNAME:-postgres}:${PASSPHRASE} ${DBHOST}:*:*:${USERNAME:-postgres}:${PASSPHRASE}
EOF EOF
# Execute cron with parameters (autopostgresql script is under /etc/cron.daily) # 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}