f2128693e819a2a8a96ae417b36cecdc20154b8f
[oom.git] / kubernetes / common / mariadb-galera / templates / backup / cronjob.yaml
1 {{/*
2 # Copyright © 2019 Amdocs, Bell Canada, Samsung Electronics
3 # Copyright © 2020 Orange
4 #
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 */}}
17
18 {{- if and .Values.backup.enabled .Values.global.mariadbGalera.useOperator }}
19 {{ include "common.mariadbOpBackup" . }}
20 {{ else }}
21 {{- if and .Values.backup.enabled .Values.persistence.enabled }}
22 apiVersion: batch/v1beta1
23 kind: CronJob
24 metadata:
25   name: {{ include "common.fullname" . }}-backup
26   namespace: {{ include "common.namespace" . }}
27   labels: {{- include "common.labels" . | nindent 4 }}
28 spec:
29   schedule: {{ .Values.backup.cron | quote }}
30   concurrencyPolicy: Forbid
31   startingDeadlineSeconds: 120
32   jobTemplate:
33     spec:
34       template:
35         metadata:
36           annotations:
37             # Workarround to exclude K8S API from istio communication
38             # as init-container (readinessCheck) does not work with the
39             # Istio CNI plugin, see:
40             # (https://istio.io/latest/docs/setup/additional-setup/cni/#compatibility-with-application-init-containers)
41             traffic.sidecar.istio.io/excludeOutboundPorts: "443"
42         spec:
43           serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
44           {{ include "common.podSecurityContext" . | indent 10 | trim}}
45           restartPolicy: Never
46           initContainers: {{- include "common.readinessCheck.waitFor" . | nindent 12 }}
47             - name: mariadb-galera-backup-init
48               image: {{ include "repositoryGenerator.image.mariadb" . }}
49               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
50               securityContext:
51                 allowPrivilegeEscalation: false
52                 privileged: false
53                 readOnlyRootFilesystem: false
54               command:
55                 - /bin/bash
56                 - -c
57                 - |
58                     remove_dir(){
59                       dirToRemove=$1
60                       rm -rf $dirToRemove
61                       echo "Failed" > /backup/backup.log
62                       echo "Backup failed!!!"
63                     }
64
65                     target_dir=/backup/backup-`date +%s`
66                     mkdir -p $target_dir
67
68                     mysqlhost={{ include "common.fullname" . }}-0.{{ include "common.servicename" . }}-headless.{{ include "common.namespace" . }}
69
70                     mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
71
72                     ret_code=$?
73                     if [ $ret_code -ne 0 ]; then
74                       remove_dir $target_dir
75                       exit 0
76                     fi
77
78                     echo "Starting Backup Preparation!!!"
79                     mariabackup --prepare --target-dir=$target_dir
80                     ret_code=$?
81                     if [ $ret_code -ne 0 ]; then
82                       remove_dir $target_dir
83                       exit 0
84                     fi
85                     echo "Success" > /backup/backup.log
86                     echo "Backup Successful!!!"
87               env:
88                 - name: DB_PASS
89                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
90               resources: {{ include "common.resources" . | nindent 16 }}
91               volumeMounts:
92                 - name: backup-dir
93                   mountPath: /backup
94                 - name: data
95                   mountPath: /bitnami/mariadb
96           containers:
97             - name: mariadb-backup-validate
98               image: {{ include "repositoryGenerator.image.mariadb" . }}
99               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
100               securityContext:
101                 allowPrivilegeEscalation: false
102                 privileged: false
103                 readOnlyRootFilesystem: false
104               env:
105                 - name: MARIADB_ROOT_PASSWORD
106                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
107               command:
108                 - /bin/bash
109                 - -c
110                 - |
111                   remove_dir(){
112                     dirToRemove=$1
113                     rm -rf $dirToRemove
114                     echo "Validation Failed!!!";
115                   }
116
117                   backup_result=`cat /backup/backup.log`
118                   rm -rf /backup/backup.log
119
120                   if [ "$backup_result" == "Failed" ]; then
121                     echo "Backup Failed!!! Validation Failed!!!";
122                     exit 0
123                   fi
124
125                   target_dir=$(ls -td -- /backup/backup-* | head -n 1)
126                   cp -Ra $target_dir/* /bitnami/mariadb/data
127
128                   if [ ! "$(ls -A /bitnami/mariadb/data)" ]; then
129                     remove_dir $target_dir
130                     exit 0
131                   fi
132
133                   /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh &
134
135                   count=0
136                   until mysql --user=root --password=$MARIADB_ROOT_PASSWORD  -e "SELECT 1";
137                     do sleep 3;
138                     count=`expr $count + 1`;
139                     if [ $count -ge 30 ]; then
140                       remove_dir $target_dir
141                       exit 0;
142                     fi;
143                   done
144
145                   mysqlcheck -A  --user=root --password=$MARIADB_ROOT_PASSWORD > /tmp/output.log
146                   error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
147
148                   cat /tmp/output.log
149
150                   if [ $error_lines -gt 1 ];then
151                     remove_dir $target_dir
152                   else
153                     echo "Validation successful!!!"
154                     cd /backup
155                     totalFiles=`ls -t | grep "backup-" | wc -l`
156                     if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
157                       filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
158                       ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
159                     fi
160                   fi
161               resources: {{ include "common.resources" . | nindent 16 }}
162               volumeMounts:
163                 - mountPath: /bitnami/mariadb/data
164                   name: tmp-data
165                 - mountPath: /opt/bitnami/mariadb/tmp
166                   name: tmp
167                 - mountPath: /etc/localtime
168                   name: localtime
169                   readOnly: true
170                 - name: backup-dir
171                   mountPath: /backup
172           {{- include "common.imagePullSecrets" . | nindent 10 }}
173           volumes:
174             - name: localtime
175               hostPath:
176                 path: /etc/localtime
177             - name: data
178               persistentVolumeClaim:
179             {{- if .Values.persistence.existingClaim }}
180                 claimName: {{ .Values.persistence.existingClaim }}
181             {{- else }}
182                 claimName: {{ include "common.fullname" . }}-{{ include "common.fullname" . }}-0
183             {{- end }}
184             - name: backup-dir
185               persistentVolumeClaim:
186                 claimName: {{ include "common.fullname" . }}-backup-data
187             - name: tmp-data
188               emptyDir: {}
189             - name: tmp
190               emptyDir: {}
191 {{- end }}
192 {{- end }}