Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 1 | {{/* |
| 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 }} |
dasarathi528024 | 65e8f78 | 2023-05-03 09:51:20 +0000 | [diff] [blame] | 17 | {{- if .Values.k8ssandraOperator.enabled }} |
| 18 | {{ else }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +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 | 60c7480 | 2019-12-12 14:35:01 +0100 | [diff] [blame] | 24 | labels: {{- include "common.labels" . | nindent 4 }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 25 | spec: |
| 26 | schedule: {{ .Values.backup.cron | quote }} |
| 27 | concurrencyPolicy: Forbid |
| 28 | startingDeadlineSeconds: 120 |
| 29 | jobTemplate: |
| 30 | spec: |
| 31 | template: |
Sylvain Desbureaux | 60c7480 | 2019-12-12 14:35:01 +0100 | [diff] [blame] | 32 | metadata: |
| 33 | labels: {{- include "common.labels" . | nindent 12 }} |
| 34 | annotations: {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" .) | nindent 12 }} |
| 35 | name: {{ include "common.name" . }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 36 | spec: |
| 37 | restartPolicy: Never |
| 38 | initContainers: |
| 39 | - command: |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 40 | - /app/ready.py |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 41 | args: |
| 42 | - --container-name |
| 43 | - {{ include "common.name" . }} |
| 44 | env: |
| 45 | - name: NAMESPACE |
| 46 | valueFrom: |
| 47 | fieldRef: |
| 48 | apiVersion: v1 |
| 49 | fieldPath: metadata.namespace |
Sylvain Desbureaux | 7743d8b | 2020-11-19 16:45:23 +0100 | [diff] [blame] | 50 | image: {{ include "repositoryGenerator.image.readiness" . }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 51 | imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} |
| 52 | name: {{ include "common.name" . }}-readiness |
| 53 | - name: "cassandra-backup-init" |
Sylvain Desbureaux | 7743d8b | 2020-11-19 16:45:23 +0100 | [diff] [blame] | 54 | image: {{ include "repositoryGenerator.image.readiness" . }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 55 | imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} |
| 56 | command: |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 57 | - /bin/bash |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 58 | - -c |
| 59 | - | |
| 60 | clearSnapshot(){ |
| 61 | curr_time=$1 |
| 62 | echo "Clearing snapshots!!!" |
| 63 | command="nodetool clearsnapshot -t $curr_time" |
Marat Salakhutdinov | bc10e22 | 2020-10-13 19:22:15 -0400 | [diff] [blame] | 64 | /app/exec.py -p "{{ include "common.name" . }}" -c "$command" |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 65 | } |
| 66 | {{ $root := . }} |
| 67 | curr_time=`date +%s` |
| 68 | pids="" |
| 69 | set -x |
| 70 | |
| 71 | echo "Copying data" |
| 72 | {{ range $i, $e := until (int .Values.replicaCount) }} |
| 73 | target_dir=/backup/temp/cassandra-{{ $i }} |
| 74 | mkdir -p $target_dir |
| 75 | cp -Ra /onap-data/cassandra-{{ $i }}/data/ $target_dir/ |
| 76 | {{- end }} |
| 77 | |
| 78 | echo "Executing cleanup!!" |
| 79 | command="nodetool cleanup" |
Marat Salakhutdinov | bc10e22 | 2020-10-13 19:22:15 -0400 | [diff] [blame] | 80 | /app/exec.py -p "{{ include "common.name" . }}" -c "$command" |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 81 | echo "Cleaned Node!! Backing up database now!!!" |
| 82 | |
| 83 | command="nodetool snapshot -t $curr_time" |
Marat Salakhutdinov | bc10e22 | 2020-10-13 19:22:15 -0400 | [diff] [blame] | 84 | /app/exec.py -p "{{ include "common.name" . }}" -c "$command" |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 85 | retCode=$? |
| 86 | if [ $retCode -ne 0 ]; then |
| 87 | echo "Backup Failed!!!" |
| 88 | rm -rf /backup/temp |
| 89 | clearSnapshot $curr_time |
| 90 | echo "Failed" > /backup/backup.log |
| 91 | exit 0 |
| 92 | fi |
| 93 | |
| 94 | backup_dir=/backup/temp |
| 95 | {{ range $i, $e := until (int .Values.replicaCount) }} |
| 96 | for d in $backup_dir/cassandra-{{ $i }}/data/*/ ; do |
| 97 | d=$(echo $d | sed 's:/*$::') |
| 98 | keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }') |
| 99 | if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 100 | /app/restore.sh -b $backup_dir/cassandra-{{ $i }}/data -s /onap-data/cassandra-{{ $i }}/data/$keyspace_name -k $keyspace_name -t $curr_time & |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 101 | pids="$pids $!" |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 102 | fi |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 103 | done |
| 104 | {{- end }} |
| 105 | |
| 106 | for p in $pids; do |
| 107 | wait $p |
| 108 | if [ $? -ne 0 ]; then |
| 109 | rm -rf /backup/temp |
| 110 | echo "Creation of Backup Failed!!!" |
| 111 | clearSnapshot $curr_time |
| 112 | echo "Failed" > /backup/backup.log |
| 113 | exit 0 |
| 114 | fi |
| 115 | done |
| 116 | |
| 117 | clearSnapshot $curr_time |
| 118 | |
| 119 | exit_code=$? |
| 120 | if [ $exit_code -ne 0 ]; then |
| 121 | rm -rf /backup/temp |
| 122 | echo "Backup Failed!!!" |
| 123 | echo "Failed" > /backup/backup.log |
| 124 | exit 0 |
| 125 | fi |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 126 | |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 127 | mv /backup/temp /backup/backup-${curr_time} |
| 128 | echo "Success" > /backup/backup.log |
| 129 | echo "Cassandra Backup Succeeded" |
| 130 | env: |
| 131 | - name: NAMESPACE |
| 132 | valueFrom: |
| 133 | fieldRef: |
| 134 | apiVersion: v1 |
| 135 | fieldPath: metadata.namespace |
| 136 | volumeMounts: |
| 137 | - mountPath: /etc/localtime |
| 138 | name: localtime |
| 139 | readOnly: true |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 140 | {{- range $i := until (int .Values.replicaCount)}} |
| 141 | - mountPath: /onap-data/cassandra-{{ $i }} |
| 142 | name: data-dir-{{ $i }} |
| 143 | {{- end }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 144 | - mountPath: /backup |
| 145 | name: backup-dir |
| 146 | - name: scripts |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 147 | mountPath: /app/restore.sh |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 148 | subPath: restore.sh |
| 149 | - name: scripts |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 150 | mountPath: /app/exec.py |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 151 | subPath: exec.py |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 152 | containers: |
| 153 | - name: cassandra-backup-validate |
Sylvain Desbureaux | 7743d8b | 2020-11-19 16:45:23 +0100 | [diff] [blame] | 154 | image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.image }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 155 | imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} |
| 156 | command: |
| 157 | - /bin/bash |
| 158 | - -c |
| 159 | - | |
| 160 | remove_dir(){ |
| 161 | dirToRemove=$1 |
| 162 | rm -rf $dirToRemove |
| 163 | } |
| 164 | |
| 165 | backup_result=`cat /backup/backup.log` |
| 166 | rm -rf /backup/backup.log |
| 167 | |
| 168 | if [ "$backup_result" == "Failed" ]; then |
| 169 | echo "Backup Failed!!! So Validation Failed!!!"; |
| 170 | exit 0 |
| 171 | fi |
| 172 | |
| 173 | target_dir=$(ls -td -- /backup/*/ | head -n 1) |
| 174 | chown -R cassandra.cassandra $target_dir |
| 175 | {{- $root := . -}} |
| 176 | {{ range $i, $e := until (int .Values.replicaCount) }} |
| 177 | dbSize=$(du -ks $target_dir/cassandra-{{ $i }}/data|awk -F " " '{ printf $1 }') |
| 178 | minDbSize={{ (int $root.Values.backup.dbSize) }} |
| 179 | if [ $dbSize -lt $minDbSize ]; then |
| 180 | remove_dir $target_dir |
| 181 | echo "Validation Failed!!! dbSize ($dbSize) is less than minimum size (1)!!!" |
| 182 | exit 0 |
| 183 | fi |
| 184 | rm -rf /var/lib/cassandra/* |
| 185 | cp -Ra $target_dir/cassandra-{{ $i }}/data /var/lib/cassandra |
| 186 | export CASSANDRA_LISTEN_ADDRESS="127.0.0.1" |
| 187 | /docker-entrypoint.sh -Dcassandra.ignore_dc=true -Dcassandra.ignore_rack=true & |
| 188 | CASS_PID=$! |
| 189 | sleep 45 |
| 190 | |
| 191 | for d in $target_dir/cassandra-{{ $i }}/data/*/; do |
| 192 | d=$(echo $d | sed 's:/*$::') |
| 193 | keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }') |
| 194 | if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then |
| 195 | echo "Verifying the data for $keyspace_name " |
| 196 | nodetool verify -e $keyspace_name |
| 197 | ret=$? |
| 198 | if [ $ret -ne 0 ]; then |
| 199 | remove_dir $target_dir |
| 200 | echo "Validation Failed!!!" |
| 201 | exit 0 |
| 202 | fi |
| 203 | fi |
| 204 | done |
| 205 | kill -9 $CASS_PID |
| 206 | {{- end }} |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 207 | echo "Validation Successful!!!" |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 208 | cd /backup |
| 209 | totalFiles=`ls -t | grep "backup-" | wc -l` |
| 210 | if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then |
| 211 | filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}` |
| 212 | ls -tr | grep backup | head -$filestoDelete | xargs rm -rf |
| 213 | fi |
| 214 | env: |
| 215 | - name: CASSANDRA_CLUSTER_NAME |
| 216 | value: {{ .Values.config.clusterName }} |
| 217 | - name: MAX_HEAP_SIZE |
| 218 | value: {{ .Values.config.heap.max }} |
| 219 | - name: HEAP_NEWSIZE |
| 220 | value: {{ .Values.config.heap.min }} |
| 221 | - name: HOST_IP |
| 222 | valueFrom: |
| 223 | fieldRef: |
| 224 | fieldPath: status.podIP |
| 225 | volumeMounts: |
| 226 | - name: backup-dir |
| 227 | mountPath: /backup |
| 228 | - name: localtime |
| 229 | mountPath: /etc/localtime |
| 230 | readOnly: true |
| 231 | volumes: |
| 232 | - name: localtime |
| 233 | hostPath: |
| 234 | path: /etc/localtime |
| 235 | - name: scripts |
| 236 | configMap: |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 237 | name: {{ include "common.fullname" . }}-configmap |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 238 | defaultMode: 0755 |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 239 | {{- range $i := until (int .Values.replicaCount)}} |
| 240 | - name: data-dir-{{ $i }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 241 | persistentVolumeClaim: |
Sylvain Desbureaux | 1694e1d | 2020-08-21 09:58:25 +0200 | [diff] [blame] | 242 | claimName: {{ include "common.fullname" . }}-data-{{ $i }} |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 243 | {{- end }} |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 244 | - name: backup-dir |
| 245 | persistentVolumeClaim: |
| 246 | claimName: {{ include "common.fullname" . }}-backup-data |
| 247 | {{- end -}} |
dasarathi528024 | 65e8f78 | 2023-05-03 09:51:20 +0000 | [diff] [blame] | 248 | {{- end -}} |