2 # Copyright © 2019 Amdocs, Bell Canada, Samsung Electronics
3 # Copyright © 2020 Orange
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 {{- if .Values.backup.enabled }}
19 apiVersion: batch/v1beta1
22 name: {{ include "common.fullname" . }}-backup
23 namespace: {{ include "common.namespace" . }}
24 labels: {{- include "common.labels" . | nindent 4 }}
26 schedule: {{ .Values.backup.cron | quote }}
27 concurrencyPolicy: Forbid
28 startingDeadlineSeconds: 120
33 serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
34 {{ include "common.podSecurityContext" . | indent 10 | trim}}
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 }}
48 echo "Failed" > /backup/backup.log
49 echo "Backup failed!!!"
52 target_dir=/backup/backup-`date +%s`
55 mysqlhost={{ include "common.servicename" . }}.{{ include "common.namespace" . }}
57 mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
60 if [ $ret_code -ne 0 ]; then
61 remove_dir $target_dir
65 echo "Starting Backup Preparation!!!"
66 mariabackup --prepare --target-dir=$target_dir
68 if [ $ret_code -ne 0 ]; then
69 remove_dir $target_dir
72 echo "Success" > /backup/backup.log
73 echo "Backup Successful!!!"
76 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
77 resources: {{ include "common.resources" . | nindent 12 }}
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 }}
87 - name: MYSQL_ROOT_PASSWORD
88 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }}
96 echo "Validation Failed!!!";
99 backup_result=`cat /backup/backup.log`
100 rm -rf /backup/backup.log
102 if [ "$backup_result" == "Failed" ]; then
103 echo "Backup Failed!!! Validation Failed!!!";
107 target_dir=$(ls -td -- /backup/backup-* | head -n 1)
108 cp -Ra $target_dir/* /var/lib/mysql/
110 if [ ! "$(ls -A /var/lib/mysql)" ]; then
111 remove_dir $target_dir
115 /docker-entrypoint.sh mysqld &
118 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SELECT 1";
120 count=`expr $count + 1`;
121 if [ $count -ge 30 ]; then
122 remove_dir $target_dir
127 mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log
128 error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
132 if [ $error_lines -gt 1 ];then
133 remove_dir $target_dir
135 echo "Validation successful!!!"
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
143 resources: {{ include "common.resources" . | nindent 12 }}
145 - mountPath: /etc/localtime
151 - name: {{ include "common.namespace" . }}-docker-registry-key
157 persistentVolumeClaim:
158 claimName: {{ include "common.fullname" . }}-backup-data