blob: a5f15781594327db9182bd61f626067435910b0a [file] [log] [blame]
Akansha Dua3fb95ef2019-09-04 11:47:43 +00001{{/*
2# Copyright © 2019 Amdocs, Bell Canada
3#
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 "+" "_" }}
25 release: {{ .Release.Name }}
26 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:
38 - /root/ready.py
39 args:
40 - --container-name
41 - {{ include "common.name" . }}
42 env:
43 - name: NAMESPACE
44 valueFrom:
45 fieldRef:
46 apiVersion: v1
47 fieldPath: metadata.namespace
48 image: "{{ .Values.global.readinessRepository }}/{{ .Values.global.readinessImage }}"
49 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
89 valueFrom:
90 secretKeyRef:
91 name: {{ include "common.fullname" . }}
92 key: db-root-password
93 volumeMounts:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +010094 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +000095 mountPath: /backup
Akansha Dua3fb95ef2019-09-04 11:47:43 +000096 containers:
97 - name: mariadb-backup-validate
98 image: "{{ include "common.repository" . }}/{{ .Values.backupImage }}"
99 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
100 env:
101 - name: MYSQL_ROOT_PASSWORD
102 valueFrom:
103 secretKeyRef:
104 name: {{ include "common.fullname" . }}
105 key: db-root-password
106 command:
107 - /bin/bash
108 - -c
109 - |
110 remove_dir(){
111 dirToRemove=$1
112 rm -rf $dirToRemove
113 echo "Validation Failed!!!";
114 }
115
116 backup_result=`cat /backup/backup.log`
117 rm -rf /backup/backup.log
118
119 if [ "$backup_result" == "Failed" ]; then
120 echo "Backup Failed!!! So Validation Failed!!!";
121 exit 0
122 fi
123
124 target_dir=$(ls -td -- /backup/backup-* | head -n 1)
125 cp -Ra $target_dir/* /var/lib/mysql/
126
127 if [ ! "$(ls -A /var/lib/mysql)" ]; then
128 remove_dir $target_dir
129 exit 0
130 fi
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100131
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000132 /docker-entrypoint.sh mysqld &
133
134 count=0
135 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SELECT 1";
136 do sleep 3;
137 count=`expr $count + 1`;
138 if [ $count -ge 30 ]; then
139 remove_dir $target_dir
140 exit 0;
141 fi;
142 done
143
144 mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log
145 error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l`
146
147 cat /tmp/output.log
148
149 if [ $error_lines -gt 1 ];then
150 remove_dir $target_dir
151 else
152 echo "Validation successful!!!"
153 cd /backup
154 totalFiles=`ls -t | grep "backup-" | wc -l`
155 if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
156 filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
157 ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
158 fi
159 fi
160 volumeMounts:
161 - mountPath: /etc/localtime
162 name: localtime
163 readOnly: true
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100164 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000165 mountPath: /backup
166 volumes:
167 - name: localtime
168 hostPath:
169 path: /etc/localtime
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100170 - name: backup-dir
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000171 persistentVolumeClaim:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100172 claimName: {{ include "common.fullname" . }}-backup-data
Akansha Dua3fb95ef2019-09-04 11:47:43 +0000173{{- end }}