210fbd02ba52d5f0784cd52084debf81f0aa999e
[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 .Values.backup.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               {{ include "common.containerSecurityContext" . | indent 14 | trim }}
41               command:
42                 - /bin/bash
43                 - -c
44                 - |
45                     remove_dir(){
46                       dirToRemove=$1
47                       rm -rf $dirToRemove
48                       echo "Failed" > /backup/backup.log
49                       echo "Backup failed!!!"
50                     }
51
52                     target_dir=/backup/backup-`date +%s`
53                     mkdir -p $target_dir
54
55                     mysqlhost={{ include "common.servicename" . }}.{{ include "common.namespace" . }}
56
57                     mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
58
59                     ret_code=$?
60                     if [ $ret_code -ne 0 ]; then
61                       remove_dir $target_dir
62                       exit 0
63                     fi
64
65                     echo "Starting Backup Preparation!!!"
66                     mariabackup --prepare --target-dir=$target_dir
67                     ret_code=$?
68                     if [ $ret_code -ne 0 ]; then
69                       remove_dir $target_dir
70                       exit 0
71                     fi
72                     echo "Success" > /backup/backup.log
73                     echo "Backup Successful!!!"
74               env:
75                 - name: DB_PASS
76                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
77               resources: {{ include "common.resources" . | nindent 12 }}
78               volumeMounts:
79                 - name: backup-dir
80                   mountPath: /backup
81           containers:
82             - name: mariadb-backup-validate
83               image: {{ include "repositoryGenerator.image.mariadb" . }}
84               imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
85               {{ include "common.containerSecurityContext" . | indent 14 | trim }}
86               env:
87                 - name: MYSQL_ROOT_PASSWORD
88                   {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
89               command:
90                 - /bin/bash
91                 - -c
92                 - |
93                   remove_dir(){
94                     dirToRemove=$1
95                     rm -rf $dirToRemove
96                     echo "Validation Failed!!!";
97                   }
98
99                   backup_result=`cat /backup/backup.log`
100                   rm -rf /backup/backup.log
101
102                   if [ "$backup_result" == "Failed" ]; then
103                     echo "Backup Failed!!! Validation Failed!!!";
104                     exit 0
105                   fi
106
107                   target_dir=$(ls -td -- /backup/backup-* | head -n 1)
108                   cp -Ra $target_dir/* /var/lib/mysql/
109
110                   if [ ! "$(ls -A /var/lib/mysql)" ]; then
111                     remove_dir $target_dir
112                     exit 0
113                   fi
114
115                   /docker-entrypoint.sh mysqld &
116
117                   count=0
118                   until mysql --user=root --password=$MYSQL_ROOT_PASSWORD  -e "SELECT 1";
119                     do sleep 3;
120                     count=`expr $count + 1`;
121                     if [ $count -ge 30 ]; then
122                       remove_dir $target_dir
123                       exit 0;
124                     fi;
125                   done
126
127                   mysqlcheck -A  --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log
128                   error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
129
130                   cat /tmp/output.log
131
132                   if [ $error_lines -gt 1 ];then
133                     remove_dir $target_dir
134                   else
135                     echo "Validation successful!!!"
136                     cd /backup
137                     totalFiles=`ls -t | grep "backup-" | wc -l`
138                     if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
139                       filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
140                       ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
141                     fi
142                   fi
143               resources: {{ include "common.resources" . | nindent 12 }}
144               volumeMounts:
145                 - mountPath: /etc/localtime
146                   name: localtime
147                   readOnly: true
148                 - name: backup-dir
149                   mountPath: /backup
150           imagePullSecrets:
151             - name: {{ include "common.namespace" . }}-docker-registry-key
152           volumes:
153             - name: localtime
154               hostPath:
155                 path: /etc/localtime
156             - name: backup-dir
157               persistentVolumeClaim:
158                 claimName: {{ include "common.fullname" . }}-backup-data
159 {{- end }}