-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.3_create_postgres_blueprint.sh
executable file
·80 lines (80 loc) · 3 KB
/
4.3_create_postgres_blueprint.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
set -v
cat <<EOF | kubectl create -n kanister -f -
apiVersion: cr.kanister.io/v1alpha1
kind: Blueprint
metadata:
name: postgres-bp
actions:
backup:
kind: StatefulSet
outputArtifacts:
cloudObject:
keyValue:
backupLocation: "{{ .Phases.pgDump.Output.backupLocation }}"
phases:
- func: KubeTask
name: pgDump
objects:
pgSecret:
kind: Secret
name: '{{ index .Object.metadata.labels "app.kubernetes.io/instance" }}-postgresql'
namespace: '{{ .StatefulSet.Namespace }}'
args:
image: ghcr.io/kanisterio/postgres-kanister-tools:0.50.0
namespace: '{{ .StatefulSet.Namespace }}'
command:
- bash
- -c
- |
set -o pipefail
set -o errexit
export PGHOST='{{ index .Object.metadata.labels "app.kubernetes.io/instance" }}-postgresql.{{ .StatefulSet.Namespace }}.svc.cluster.local'
export PGUSER='postgres'
export PGPASSWORD='{{ index .Phases.pgDump.Secrets.pgSecret.Data "postgresql-password" | toString }}'
BACKUP_LOCATION=pg_backups/{{ .StatefulSet.Namespace }}/{{ .StatefulSet.Name }}/{{ toDate "2006-01-02T15:04:05.999999999Z07:00" .Time | date "2006-01-02T15:04:05Z07:00" }}/backup.sql.gz
pg_dumpall --clean -U \$PGUSER | gzip -c | kando location push --profile '{{ toJson .Profile }}' --path "\${BACKUP_LOCATION}" -
kando output backupLocation "\${BACKUP_LOCATION}"
restore:
kind: StatefulSet
inputArtifactNames:
- cloudObject
phases:
- func: KubeTask
name: pgRestore
objects:
pgSecret:
kind: Secret
name: '{{ index .Object.metadata.labels "app.kubernetes.io/instance" }}-postgresql'
namespace: '{{ .StatefulSet.Namespace }}'
args:
image: ghcr.io/kanisterio/postgres-kanister-tools:0.50.0
namespace: '{{ .StatefulSet.Namespace }}'
command:
- bash
- -c
- |
set -o pipefail
set -o errexit
export PGHOST='{{ index .Object.metadata.labels "app.kubernetes.io/instance" }}-postgresql.{{ .StatefulSet.Namespace }}.svc.cluster.local'
export PGUSER='postgres'
export PGPASSWORD='{{ index .Phases.pgRestore.Secrets.pgSecret.Data "postgresql-password" | toString }}'
BACKUP_LOCATION={{ .ArtifactsIn.cloudObject.KeyValue.backupLocation }}
kando location pull --profile '{{ toJson .Profile }}' --path "\${BACKUP_LOCATION}" - | gunzip -c -f | psql -q -U "\${PGUSER}"
delete:
type: Namespace
inputArtifactNames:
- cloudObject
phases:
- func: KubeTask
name: deleteDump
args:
image: ghcr.io/kanisterio/postgres-kanister-tools:0.50.0
namespace: "{{ .Namespace.Name }}"
command:
- bash
- -c
- |
set -o pipefail
set -o errexit
kando location delete --profile '{{ toJson .Profile }}' --path '{{ .ArtifactsIn.cloudObject.KeyValue.backupLocation }}'
EOF