forked from Annixa/docker-pg_dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
43 lines (37 loc) · 1.1 KB
/
start.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
#!/bin/bash
set -e
COMMAND=${1:-dump}
CRON_SCHEDULE=${CRON_SCHEDULE:-0 3 * * *}
PREFIX=${PREFIX:-dump}
PGUSER=${PGUSER:-postgres}
PGDB=${PGDB:-postgres}
PGHOST=${PGHOST:-db}
PGPORT=${PGPORT:-5432}
PGTABLE=${PGTABLE:-*}
if [ ! -z ${PGTABLE_SCRIPT+x} ]; then
PGTABLE=$(bash -c "echo $PGTABLE_SCRIPT")
fi
if [[ "$COMMAND" == 'dump' ]]; then
exec /dump.sh
elif [[ "$COMMAND" == 'dump-cron' ]]; then
LOGFIFO='/var/log/cron.fifo'
if [[ ! -e "$LOGFIFO" ]]; then
mkfifo "$LOGFIFO"
fi
CRON_ENV="PREFIX='$PREFIX'\nPGUSER='$PGUSER'\nPGDB='$PGDB'\nPGHOST='$PGHOST'\nPGPORT='$PGPORT'\nPGTABLE='$PGTABLE'"
if [ -n "$PGPASSWORD" ]; then
CRON_ENV="$CRON_ENV\nPGPASSWORD='$PGPASSWORD'"
fi
if [ ! -z "$DELETE_OLDER_THAN" ]; then
CRON_ENV="$CRON_ENV\nDELETE_OLDER_THAN='$DELETE_OLDER_THAN'"
fi
echo "$CRON_ENV\n$CRON_SCHEDULE /dump.sh > $LOGFIFO 2>&1"
echo -e "$CRON_ENV\n$CRON_SCHEDULE /dump.sh > $LOGFIFO 2>&1" | crontab -
crontab -l
cron
tail -f "$LOGFIFO"
else
echo "Unknown command $COMMAND"
echo "Available commands: dump, dump-cron"
exit 1
fi