Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 1 | {{/* |
Krzysztof Opasiak | 01c975b | 2019-12-16 17:42:38 +0100 | [diff] [blame] | 2 | # Copyright © 2019 Amdocs, Bell Canada, Samsung Electronics |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 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 }} |
| 17 | apiVersion: batch/v1beta1 |
| 18 | kind: CronJob |
| 19 | metadata: |
| 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 Opasiak | 137d7cc | 2020-01-24 23:49:11 +0100 | [diff] [blame] | 25 | release: {{ include "common.release" . }} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 26 | heritage: {{ .Release.Service }} |
| 27 | spec: |
| 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 |
Krzysztof Opasiak | a789c1a | 2020-01-22 00:06:32 +0100 | [diff] [blame] | 89 | {{- include "common.secret.envFromSecret" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 14}} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 90 | volumeMounts: |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 91 | - name: backup-dir |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 92 | mountPath: /backup |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 93 | containers: |
| 94 | - name: mariadb-backup-validate |
| 95 | image: "{{ include "common.repository" . }}/{{ .Values.backupImage }}" |
| 96 | imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} |
| 97 | env: |
| 98 | - name: MYSQL_ROOT_PASSWORD |
Krzysztof Opasiak | a789c1a | 2020-01-22 00:06:32 +0100 | [diff] [blame] | 99 | {{- include "common.secret.envFromSecret" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 14}} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 100 | command: |
| 101 | - /bin/bash |
| 102 | - -c |
| 103 | - | |
| 104 | remove_dir(){ |
| 105 | dirToRemove=$1 |
| 106 | rm -rf $dirToRemove |
| 107 | echo "Validation Failed!!!"; |
| 108 | } |
| 109 | |
| 110 | backup_result=`cat /backup/backup.log` |
| 111 | rm -rf /backup/backup.log |
| 112 | |
| 113 | if [ "$backup_result" == "Failed" ]; then |
| 114 | echo "Backup Failed!!! So Validation Failed!!!"; |
| 115 | exit 0 |
| 116 | fi |
| 117 | |
| 118 | target_dir=$(ls -td -- /backup/backup-* | head -n 1) |
| 119 | cp -Ra $target_dir/* /var/lib/mysql/ |
| 120 | |
| 121 | if [ ! "$(ls -A /var/lib/mysql)" ]; then |
| 122 | remove_dir $target_dir |
| 123 | exit 0 |
| 124 | fi |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 125 | |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 126 | /docker-entrypoint.sh mysqld & |
| 127 | |
| 128 | count=0 |
| 129 | until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SELECT 1"; |
| 130 | do sleep 3; |
| 131 | count=`expr $count + 1`; |
| 132 | if [ $count -ge 30 ]; then |
| 133 | remove_dir $target_dir |
| 134 | exit 0; |
| 135 | fi; |
| 136 | done |
| 137 | |
| 138 | mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log |
| 139 | error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l` |
| 140 | |
| 141 | cat /tmp/output.log |
| 142 | |
| 143 | if [ $error_lines -gt 1 ];then |
| 144 | remove_dir $target_dir |
| 145 | else |
| 146 | echo "Validation successful!!!" |
| 147 | cd /backup |
| 148 | totalFiles=`ls -t | grep "backup-" | wc -l` |
| 149 | if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then |
| 150 | filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}` |
| 151 | ls -tr | grep backup | head -$filestoDelete | xargs rm -rf |
| 152 | fi |
| 153 | fi |
| 154 | volumeMounts: |
| 155 | - mountPath: /etc/localtime |
| 156 | name: localtime |
| 157 | readOnly: true |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 158 | - name: backup-dir |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 159 | mountPath: /backup |
| 160 | volumes: |
| 161 | - name: localtime |
| 162 | hostPath: |
| 163 | path: /etc/localtime |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 164 | - name: backup-dir |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 165 | persistentVolumeClaim: |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 166 | claimName: {{ include "common.fullname" . }}-backup-data |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 167 | {{- end }} |