cronjob.yaml
8.27 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-backups
spec:
storageClassName: {{ .Values.backup.pvc.storageClassName }}
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.backup.pvc.size }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-backup-local
data:
POSTGRES_DB: |-
{{- range $database := .Values.postgresql.databases }}
{{ $database }}
{{- end }}{{ .Values.local.postgresDb | quote }}
BACKUP_KEEP_DAYS: {{ .Values.local.backupKeepDays | quote }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-auth
stringData:
POSTGRES_USER: {{ .Values.postgresql.auth.postgresqlUser }}
POSTGRES_PASSWORD: {{ .Values.postgresql.auth.postgresqlPassword }}
---
{{- if .Values.rclone.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
data:
{{ .Values.rclone.config | toYaml | nindent 2 }}
{{- end }}
---
{{- if .Values.rclone.enabled }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
stringData:
{{ .Values.rclone.secret | toYaml | nindent 2 }}
{{- end }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-backup
labels:
{{- include "postgresql-rclone.labels" . | nindent 4 }}
spec:
schedule: {{ .Values.backup.schedule }}
startingDeadlineSeconds: 3600
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 10
failedJobsHistoryLimit: 10
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
imagePullSecrets:
{{- toYaml .Values.imagePullSecrets | nindent 12 }}
volumes:
- name: backups
persistentVolumeClaim:
claimName: {{ include "postgresql-rclone.fullname" . }}-backups
- name: postgresql-auth
secret:
secretName: {{ include "postgresql-rclone.fullname" . }}-auth
- name: local-config
configMap:
name: {{ include "postgresql-rclone.fullname" . }}-local
{{- if .Values.rclone.enabled }}
- name: rclone-auth
secret:
secretName: {{ include "postgresql-rclone.fullname" . }}-rclone
- name: rclone-config
configMap:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
{{- end }}
initContainers:
- name: dump-databases
image: "{{ .Values.local.image.repository }}:{{ .Values.local.image.tag }}"
imagePullPolicy: {{ .Values.local.image.pullPolicy }}
volumeMounts:
- name: backups
mountPath: /backups
- name: local-config
mountPath: /config
- name: postgresql-auth
mountPath: /secret
env:
- name: BACKUP_KEEP_DAYS
valueFrom:
configMapKeyRef:
name: {{ include "postgresql-rclone.fullname" . }}-local
key: BACKUP_KEEP_DAYS
- name: BACKUP_DIR
value: /backups
- name: POSTGRES_HOST
value: {{ .Values.postgresql.host }}
- name: POSTGRES_DB_FILE
value: /config/POSTGRES_DB
- name: POSTGRES_USER_FILE
value: /secret/POSTGRES_USER
- name: POSTGRES_PASSWORD_FILE
value: /secret/POSTGRES_PASSWORD
command: ["/backup.sh"]
{{- if .Values.rclone.enabled }}
- name: rclone
image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}"
imagePullPolicy: {{ .Values.rclone.image.pullPolicy }}
volumeMounts:
- name: backups
mountPath: /backups
- name: rclone-config
mountPath: /config
- name: rclone-auth
mountPath: /secret
envFrom:
- configMapRef:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
- secretRef:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
{{- if .Values.rclone.crypt.enabled }}
env:
- name: RCLONE_CRYPT_REMOTE
value: ":s3:$(S3_BUCKET)/$(S3_PREFIX)/"
{{- end }}
{{- if .Values.rclone.crypt.enabled }}
args: ["copy", "/backups/", ":crypt:"]
{{- else }}
args: ["copy", "/backups/", ":s3:$(S3_BUCKET)/$(S3_PREFIX)/"]
{{- end }}
{{- end }}
containers:
- name: show-dumps
image: bash
volumeMounts:
- name: backups
mountPath: /backups
command: ["ls"]
args: ["-alR", "/backups"]
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-restores
spec:
storageClassName: {{ .Values.restore.pvc.storageClassName }}
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.restore.pvc.size }}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "postgresql-rclone.fullname" . }}-restore
labels:
{{- include "postgresql-rclone.labels" . | nindent 4 }}
spec:
template:
spec:
restartPolicy: OnFailure
imagePullSecrets:
{{- toYaml .Values.imagePullSecrets | nindent 12 }}
volumes:
- name: restores
persistentVolumeClaim:
claimName: {{ include "postgresql-rclone.fullname" . }}-restores
{{- if .Values.rclone.enabled }}
- name: rclone-auth
secret:
secretName: {{ include "postgresql-rclone.fullname" . }}-rclone
- name: rclone-config
configMap:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
{{- end }}
initContainers:
{{- if .Values.rclone.enabled }}
- name: rclone
image: "{{ .Values.rclone.image.repository }}:{{ .Values.rclone.image.tag }}"
imagePullPolicy: {{ .Values.rclone.image.pullPolicy }}
volumeMounts:
- name: restores
mountPath: /restores
- name: rclone-config
mountPath: /config
- name: rclone-auth
mountPath: /secret
envFrom:
- configMapRef:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
- secretRef:
name: {{ include "postgresql-rclone.fullname" . }}-rclone
{{- if .Values.rclone.crypt.enabled }}
env:
- name: RCLONE_CRYPT_REMOTE
value: ":s3:$(S3_BUCKET)/$(S3_PREFIX)/last/"
{{- end }}
{{- if .Values.rclone.crypt.enabled }}
args: ["copy", ":crypt:", "/restores/"]
{{- else }}
args: ["-v", "copy", ":s3:$(S3_BUCKET)/$(S3_PREFIX)/last/", "/restores/.work/"]
{{- end }}
{{- end }}
- name: link-latest
image: "{{ .Values.bash.image.repository }}:{{ .Values.bash.image.tag }}"
imagePullPolicy: {{ .Values.bash.image.pullPolicy }}
volumeMounts:
- name: restores
mountPath: /restores
command:
- bash
- -cx
- |-
find /restores/.work -maxdepth 1 -mindepth 1 -name '*.sql.gz' | \
sort | \
awk '{split($1, a, /-([^-]*)-([^-]*)$/)} !uniq[a[1] a[2] a[3]]++' | \
while read file; do \
ln -f "$file" "/restores/"
done
- name: show-restores
image: "{{ .Values.bash.image.repository }}:{{ .Values.bash.image.tag }}"
imagePullPolicy: {{ .Values.bash.image.pullPolicy }}
volumeMounts:
- name: restores
mountPath: /restores
command: ["ls"]
args: ["-alR", "/restores"]
containers:
- name: ready
image: "{{ .Values.bash.image.repository }}:{{ .Values.bash.image.tag }}"
imagePullPolicy: {{ .Values.bash.image.pullPolicy }}
volumeMounts:
- name: restores
mountPath: /restores
command:
- bash
- -cx
- |-
touch /restores/.restored