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 |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 3 | # Copyright © 2020 Orange |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 4 | # |
| 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 Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 17 | |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 18 | {{- if and .Values.backup.enabled .Values.persistence.enabled }} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 19 | apiVersion: batch/v1beta1 |
| 20 | kind: CronJob |
| 21 | metadata: |
| 22 | name: {{ include "common.fullname" . }}-backup |
| 23 | namespace: {{ include "common.namespace" . }} |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 24 | labels: {{- include "common.labels" . | nindent 4 }} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 25 | spec: |
| 26 | schedule: {{ .Values.backup.cron | quote }} |
| 27 | concurrencyPolicy: Forbid |
| 28 | startingDeadlineSeconds: 120 |
| 29 | jobTemplate: |
| 30 | spec: |
| 31 | template: |
| 32 | spec: |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 33 | serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}} |
| 34 | {{ include "common.podSecurityContext" . | indent 10 | trim}} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 35 | restartPolicy: Never |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 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 }} |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 40 | securityContext: |
| 41 | allowPrivilegeEscalation: false |
| 42 | privileged: false |
| 43 | readOnlyRootFilesystem: false |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 44 | command: |
| 45 | - /bin/bash |
| 46 | - -c |
| 47 | - | |
| 48 | remove_dir(){ |
| 49 | dirToRemove=$1 |
| 50 | rm -rf $dirToRemove |
| 51 | echo "Failed" > /backup/backup.log |
| 52 | echo "Backup failed!!!" |
| 53 | } |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 54 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 55 | target_dir=/backup/backup-`date +%s` |
| 56 | mkdir -p $target_dir |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 57 | |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 58 | mysqlhost={{ include "common.fullname" . }}-0.{{ include "common.servicename" . }}-headless.{{ include "common.namespace" . }} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 59 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 60 | mariabackup --backup --target-dir=$target_dir --user=root --password=$DB_PASS --host=$mysqlhost |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 61 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 62 | ret_code=$? |
| 63 | if [ $ret_code -ne 0 ]; then |
| 64 | remove_dir $target_dir |
| 65 | exit 0 |
| 66 | fi |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 67 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 68 | echo "Starting Backup Preparation!!!" |
| 69 | mariabackup --prepare --target-dir=$target_dir |
| 70 | ret_code=$? |
| 71 | if [ $ret_code -ne 0 ]; then |
| 72 | remove_dir $target_dir |
| 73 | exit 0 |
| 74 | fi |
| 75 | echo "Success" > /backup/backup.log |
| 76 | echo "Backup Successful!!!" |
| 77 | env: |
| 78 | - name: DB_PASS |
| 79 | {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }} |
| 80 | resources: {{ include "common.resources" . | nindent 12 }} |
| 81 | volumeMounts: |
| 82 | - name: backup-dir |
| 83 | mountPath: /backup |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 84 | - name: data |
| 85 | mountPath: /bitnami/mariadb |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 86 | containers: |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 87 | - name: mariadb-backup-validate |
| 88 | image: {{ include "repositoryGenerator.image.mariadb" . }} |
| 89 | imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 90 | securityContext: |
| 91 | allowPrivilegeEscalation: false |
| 92 | privileged: false |
| 93 | readOnlyRootFilesystem: false |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 94 | env: |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 95 | - name: MARIADB_ROOT_PASSWORD |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 96 | {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }} |
| 97 | command: |
| 98 | - /bin/bash |
| 99 | - -c |
| 100 | - | |
| 101 | remove_dir(){ |
| 102 | dirToRemove=$1 |
| 103 | rm -rf $dirToRemove |
| 104 | echo "Validation Failed!!!"; |
| 105 | } |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 106 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 107 | backup_result=`cat /backup/backup.log` |
| 108 | rm -rf /backup/backup.log |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 109 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 110 | if [ "$backup_result" == "Failed" ]; then |
| 111 | echo "Backup Failed!!! Validation Failed!!!"; |
| 112 | exit 0 |
| 113 | fi |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 114 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 115 | target_dir=$(ls -td -- /backup/backup-* | head -n 1) |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 116 | cp -Ra $target_dir/* /bitnami/mariadb/data |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 117 | |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 118 | if [ ! "$(ls -A /bitnami/mariadb/data)" ]; then |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 119 | remove_dir $target_dir |
| 120 | exit 0 |
| 121 | fi |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 122 | |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 123 | /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh & |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 124 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 125 | count=0 |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 126 | until mysql --user=root --password=$MARIADB_ROOT_PASSWORD -e "SELECT 1"; |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 127 | do sleep 3; |
| 128 | count=`expr $count + 1`; |
| 129 | if [ $count -ge 30 ]; then |
| 130 | remove_dir $target_dir |
| 131 | exit 0; |
| 132 | fi; |
| 133 | done |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 134 | |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 135 | mysqlcheck -A --user=root --password=$MARIADB_ROOT_PASSWORD > /tmp/output.log |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 136 | error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l` |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 137 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 138 | cat /tmp/output.log |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 139 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 140 | if [ $error_lines -gt 1 ];then |
| 141 | remove_dir $target_dir |
| 142 | else |
| 143 | echo "Validation successful!!!" |
| 144 | cd /backup |
| 145 | totalFiles=`ls -t | grep "backup-" | wc -l` |
| 146 | if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then |
| 147 | filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}` |
| 148 | ls -tr | grep backup | head -$filestoDelete | xargs rm -rf |
| 149 | fi |
| 150 | fi |
| 151 | resources: {{ include "common.resources" . | nindent 12 }} |
| 152 | volumeMounts: |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 153 | - mountPath: /bitnami/mariadb/data |
| 154 | name: tmp-data |
| 155 | - mountPath: /opt/bitnami/mariadb/tmp |
| 156 | name: tmp |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 157 | - mountPath: /etc/localtime |
| 158 | name: localtime |
| 159 | readOnly: true |
| 160 | - name: backup-dir |
| 161 | mountPath: /backup |
| 162 | imagePullSecrets: |
| 163 | - name: {{ include "common.namespace" . }}-docker-registry-key |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 164 | volumes: |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 165 | - name: localtime |
| 166 | hostPath: |
| 167 | path: /etc/localtime |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 168 | - name: data |
| 169 | persistentVolumeClaim: |
| 170 | {{- if .Values.persistence.existingClaim }} |
| 171 | claimName: {{ .Values.persistence.existingClaim }} |
| 172 | {{- else }} |
| 173 | claimName: {{ include "common.fullname" . }}-{{ include "common.fullname" . }}-0 |
| 174 | {{- end }} |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 175 | - name: backup-dir |
| 176 | persistentVolumeClaim: |
| 177 | claimName: {{ include "common.fullname" . }}-backup-data |
Jozsef Csongvai | cabbb6f | 2022-03-04 15:58:31 -0500 | [diff] [blame] | 178 | - name: tmp-data |
| 179 | emptyDir: {} |
| 180 | - name: tmp |
| 181 | emptyDir: {} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 182 | {{- end }} |