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