docker-autopostgresqlbackup/docker-entrypoint.sh

55 lines
1.8 KiB
Bash
Raw Normal View History

2018-01-23 18:58:37 +01:00
#!/bin/bash
2018-01-23 18:01:22 +01:00
2018-01-23 18:12:33 +01:00
# Logic for Password file required
# If PASSWORD_SECRET env var is defined, search for the /run/secrets/${PASSWORD_SECRET} and read the content
# If PASSWORD_SECRET is not defined, use PASSWORD env variable
# The idea, as specified in the software:
# create a file $HOME/.pgpass containing a line like this
# hostname:*:*:dbuser:dbpass
# replace hostname with the value of DBHOST and postgres with
# the value of USERNAME
PASSPHRASE=""
if [ "${PASSWORD_SECRET}" ]; then
2018-01-23 18:44:03 +01:00
echo "Using docker secrets..."
2018-01-23 18:12:33 +01:00
if [ -f "/run/secrets/${PASSWORD_SECRET}" ]; then
2018-01-23 18:32:57 +01:00
PASSPHRASE=$(cat /run/secrets/${PASSWORD_SECRET})
2018-01-23 18:12:33 +01:00
else
echo "ERROR: Secret file not found in /run/secrets/${PASSWORD_SECRET}"
echo "Please verify your docker secrets configuration."
exit 1
fi
else
2018-01-23 18:44:03 +01:00
echo "Using environment password..."
2018-01-23 18:32:57 +01:00
PASSPHRASE=${PASSWORD}
2018-01-23 18:12:33 +01:00
fi
2018-01-23 18:44:03 +01:00
# 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
2018-01-23 18:58:37 +01:00
cat <<-EOF > /etc/crontab
2018-01-23 18:44:03 +01:00
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
2018-01-23 18:58:37 +01:00
${CRON_SCHEDULE} /usr/sbin/autopostgresqlbackup
EOF
else
echo "Using cron.daily schedule..."
2018-01-23 18:44:03 +01:00
fi
2018-01-23 18:12:33 +01:00
# Create the file
2018-01-23 18:44:03 +01:00
echo "Creating the password file..."
2018-01-23 18:12:33 +01:00
cat <<-EOF > ${HOME}/.pgpass
2018-01-23 18:58:37 +01:00
${DBHOST:-localhost}:*:*:${USERNAME:-postgres}:${PASSPHRASE}
2018-01-23 18:12:33 +01:00
EOF
# Permissions for this file shoudl be set to 0600
chmod +x 0600 ${HOME}/.pgpass
2018-01-23 18:12:33 +01:00
# Execute cron with parameters (autopostgresql script is under /etc/cron.daily)
2018-01-23 18:44:03 +01:00
echo "Execute cron service..."
2018-01-23 18:58:37 +01:00
exec cron -f -l ${CRON_LOG_LEVEL:-8}