Merge "[UUI] Service Mesh Compliance for UUI"
[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.persistence.enabled }}
19 apiVersion: batch/v1beta1
20 kind: CronJob
21 metadata:
22   name: {{ include "common.fullname" . }}-backup
23   namespace: {{ include "common.namespace" . }}
24   labels: {{- include "common.labels" . | nindent 4 }}
25 spec:
26   schedule: {{ .Values.backup.cron | quote }}
27   concurrencyPolicy: Forbid
28   startingDeadlineSeconds: 120
29   jobTemplate:
30     spec:
31       template:
32         spec:
33           serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
34           {{ include "common.podSecurityContext" . | indent 10 | trim}}
35           restartPolicy: Never
36           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 }}
40               securityContext:
41                 allowPrivilegeEscalation: false
42                 privileged: false
43                 readOnlyRootFilesystem: false
44               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                     }
54
55                     target_dir=/backup/backup-`date +%s`
56                     mkdir -p $target_dir
57
58                     mysqlhost={{ include "common.fullname" . }}-0.{{ include "common.servicename" . }}-headless.{{ include "common.namespace" . }}
59
60                     mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
61
62                     ret_code=$?
63                     if [ $ret_code -ne 0 ]; then
64                       remove_dir $target_dir
65                       exit 0
66                     fi
67
68                     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
84                 - name: data
85                   mountPath: /bitnami/mariadb
86           containers:
87             - name: mariadb-backup-validate
88               image: {{ include "repositoryGenerator.image.mariadb" . }}
89               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
90               securityContext:
91                 allowPrivilegeEscalation: false
92                 privileged: false
93                 readOnlyRootFilesystem: false
94               env:
95                 - name: MARIADB_ROOT_PASSWORD
96                   {{- 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                   }
106
107                   backup_result=`cat /backup/backup.log`
108                   rm -rf /backup/backup.log
109
110                   if [ "$backup_result" == "Failed" ]; then
111                     echo "Backup Failed!!! Validation Failed!!!";
112                     exit 0
113                   fi
114
115                   target_dir=$(ls -td -- /backup/backup-* | head -n 1)
116                   cp -Ra $target_dir/* /bitnami/mariadb/data
117
118                   if [ ! "$(ls -A /bitnami/mariadb/data)" ]; then
119                     remove_dir $target_dir
120                     exit 0
121                   fi
122
123                   /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh &
124
125                   count=0
126                   until mysql --user=root --password=$MARIADB_ROOT_PASSWORD  -e "SELECT 1";
127                     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
134
135                   mysqlcheck -A  --user=root --password=$MARIADB_ROOT_PASSWORD > /tmp/output.log
136                   error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
137
138                   cat /tmp/output.log
139
140                   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:
153                 - mountPath: /bitnami/mariadb/data
154                   name: tmp-data
155                 - mountPath: /opt/bitnami/mariadb/tmp
156                   name: tmp
157                 - 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
164           volumes:
165             - name: localtime
166               hostPath:
167                 path: /etc/localtime
168             - 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 }}
175             - name: backup-dir
176               persistentVolumeClaim:
177                 claimName: {{ include "common.fullname" . }}-backup-data
178             - name: tmp-data
179               emptyDir: {}
180             - name: tmp
181               emptyDir: {}
182 {{- end }}