docker-autopostgresqlbackup/docker-entrypoint.sh

58 lines
2.0 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
2019-05-30 13:39:38 +02:00
crontab /etc/crontab
2018-01-23 18:58:37 +01:00
else
echo "Using cron.daily schedule..."
2019-05-30 13:39:38 +02:00
echo -e "#!/bin/bash\n/usr/sbin/autopostgresqlbackup" > /etc/periodic/daily/backup.sh
chmod +x /etc/periodic/daily/backup.sh
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
2018-01-23 19:33:01 +01:00
chmod 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..."
2019-05-30 13:39:38 +02:00
exec crond -f -l ${CRON_LOG_LEVEL:-8}