forked from Martlark/pg_dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·110 lines (95 loc) · 2.64 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# entrypoint.sh
set -e
crond
su - ${PGUSER}
if [[ -z $COMMAND ]];
then
COMMAND=${1:-dump-cron}
fi
CRON_SCHEDULE=${CRON_SCHEDULE:-0 1 * * *}
PREFIX=${PREFIX:-dump}
PGUSER=${PGUSER:-postgres}
PGPORT=${PGPORT:-5432}
PGDUMP=${PGDUMP:-'/dump'}
RUN_DOUBLE=${RUN_DOUBLE:-"true"}
if [[ ${RUN_DOUBLE} == "true" ]];
then
PGHOST=${PGHOST:-db}
else
PGHOST=${PGHOST:-localhost}
fi
if [[ -n ${PGDB} ]];
then
POSTGRES_DB=${PGDB:-postgres}
else
POSTGRES_DB=${POSTGRES_DB:-postgres}
fi
if [[ -n ${PG_LOG} ]];
then
echo "COMMAND: ${COMMAND}"
echo "CRON_SCHEDULE: ${CRON_SCHEDULE}"
echo "PREFIX: ${PREFIX}"
echo "PGUSER: ${PGUSER}"
echo "POSTGRES_DB: ${POSTGRES_DB}"
echo "PGHOST: ${PGHOST}"
echo "PGPORT: ${PGPORT}"
echo "PGDUMP: ${PGDUMP}"
if [[ -n ${POSTGRES_PASSWORD_FILE} ]];
then
echo "POSTGRES_PASSWORD_FILE: ${POSTGRES_PASSWORD_FILE}"
fi
fi
if [[ -f ${POSTGRES_PASSWORD_FILE} ]];
then
source ${POSTGRES_PASSWORD_FILE}
else
echo "WARN: No password file found!"
echo "It is suggested that a docker secrets file is used for security concerns."
if [[ -n ${PGPASSWORD} ]];
then
POSTGRES_PASSWORD=${PGPASSWORD}
elif [[ -z ${POSTGRES_PASSWORD} ]];
then
echo "ERROR: No POSTGRES_PASSWORD set!"
exit 1
fi
fi
if [[ "${COMMAND}" == 'dump' ]]; then
/usr/local/bin/docker-entrypoint.sh postgres &
SLEEPTIME=5
echo "Sleeping ${SLEEPTIME} seconds to allow database to stand up..."
sleep ${SLEEPTIME}
exec /dump.sh
elif [[ "${COMMAND}" == 'dump-cron' ]]; then
if [[ -z ${LOGFIFO} ]];
then
LOGFIFO=${PGDUMP}/cron.fifo
fi
if [[ -n ${PG_LOG} ]];
then
echo "LOGFIFO: ${LOGFIFO}"
fi
if [[ ! -e "${LOGFIFO}" ]]; then
mkfifo "${LOGFIFO}"
fi
CRON_ENV="PREFIX='$PREFIX'\nPGUSER='${PGUSER}'\nPOSTGRES_DB='${POSTGRES_DB}'\nPGHOST='${PGHOST}'\nPGPORT='${PGPORT}'\nPGDUMP='${PGDUMP}'"
if [[ -n "${POSTGRES_PASSWORD}" ]]; then
CRON_ENV="$CRON_ENV\nPOSTGRES_PASSWORD='${POSTGRES_PASSWORD}'"
fi
if [[ ! -z "${RETAIN_COUNT}" ]]; then
CRON_ENV="$CRON_ENV\nRETAIN_COUNT='${RETAIN_COUNT}'"
fi
echo -e "$CRON_ENV\n$CRON_SCHEDULE /dump.sh > $LOGFIFO 2>&1" | crontab -
if [[ "${RUN_DOUBLE}" == "false" ]];
then
# Run postgres in the background. After testing, the cron task does not execute
# unless the $LOGFIFO is followed requiring this.
/usr/local/bin/docker-entrypoint.sh postgres > ${PGDUMP}/postgres.log 2>&1 &
fi
tail -f ${LOGFIFO} # For cron to run this file must be followed
else
echo "Unknown command: $COMMAND"
echo "Available commands: dump, dump-cron"
exit 1
fi