blob: 210fbd02ba52d5f0784cd52084debf81f0aa999e [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
Akansha Dua3fb95ef2019-09-04 11:47:43 +000018{{- if .Values.backup.enabled }}
19apiVersion: 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 }}
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 }
Akansha Dua3fb95ef2019-09-04 11:47:43 +000051
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010052 target_dir=/backup/backup-`date +%s`
53 mkdir -p $target_dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +000054
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010055 mysqlhost={{ include "common.servicename" . }}.{{ include "common.namespace" . }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000056
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010057 mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
Akansha Dua3fb95ef2019-09-04 11:47:43 +000058
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010059 ret_code=$?
60 if [ $ret_code -ne 0 ]; then
61 remove_dir $target_dir
62 exit 0
63 fi
Akansha Dua3fb95ef2019-09-04 11:47:43 +000064
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010065 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
Akansha Dua3fb95ef2019-09-04 11:47:43 +000081 containers:
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010082 - 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 }
Akansha Dua3fb95ef2019-09-04 11:47:43 +000098
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +010099 backup_result=`cat /backup/backup.log`
100 rm -rf /backup/backup.log
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000101
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100102 if [ "$backup_result" == "Failed" ]; then
103 echo "Backup Failed!!! Validation Failed!!!";
104 exit 0
105 fi
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000106
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100107 target_dir=$(ls -td -- /backup/backup-* | head -n 1)
108 cp -Ra $target_dir/* /var/lib/mysql/
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000109
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100110 if [ ! "$(ls -A /var/lib/mysql)" ]; then
111 remove_dir $target_dir
112 exit 0
113 fi
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100114
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100115 /docker-entrypoint.sh mysqld &
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000116
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100117 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
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000126
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100127 mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log
128 error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000129
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100130 cat /tmp/output.log
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000131
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100132 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
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000152 volumes:
Sylvain Desbureaux93a5b492020-11-27 11:07:42 +0100153 - name: localtime
154 hostPath:
155 path: /etc/localtime
156 - name: backup-dir
157 persistentVolumeClaim:
158 claimName: {{ include "common.fullname" . }}-backup-data
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000159{{- end }}