Skip to content

Commit d23709e

Browse files
committed
Add restore mode
1 parent 4d28744 commit d23709e

File tree

9 files changed

+361
-38
lines changed

9 files changed

+361
-38
lines changed

9.5/README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
PGHoard Docker image
22
====================
33

4-
Usage:
5-
------
4+
Usage
5+
-----
6+
7+
###Launch in backup mode
68

79
```shell
8-
$ docker run -d \
9-
-e PGHOARD_ACTIVE_BACKUP_MODE=pg_receivexlog \
10-
-e
11-
camptocamp/pghoard
10+
$ docker run -d camptocamp/pghoard
11+
```
12+
13+
###Launch in restore mode
14+
15+
```shell
16+
$ docker run -d -e PGHOARD_RESTORE=SITE=foo camptocamp/pghoard [--recovery-target-time <time>] [--recovery-target-xid <xid>]
1217
```
1318

1419
Environment variables:
@@ -75,3 +80,7 @@ Can be either `telegraf` or `datadog`. (default `telegraf`)
7580
## OS_REGION_NAME
7681

7782
## OS_TENANT_NAME
83+
84+
## PGHOARD_RESTORE_SITE
85+
86+
Site to restore locally from storage.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[template]
2+
src = "pghoard_restore.json.tmpl"
3+
dest = "/home/postgres/pghoard_restore.json"

9.5/docker-entrypoint.sh

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,85 @@
22

33
set -e
44

5-
echo "Fix permissions"
6-
mkdir -p /home/postgres/restore
5+
echo "Create pghoard directories..."
76
chown -R postgres /home/postgres
87

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-
178
echo "Create pghoard configuration with confd ..."
189
if getent hosts rancher-metadata; then
1910
confd -onetime -backend rancher -prefix /2015-12-19
2011
else
2112
confd -onetime -backend env
2213
fi
2314

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 $*
2641

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
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"backup_location": "/home/postgres/pghoard",
3+
"backup_sites":{
4+
"{{getenv "PGHOARD_RESTORE_SITE"}}":{
5+
"basebackup_count": 1024,
6+
"nodes":[{}],
7+
"object_storage":{
8+
{{$storage_type := getenv "PGHOARD_STORAGE_TYPE"}}
9+
{{if eq $storage_type "local"}}
10+
"storage_type": "local",
11+
"directory": "{{getenv "PGHOARD_DIRECTORY"}}"
12+
{{else if eq $storage_type "s3"}}
13+
"storage_type": "s3",
14+
"aws_access_key_id":"{{getenv "AWS_ACCESS_KEY_ID"}}",
15+
"aws_secret_access_key":"{{getenv "AWS_SECRET_ACCESS_KEY"}}",
16+
"region": "{{getenv "AWS_DEFAULT_REGION"}}",
17+
{{if getenv "AWS_HOST"}}
18+
"host": "{{getenv "AWS_HOST"}}",
19+
{{end}}
20+
{{if getenv "AWS_PORT"}}
21+
"port": {{getenv "AWS_PORT"}},
22+
{{end}}
23+
{{if getenv "AWS_IS_SECURE"}}
24+
"is_secure": "{{getenv "AWS_IS_SECURE"}}",
25+
{{end}}
26+
"bucket_name":"{{getenv "AWS_BUCKETNAME"}}"
27+
{{else if eq $storage_type "swift"}}
28+
"storage_type": "swift",
29+
"user":"{{getenv "OS_USERNAME"}}",
30+
"key":"{{getenv "OS_PASSWORD"}}",
31+
"auth_url":"{{getenv "OS_AUTH_URL"}}",
32+
"segment_size":5368709120,
33+
"container_name":"{{getenv "OS_CONTAINER_NAME"}}",
34+
{{if getenv "OS_REGION_NAME"}}
35+
"region_name":"{{getenv "OS_REGION_NAME"}}",
36+
{{end}}
37+
"tenant_name":"{{getenv "OS_TENANT_NAME"}}"
38+
{{end}}
39+
},
40+
"pg_data_directory": "/home/postgres/restore",
41+
"pg_xlog_directory": "/home/postgres/restore/pg_xlog"
42+
}
43+
},
44+
"http_address": "127.0.0.1",
45+
"http_port": 16000,
46+
"log_level": "{{getenv "PGHOARD_LOG_LEVEL"}}",
47+
"syslog": false,
48+
"syslog_address": "/dev/log",
49+
"syslog_facility": "local2"
50+
}

9.6/README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@ Usage:
55
------
66

77
```shell
8-
$ docker run -d \
9-
-e PGHOARD_ACTIVE_BACKUP_MODE=pg_receivexlog \
10-
-e
11-
camptocamp/pghoard
8+
$ docker run camptocamp/pghoard
9+
```
10+
11+
or
12+
13+
```shell
14+
$ docker run --entrypoint <command> camptocamp/pghoard
15+
```
16+
17+
where <command> is one of `pghoard`, `pghoard_archive_sync`, `pghoard_create_keys`, `pghoard_postgres_command` or `pghoard_restore`.
18+
19+
Launch in backup mode:
20+
----------------------
21+
22+
```shell
23+
$ docker run -d --entrypoint /backup.sh camptocamp/pghoard
24+
```
25+
26+
Launch in restore mode:
27+
-----------------------
28+
29+
```shell
30+
$ docker run -d -e PGHOARD_RESTORE=SITE=foo --entrypoint /restore.sh camptocamp/pghoard [--recovery-target-time <time>] [--recovery-target-xid <xid>]
1231
```
1332

1433
Environment variables:
@@ -75,3 +94,7 @@ Can be either `telegraf` or `datadog`. (default `telegraf`)
7594
## OS_REGION_NAME
7695

7796
## OS_TENANT_NAME
97+
98+
## PGHOARD_RESTORE_SITE
99+
100+
Site to restore locally from storage.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[template]
2+
src = "pghoard_restore.json.tmpl"
3+
dest = "/home/postgres/pghoard_restore.json"

9.6/docker-entrypoint.sh

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,85 @@
22

33
set -e
44

5-
echo "Fix permissions"
6-
mkdir -p /home/postgres/restore
5+
echo "Create pghoard directories..."
76
chown -R postgres /home/postgres
87

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-
178
echo "Create pghoard configuration with confd ..."
189
if getent hosts rancher-metadata; then
1910
confd -onetime -backend rancher -prefix /2015-12-19
2011
else
2112
confd -onetime -backend env
2213
fi
2314

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 $*
2641

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

Comments
 (0)