Merge pull request #14 from truenas/nextcloud-backup

Take backup of postgres on helm upgrade
This commit is contained in:
Waqar Ahmed 2020-12-15 01:09:42 +05:00 committed by GitHub
commit 8a7aac597d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 192 additions and 10 deletions

View File

@ -1,6 +1,6 @@
apiVersion: v2
name: nextcloud
version: 1.0.0
version: 1.1.0
appVersion: 19.0.3
description: A file sharing server that puts the control and security of your own data back into your hands.
keywords:

View File

@ -107,9 +107,8 @@ app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Postgres Selector labels
Nextcloud service account
*/}}
{{- define "nextcloud.postgres.selectorLabels" -}}
app.kubernetes.io/name: {{ include "nextcloud.name" . }}-postgres
app.kubernetes.io/instance: {{ .Release.Name }}-postgres
{{- end }}
{{- define "nextcloud.serviceAccountName" -}}
{{- printf "%s-service-account" .Release.Name -}}
{{- end -}}

View File

@ -0,0 +1,28 @@
{{/*
Get Nextloud Postgres Database Name
*/}}
{{- define "postgres.DatabaseName" -}}
{{- print "nextcloud" -}}
{{- end -}}
{{/*
Postgres Selector labels
*/}}
{{- define "nextcloud.postgres.selectorLabels" -}}
app.kubernetes.io/name: {{ include "nextcloud.name" . }}-postgres
app.kubernetes.io/instance: {{ .Release.Name }}-postgres
{{- end }}
{{- define "postgres.imageName" -}}
{{- print "postgres:13.1" -}}
{{- end -}}
{{/*
Retrieve postgres backup name
This will return a unique name based on revision and chart numbers specified.
*/}}
{{- define "postgres.backupName" -}}
{{- $upgradeDict := .Values.ixChartContext.upgradeMetadata -}}
{{- printf "postgres-backup-from-%s-to-%s-revision-%d" $upgradeDict.oldChartVersion $upgradeDict.newChartVersion (int64 $upgradeDict.preUpgradeRevision) -}}
{{- end }}

View File

@ -0,0 +1,21 @@
{{/*
Retrieve previous chart version from which we are upgrading to a newer chart version
*/}}
{{- define "tn.chart.old_version" -}}
{{- if .Values.ixChartContext.is_upgrade -}}
{{- .Values.ixChartContext.upgradeMetadata.oldChartVersion -}}
{{- else -}}
{{- fail "A chart upgrade is not taking place" -}}
{{- end -}}
{{- end -}}
{{/*
Retrieve new chart version to which we are upgrading from an old chart version
*/}}
{{- define "tn.chart.new_version" -}}
{{- if .Values.ixChartContext.is_upgrade -}}
{{- .Values.ixChartContext.upgradeMetadata.newChartVersion -}}
{{- else -}}
{{- fail "A chart upgrade is not taking place" -}}
{{- end -}}
{{- end -}}

View File

@ -0,0 +1,15 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: "postgres-backup-hook-config-map"
annotations:
rollme: {{ randAlphaNum 5 | quote }}
data:
entrypoint.sh: |-
#!/bin/sh
cmd="/docker-entrypoint.sh postgres"
eval "${cmd}" & disown;
until pg_isready; do
sleep 5;
done;
pg_dump -U $POSTGRES_USER -d {{ template "postgres.DatabaseName" . }} > /postgres_backups/$BACKUP_NAME;

View File

@ -0,0 +1,57 @@
{{- if .Values.ixChartContext.isUpgrade -}}
apiVersion: batch/v1
kind: Job
metadata:
name: "pre-upgrade-hook2"
annotations:
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-succeeded
rollme: {{ randAlphaNum 5 | quote }}
spec:
template:
metadata:
name: "pre-upgrade-hook2"
spec:
restartPolicy: Never
serviceAccountName: "{{ template "nextcloud.serviceAccountName" . }}"
containers:
- name: {{ .Chart.Name }}-postgres-backup
image: {{ template "postgres.imageName" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: db-details
key: db-user
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-details
key: db-password
- name: BACKUP_NAME
value: {{ template "postgres.backupName" . }}
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
- name: postgres-backup
mountPath: /postgres_backups
- name: backup-script-configmap
mountPath: /bin/backup_entrypoint.sh
readOnly: true
subPath: entrypoint.sh
command:
- "/bin/backup_entrypoint.sh"
volumes:
- name: postgres-data
hostPath:
path: {{ template "configuredPostgresHostPath" . }}
- name: postgres-backup
hostPath:
path: {{ template "configuredBackupPostgresHostPath" . }}
- name: backup-script-configmap
configMap:
defaultMode: 0700
name: "postgres-backup-hook-config-map"
{{- end -}}

View File

@ -38,8 +38,8 @@ spec:
{{- end }}
initContainers:
- name: init-postgresdb
image: busybox:latest
command: ['sh', '-c', "until nc -w 5 -vz {{ template "nextcloud.fullname" . }}-postgres 5432; do echo waiting for postgres; sleep 2; done"]
image: {{ template "postgres.imageName" . }}
command: ['sh', '-c', "until pg_isready -h {{ template "nextcloud.fullname" . }}-postgres; do echo waiting for postgres; sleep 2; done"]
imagePullPolicy: {{ .Values.image.pullPolicy }}
containers:
- name: {{ .Chart.Name }}
@ -49,7 +49,7 @@ spec:
- name: POSTGRES_HOST
value: {{ template "nextcloud.fullname" . }}-postgres:5432
- name: POSTGRES_DB
value: "nextcloud"
value: {{ template "postgres.DatabaseName" . }}
- name: POSTGRES_USER
valueFrom:
secretKeyRef:

View File

@ -0,0 +1,22 @@
{{- if .Values.ixChartContext.isUpgrade -}}
apiVersion: batch/v1
kind: Job
metadata:
name: "pre-upgrade-hook1"
annotations:
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "-1"
"helm.sh/hook-delete-policy": hook-succeeded
rollme: {{ randAlphaNum 5 | quote }}
spec:
template:
metadata:
name: "pre-upgrade-hook1"
spec:
restartPolicy: Never
serviceAccountName: "{{ template "nextcloud.serviceAccountName" . }}"
containers:
- name: kubectl
image: "bitnami/kubectl:1.19"
command: ["kubectl", "delete" , "deployment", "{{ template "nextcloud.fullname" . }}", "{{ template "nextcloud.fullname" . }}-postgres"]
{{- end -}}

View File

@ -25,7 +25,7 @@ spec:
spec:
containers:
- name: {{ .Chart.Name }}-postgres
image: "postgres:13.1"
image: {{ template "postgres.imageName" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: POSTGRES_USER

View File

@ -0,0 +1,39 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: "{{ template "nextcloud.serviceAccountName" . }}"
namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: "{{ .Release.Name }}-service-account-role-binding"
subjects:
- kind: ServiceAccount
name: "{{ template "nextcloud.serviceAccountName" . }}"
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
name: "{{ .Release.Name }}-service-account-role"
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: "{{ .Release.Name }}-service-account-role"
namespace: {{ .Release.Namespace }}
rules:
- apiGroups:
- ""
- "apps"
resources:
- pods
- deployments
verbs:
- delete
- get
- list

View File

@ -25,3 +25,4 @@ service:
nodePort: 31000
emptyDirVolumes: true
ixChartContext: {}