|
2 | 2 |
|
3 | 3 | set -e |
4 | 4 |
|
5 | | -echo "Fix permissions" |
6 | | -mkdir -p /home/postgres/restore |
| 5 | +echo "Create pghoard directories..." |
7 | 6 | chown -R postgres /home/postgres |
8 | 7 |
|
9 | | -echo "Create physical_replication_slot on master ..." |
10 | | -export PGPASSWORD=$PG_PASSWORD |
11 | | -until psql -qAt -U replicator -h $PG_HOST -d postgres -c "select user;"; do |
12 | | - echo "sleep 1s and try again ..." |
13 | | - sleep 1 |
14 | | -done |
15 | | -psql -h $PG_HOST -c "WITH foo AS (SELECT COUNT(*) AS count FROM pg_replication_slots WHERE slot_name='pghoard') SELECT pg_create_physical_replication_slot('pghoard') FROM foo WHERE count=0;" -U replicator -d postgres |
16 | | - |
17 | 8 | echo "Create pghoard configuration with confd ..." |
18 | 9 | if getent hosts rancher-metadata; then |
19 | 10 | confd -onetime -backend rancher -prefix /2015-12-19 |
20 | 11 | else |
21 | 12 | confd -onetime -backend env |
22 | 13 | fi |
23 | 14 |
|
24 | | -echo "Dump configuration..." |
25 | | -cat /home/postgres/pghoard.json |
| 15 | +if [ -z "${PGHOARD_RESTORE_SITE}" ]; then |
| 16 | + |
| 17 | + echo "Dump configuration..." |
| 18 | + cat /home/postgres/pghoard.json |
| 19 | + |
| 20 | + echo "Create physical_replication_slot on master ..." |
| 21 | + export PGPASSWORD=$PG_PASSWORD |
| 22 | + until psql -qAt -U replicator -h $PG_HOST -d postgres -c "select user;"; do |
| 23 | + echo "sleep 1s and try again ..." |
| 24 | + sleep 1 |
| 25 | + done |
| 26 | + psql -h $PG_HOST -c "WITH foo AS (SELECT COUNT(*) AS count FROM pg_replication_slots WHERE slot_name='pghoard') SELECT pg_create_physical_replication_slot('pghoard') FROM foo WHERE count=0;" -U replicator -d postgres |
| 27 | + |
| 28 | + echo "Run the pghoard daemon ..." |
| 29 | + exec gosu postgres pghoard --short-log --config /home/postgres/pghoard.json |
| 30 | + |
| 31 | +else |
| 32 | + |
| 33 | + echo "Dump configuration..." |
| 34 | + cat /home/postgres/pghoard_restore.json |
| 35 | + |
| 36 | + echo "Set pghoard to maintenance mode" |
| 37 | + touch /tmp/pghoard_maintenance_mode_file |
| 38 | + |
| 39 | + echo "Get the latest available basebackup ..." |
| 40 | + gosu postgres pghoard_restore get-basebackup --config pghoard_restore.json --site $PGHOARD_RESTORE_SITE --target-dir restore --restore-to-master --recovery-target-action promote --recovery-end-command "pkill pghoard" --overwrite $* |
26 | 41 |
|
27 | | -echo "Run the pghoard daemon ..." |
28 | | -exec gosu postgres pghoard --short-log --config /home/postgres/pghoard.json |
| 42 | + # remove custom server configuration (espacially the hot standby parameter) |
| 43 | + gosu postgres mv restore/postgresql.auto.conf restore/postgresql.auto.conf.backup |
| 44 | + |
| 45 | + echo "Start the pghoard daemon ..." |
| 46 | + gosu postgres pghoard --short-log --config /home/postgres/pghoard_restore.json & |
| 47 | + |
| 48 | + if [ -z "$RESTORE_CHECK_COMMAND" ]; then |
| 49 | + # Manual mode |
| 50 | + # Just start PostgreSQL |
| 51 | + echo "Start PostgresSQL ..." |
| 52 | + exec gosu postgres postgres -D restore |
| 53 | + else |
| 54 | + # Automatic test mode |
| 55 | + # Run test commands against PostgreSQL server and exit |
| 56 | + echo "Start PostgresSQL ..." |
| 57 | + gosu postgres pg_ctl -D restore start |
| 58 | + |
| 59 | + # Give postgres some time before starting the harassment |
| 60 | + sleep 20 |
| 61 | + |
| 62 | + until gosu postgres psql -At -c "SELECT * FROM pg_is_in_recovery()" | grep -q f |
| 63 | + do |
| 64 | + sleep 5 |
| 65 | + echo "AutoCheck: waiting for restoration to finish..." |
| 66 | + done |
| 67 | + |
| 68 | + echo "AutoCheck: running command on db..." |
| 69 | + OUT_LINES=$(gosu postgres psql -c "$RESTORE_CHECK_COMMAND" "$RESTORE_CHECK_DB" | wc -l) |
| 70 | + echo "AutoCheck: $OUT_LINES lines returned" |
| 71 | + |
| 72 | + if [ $OUT_LINES -gt 0 ]; then |
| 73 | + echo "AutoCheck: SUCCESS" |
| 74 | + RES=1 |
| 75 | + else |
| 76 | + echo "AutoCheck: FAILURE" |
| 77 | + RES=0 |
| 78 | + fi |
| 79 | + |
| 80 | + if [ ! -z "$PUSHGATEWAY_URL" ]; then |
| 81 | + cat << EOF | curl --binary-data @- ${PUSHGATEWAY_URL}/metrics/jobs/pghoard_restore/instances/${PGHOARD_RESTORE_SITE} |
| 82 | + check_success ${RES} |
| 83 | +EOF |
| 84 | + fi |
| 85 | + fi |
| 86 | +fi |
0 commit comments