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 | |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 18 | {{- if .Values.backup.enabled }} |
| 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 }} |
| 40 | {{ include "common.containerSecurityContext" . | indent 14 | trim }} |
| 41 | command: |
| 42 | - /bin/bash |
| 43 | - -c |
| 44 | - | |
| 45 | remove_dir(){ |
| 46 | dirToRemove=$1 |
| 47 | rm -rf $dirToRemove |
| 48 | echo "Failed" > /backup/backup.log |
| 49 | echo "Backup failed!!!" |
| 50 | } |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 51 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 52 | target_dir=/backup/backup-`date +%s` |
| 53 | mkdir -p $target_dir |
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 | mysqlhost={{ include "common.servicename" . }}.{{ include "common.namespace" . }} |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 56 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 57 | 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] | 58 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 59 | ret_code=$? |
| 60 | if [ $ret_code -ne 0 ]; then |
| 61 | remove_dir $target_dir |
| 62 | exit 0 |
| 63 | fi |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 64 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 65 | echo "Starting Backup Preparation!!!" |
| 66 | mariabackup --prepare --target-dir=$target_dir |
| 67 | ret_code=$? |
| 68 | if [ $ret_code -ne 0 ]; then |
| 69 | remove_dir $target_dir |
| 70 | exit 0 |
| 71 | fi |
| 72 | echo "Success" > /backup/backup.log |
| 73 | echo "Backup Successful!!!" |
| 74 | env: |
| 75 | - name: DB_PASS |
| 76 | {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }} |
| 77 | resources: {{ include "common.resources" . | nindent 12 }} |
| 78 | volumeMounts: |
| 79 | - name: backup-dir |
| 80 | mountPath: /backup |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 81 | containers: |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 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 }} |
| 86 | env: |
| 87 | - name: MYSQL_ROOT_PASSWORD |
| 88 | {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .) "key" "password") | indent 18 }} |
| 89 | command: |
| 90 | - /bin/bash |
| 91 | - -c |
| 92 | - | |
| 93 | remove_dir(){ |
| 94 | dirToRemove=$1 |
| 95 | rm -rf $dirToRemove |
| 96 | echo "Validation Failed!!!"; |
| 97 | } |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 98 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 99 | backup_result=`cat /backup/backup.log` |
| 100 | rm -rf /backup/backup.log |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 101 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 102 | if [ "$backup_result" == "Failed" ]; then |
| 103 | echo "Backup Failed!!! Validation Failed!!!"; |
| 104 | exit 0 |
| 105 | fi |
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 | target_dir=$(ls -td -- /backup/backup-* | head -n 1) |
| 108 | cp -Ra $target_dir/* /var/lib/mysql/ |
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 [ ! "$(ls -A /var/lib/mysql)" ]; then |
| 111 | remove_dir $target_dir |
| 112 | exit 0 |
| 113 | fi |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 114 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 115 | /docker-entrypoint.sh mysqld & |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 116 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 117 | count=0 |
| 118 | until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SELECT 1"; |
| 119 | do sleep 3; |
| 120 | count=`expr $count + 1`; |
| 121 | if [ $count -ge 30 ]; then |
| 122 | remove_dir $target_dir |
| 123 | exit 0; |
| 124 | fi; |
| 125 | done |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 126 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 127 | mysqlcheck -A --user=root --password=$MYSQL_ROOT_PASSWORD > /tmp/output.log |
| 128 | error_lines=`cat /tmp/output.log| grep -v "OK" | wc -l` |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 129 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 130 | cat /tmp/output.log |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 131 | |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 132 | if [ $error_lines -gt 1 ];then |
| 133 | remove_dir $target_dir |
| 134 | else |
| 135 | echo "Validation successful!!!" |
| 136 | cd /backup |
| 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 |
| 141 | fi |
| 142 | fi |
| 143 | resources: {{ include "common.resources" . | nindent 12 }} |
| 144 | volumeMounts: |
| 145 | - mountPath: /etc/localtime |
| 146 | name: localtime |
| 147 | readOnly: true |
| 148 | - name: backup-dir |
| 149 | mountPath: /backup |
| 150 | imagePullSecrets: |
| 151 | - name: {{ include "common.namespace" . }}-docker-registry-key |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 152 | volumes: |
Sylvain Desbureaux | 93a5b49 | 2020-11-27 11:07:42 +0100 | [diff] [blame] | 153 | - name: localtime |
| 154 | hostPath: |
| 155 | path: /etc/localtime |
| 156 | - name: backup-dir |
| 157 | persistentVolumeClaim: |
| 158 | claimName: {{ include "common.fullname" . }}-backup-data |
Akansha Dua | 3fb95ef | 2019-09-04 11:47:43 +0000 | [diff] [blame] | 159 | {{- end }} |