blob: bce0eb9c43aa6c901f9262c71a1001e3e9ad19aa [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
Akansha Dua3fb95ef2019-09-04 11:47:43 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15*/}}
16{{- if .Values.backup.enabled }}
17apiVersion: batch/v1beta1
18kind: CronJob
19metadata:
20 name: {{ include "common.fullname" . }}-backup
21 namespace: {{ include "common.namespace" . }}
22 labels:
23 app: {{ include "common.fullname" . }}
24 chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +010025 release: {{ include "common.release" . }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000026 heritage: {{ .Release.Service }}
27spec:
28 schedule: {{ .Values.backup.cron | quote }}
29 concurrencyPolicy: Forbid
30 startingDeadlineSeconds: 120
31 jobTemplate:
32 spec:
33 template:
34 spec:
35 restartPolicy: Never
36 initContainers:
37 - command:
Sylvain Desbureaux1694e1d2020-08-21 09:58:25 +020038 - /app/ready.py
Akansha Dua3fb95ef2019-09-04 11:47:43 +000039 args:
40 - --container-name
41 - {{ include "common.name" . }}
42 env:
43 - name: NAMESPACE
44 valueFrom:
45 fieldRef:
46 apiVersion: v1
47 fieldPath: metadata.namespace
Sylvain Desbureaux1694e1d2020-08-21 09:58:25 +020048 image: "{{ include "common.repository" . }}/{{ .Values.global.readinessImage }}"
Akansha Dua3fb95ef2019-09-04 11:47:43 +000049 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
50 name: {{ include "common.name" . }}-readiness
51 - name: mariadb-galera-backup-init
52 image: "{{ include "common.repository" . }}/{{ .Values.backupImage }}"
53 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
54 command:
55 - /bin/bash
56 - -c
57 - |
58 remove_dir(){
59 dirToRemove=$1
60 rm -rf $dirToRemove
61 echo "Failed" > /backup/backup.log
62 echo "Backup failed!!!"
63 }
64
65 target_dir=/backup/backup-`date +%s`
66 mkdir -p $target_dir
67
68 mysqlhost={{ include "common.fullname" . }}-{{ sub .Values.replicaCount 1 }}.{{ .Values.service.name }}
69
70 mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost
71
72 ret_code=$?
73 if [ $ret_code -ne 0 ]; then
74 remove_dir $target_dir
75 exit 0
76 fi
77
78 echo "Starting Backup Preparation!!!"
79 mariabackup --prepare --target-dir=$target_dir
80 ret_code=$?
81 if [ $ret_code -ne 0 ]; then
82 remove_dir $target_dir
83 exit 0
84 fi
85 echo "Success" > /backup/backup.log
86 echo "Backup Successful!!!"
87 env:
88 - name: DB_PASS
Krzysztof Opasiakc0a57f82020-03-23 15:50:13 +010089 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 14}}
Akansha Dua3fb95ef2019-09-04 11:47:43 +000090 volumeMounts:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +010091 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +000092 mountPath: /backup
Marat Salakhutdinov2ae64f62020-07-24 09:52:19 -040093 - name: db-data
94 mountPath: /var/lib/mysql
Akansha Dua3fb95ef2019-09-04 11:47:43 +000095 containers:
96 - name: mariadb-backup-validate
97 image: "{{ include "common.repository" . }}/{{ .Values.backupImage }}"
98 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
99 env:
100 - name: MYSQL_ROOT_PASSWORD
Krzysztof Opasiakc0a57f82020-03-23 15:50:13 +0100101 {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 14}}
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000102 command:
103 - /bin/bash
104 - -c
105 - |
106 remove_dir(){
107 dirToRemove=$1
108 rm -rf $dirToRemove
109 echo "Validation Failed!!!";
110 }
111
112 backup_result=`cat /backup/backup.log`
113 rm -rf /backup/backup.log
114
115 if [ "$backup_result" == "Failed" ]; then
116 echo "Backup Failed!!! So Validation Failed!!!";
117 exit 0
118 fi
119
120 target_dir=$(ls -td -- /backup/backup-* | head -n 1)
121 cp -Ra $target_dir/* /var/lib/mysql/
122
123 if [ ! "$(ls -A /var/lib/mysql)" ]; then
124 remove_dir $target_dir
125 exit 0
126 fi
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100127
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000128 /docker-entrypoint.sh mysqld &
129
130 count=0
131 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SELECT 1";
132 do sleep 3;
133 count=`expr $count + 1`;
134 if [ $count -ge 30 ]; then
135 remove_dir $target_dir
136 exit 0;
137 fi;
138 done
139
140 mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log
141 error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
142
143 cat /tmp/output.log
144
145 if [ $error_lines -gt 1 ];then
146 remove_dir $target_dir
147 else
148 echo "Validation successful!!!"
149 cd /backup
150 totalFiles=`ls -t | grep "backup-" | wc -l`
151 if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
152 filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
153 ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
154 fi
155 fi
156 volumeMounts:
157 - mountPath: /etc/localtime
158 name: localtime
159 readOnly: true
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100160 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000161 mountPath: /backup
162 volumes:
163 - name: localtime
164 hostPath:
165 path: /etc/localtime
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100166 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000167 persistentVolumeClaim:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100168 claimName: {{ include "common.fullname" . }}-backup-data
Marat Salakhutdinov2ae64f62020-07-24 09:52:19 -0400169 - name: db-data
170 persistentVolumeClaim:
171 claimName: {{ include "common.fullname" . }}-data-{{ include "common.fullname" . }}-{{ sub .Values.replicaCount 1 }}
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000172{{- end }}