blob: 4248cfe85cb67693ada1414ff265f94e748b1a61 [file] [log] [blame]
Akansha Dua3fb95ef2019-09-04 11:47:43 +00001{{/*
Krzysztof Opasiak01c975b2019-12-16 17:42:38 +01002# Copyright © 2019 Amdocs, Bell Canada, Samsung Electronics
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +01003# Copyright © 2020 Orange
Akansha Dua3fb95ef2019-09-04 11:47:43 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16*/}}
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010017
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050018{{- if and .Values.backup.enabled .Values.persistence.enabled }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000019apiVersion: batch/v1beta1
20kind: CronJob
21metadata:
22 name: {{ include "common.fullname" . }}-backup
23 namespace: {{ include "common.namespace" . }}
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010024 labels: {{- include "common.labels" . | nindent 4 }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000025spec:
26 schedule: {{ .Values.backup.cron | quote }}
27 concurrencyPolicy: Forbid
28 startingDeadlineSeconds: 120
29 jobTemplate:
30 spec:
31 template:
32 spec:
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010033 serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
34 {{ include "common.podSecurityContext" . | indent 10 | trim}}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000035 restartPolicy: Never
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010036 initContainers: {{- include "common.readinessCheck.waitFor" . | nindent 12 }}
37 - name: mariadb-galera-backup-init
38 image: {{ include "repositoryGenerator.image.mariadb" . }}
39 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050040 securityContext:
41 allowPrivilegeEscalation: false
42 privileged: false
43 readOnlyRootFilesystem: false
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010044 command:
45 - /bin/bash
46 - -c
47 - |
48 remove_dir(){
49 dirToRemove=$1
50 rm -rf $dirToRemove
51 echo "Failed" > /backup/backup.log
52 echo "Backup failed!!!"
53 }
Akansha Dua3fb95ef2019-09-04 11:47:43 +000054
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010055 target_dir=/backup/backup-`date +%s`
56 mkdir -p $target_dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +000057
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050058 mysqlhost={{ include "common.fullname" . }}-0.{{ include "common.servicename" . }}-headless.{{ include "common.namespace" . }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000059
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010060 mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
Akansha Dua3fb95ef2019-09-04 11:47:43 +000061
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010062 ret_code=$?
63 if [ $ret_code -ne 0 ]; then
64 remove_dir $target_dir
65 exit 0
66 fi
Akansha Dua3fb95ef2019-09-04 11:47:43 +000067
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010068 echo "Starting Backup Preparation!!!"
69 mariabackup --prepare --target-dir=$target_dir
70 ret_code=$?
71 if [ $ret_code -ne 0 ]; then
72 remove_dir $target_dir
73 exit 0
74 fi
75 echo "Success" > /backup/backup.log
76 echo "Backup Successful!!!"
77 env:
78 - name: DB_PASS
79 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
80 resources: {{ include "common.resources" . | nindent 12 }}
81 volumeMounts:
82 - name: backup-dir
83 mountPath: /backup
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050084 - name: data
85 mountPath: /bitnami/mariadb
Akansha Dua3fb95ef2019-09-04 11:47:43 +000086 containers:
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010087 - name: mariadb-backup-validate
88 image: {{ include "repositoryGenerator.image.mariadb" . }}
89 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050090 securityContext:
91 allowPrivilegeEscalation: false
92 privileged: false
93 readOnlyRootFilesystem: false
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010094 env:
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -050095 - name: MARIADB_ROOT_PASSWORD
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010096 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
97 command:
98 - /bin/bash
99 - -c
100 - |
101 remove_dir(){
102 dirToRemove=$1
103 rm -rf $dirToRemove
104 echo "Validation Failed!!!";
105 }
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000106
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100107 backup_result=`cat /backup/backup.log`
108 rm -rf /backup/backup.log
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000109
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100110 if [ "$backup_result" == "Failed" ]; then
111 echo "Backup Failed!!! Validation Failed!!!";
112 exit 0
113 fi
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000114
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100115 target_dir=$(ls -td -- /backup/backup-* | head -n 1)
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500116 cp -Ra $target_dir/* /bitnami/mariadb/data
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000117
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500118 if [ ! "$(ls -A /bitnami/mariadb/data)" ]; then
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100119 remove_dir $target_dir
120 exit 0
121 fi
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100122
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500123 /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh &
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000124
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100125 count=0
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500126 until mysql --user=root --password=$MARIADB_ROOT_PASSWORD -e "SELECT 1";
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100127 do sleep 3;
128 count=`expr $count + 1`;
129 if [ $count -ge 30 ]; then
130 remove_dir $target_dir
131 exit 0;
132 fi;
133 done
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000134
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500135 mysqlcheck -A --user=root --password=$MARIADB_ROOT_PASSWORD > /tmp/output.log
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100136 error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000137
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100138 cat /tmp/output.log
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000139
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100140 if [ $error_lines -gt 1 ];then
141 remove_dir $target_dir
142 else
143 echo "Validation successful!!!"
144 cd /backup
145 totalFiles=`ls -t | grep "backup-" | wc -l`
146 if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
147 filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
148 ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
149 fi
150 fi
151 resources: {{ include "common.resources" . | nindent 12 }}
152 volumeMounts:
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500153 - mountPath: /bitnami/mariadb/data
154 name: tmp-data
155 - mountPath: /opt/bitnami/mariadb/tmp
156 name: tmp
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100157 - mountPath: /etc/localtime
158 name: localtime
159 readOnly: true
160 - name: backup-dir
161 mountPath: /backup
162 imagePullSecrets:
163 - name: {{ include "common.namespace" . }}-docker-registry-key
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000164 volumes:
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100165 - name: localtime
166 hostPath:
167 path: /etc/localtime
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500168 - name: data
169 persistentVolumeClaim:
170 {{- if .Values.persistence.existingClaim }}
171 claimName: {{ .Values.persistence.existingClaim }}
172 {{- else }}
173 claimName: {{ include "common.fullname" . }}-{{ include "common.fullname" . }}-0
174 {{- end }}
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100175 - name: backup-dir
176 persistentVolumeClaim:
177 claimName: {{ include "common.fullname" . }}-backup-data
Jozsef Csongvaicabbb6f2022-03-04 15:58:31 -0500178 - name: tmp-data
179 emptyDir: {}
180 - name: tmp
181 emptyDir: {}
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000182{{- end }}