[MARIADB][COMMON] Add support for mariadb-operator
[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         spec:
36           serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
37           {{ include "common.podSecurityContext" . | indent 10 | trim}}
38           restartPolicy: Never
39           initContainers: {{- include "common.readinessCheck.waitFor" . | nindent 12 }}
40             - name: mariadb-galera-backup-init
41               image: {{ include "repositoryGenerator.image.mariadb" . }}
42               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
43               securityContext:
44                 allowPrivilegeEscalation: false
45                 privileged: false
46                 readOnlyRootFilesystem: false
47               command:
48                 - /bin/bash
49                 - -c
50                 - |
51                     remove_dir(){
52                       dirToRemove=$1
53                       rm -rf $dirToRemove
54                       echo "Failed" > /backup/backup.log
55                       echo "Backup failed!!!"
56                     }
57
58                     target_dir=/backup/backup-`date +%s`
59                     mkdir -p $target_dir
60
61                     mysqlhost={{ include "common.fullname" . }}-0.{{ include "common.servicename" . }}-headless.{{ include "common.namespace" . }}
62
63                     mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
64
65                     ret_code=$?
66                     if [ $ret_code -ne 0 ]; then
67                       remove_dir $target_dir
68                       exit 0
69                     fi
70
71                     echo "Starting Backup Preparation!!!"
72                     mariabackup --prepare --target-dir=$target_dir
73                     ret_code=$?
74                     if [ $ret_code -ne 0 ]; then
75                       remove_dir $target_dir
76                       exit 0
77                     fi
78                     echo "Success" > /backup/backup.log
79                     echo "Backup Successful!!!"
80               env:
81                 - name: DB_PASS
82                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
83               resources: {{ include "common.resources" . | nindent 16 }}
84               volumeMounts:
85                 - name: backup-dir
86                   mountPath: /backup
87                 - name: data
88                   mountPath: /bitnami/mariadb
89           containers:
90             - name: mariadb-backup-validate
91               image: {{ include "repositoryGenerator.image.mariadb" . }}
92               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
93               securityContext:
94                 allowPrivilegeEscalation: false
95                 privileged: false
96                 readOnlyRootFilesystem: false
97               env:
98                 - name: MARIADB_ROOT_PASSWORD
99                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
100               command:
101                 - /bin/bash
102                 - -c
103                 - |
104                   remove_dir(){
105                     dirToRemove=$1
106                     rm -rf $dirToRemove
107                     echo "Validation Failed!!!";
108                   }
109
110                   backup_result=`cat /backup/backup.log`
111                   rm -rf /backup/backup.log
112
113                   if [ "$backup_result" == "Failed" ]; then
114                     echo "Backup Failed!!! Validation Failed!!!";
115                     exit 0
116                   fi
117
118                   target_dir=$(ls -td -- /backup/backup-* | head -n 1)
119                   cp -Ra $target_dir/* /bitnami/mariadb/data
120
121                   if [ ! "$(ls -A /bitnami/mariadb/data)" ]; then
122                     remove_dir $target_dir
123                     exit 0
124                   fi
125
126                   /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh &
127
128                   count=0
129                   until mysql --user=root --password=$MARIADB_ROOT_PASSWORD  -e "SELECT 1";
130                     do sleep 3;
131                     count=`expr $count + 1`;
132                     if [ $count -ge 30 ]; then
133                       remove_dir $target_dir
134                       exit 0;
135                     fi;
136                   done
137
138                   mysqlcheck -A  --user=root --password=$MARIADB_ROOT_PASSWORD > /tmp/output.log
139                   error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
140
141                   cat /tmp/output.log
142
143                   if [ $error_lines -gt 1 ];then
144                     remove_dir $target_dir
145                   else
146                     echo "Validation successful!!!"
147                     cd /backup
148                     totalFiles=`ls -t | grep "backup-" | wc -l`
149                     if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
150                       filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
151                       ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
152                     fi
153                   fi
154               resources: {{ include "common.resources" . | nindent 16 }}
155               volumeMounts:
156                 - mountPath: /bitnami/mariadb/data
157                   name: tmp-data
158                 - mountPath: /opt/bitnami/mariadb/tmp
159                   name: tmp
160                 - mountPath: /etc/localtime
161                   name: localtime
162                   readOnly: true
163                 - name: backup-dir
164                   mountPath: /backup
165           imagePullSecrets:
166             - name: {{ include "common.namespace" . }}-docker-registry-key
167           volumes:
168             - name: localtime
169               hostPath:
170                 path: /etc/localtime
171             - name: data
172               persistentVolumeClaim:
173             {{- if .Values.persistence.existingClaim }}
174                 claimName: {{ .Values.persistence.existingClaim }}
175             {{- else }}
176                 claimName: {{ include "common.fullname" . }}-{{ include "common.fullname" . }}-0
177             {{- end }}
178             - name: backup-dir
179               persistentVolumeClaim:
180                 claimName: {{ include "common.fullname" . }}-backup-data
181             - name: tmp-data
182               emptyDir: {}
183             - name: tmp
184               emptyDir: {}
185 {{- end }}
186 {{- end }}