2 # Copyright © 2019 Amdocs, Bell Canada
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 {{- if .Values.backup.enabled }}
17 {{- if .Values.global.cassandra.useOperator }}
19 apiVersion: batch/v1beta1
22 name: {{ include "common.fullname" . }}-backup
23 namespace: {{ include "common.namespace" . }}
24 labels: {{- include "common.labels" . | nindent 4 }}
26 schedule: {{ .Values.backup.cron | quote }}
27 concurrencyPolicy: Forbid
28 startingDeadlineSeconds: 120
33 labels: {{- include "common.labels" . | nindent 12 }}
34 annotations: {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" .) | nindent 12 }}
35 name: {{ include "common.name" . }}
43 - {{ include "common.name" . }}
49 fieldPath: metadata.namespace
50 image: {{ include "repositoryGenerator.image.readiness" . }}
51 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
52 name: {{ include "common.name" . }}-readiness
60 - name: "cassandra-backup-init"
61 image: {{ include "repositoryGenerator.image.readiness" . }}
62 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
76 echo "Clearing snapshots!!!"
77 command="nodetool clearsnapshot -t $curr_time"
78 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
86 {{ range $i, $e := until (int .Values.replicaCount) }}
87 target_dir=/backup/temp/cassandra-{{ $i }}
89 cp -Ra /onap-data/cassandra-{{ $i }}/data/ $target_dir/
92 echo "Executing cleanup!!"
93 command="nodetool cleanup"
94 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
95 echo "Cleaned Node!! Backing up database now!!!"
97 command="nodetool snapshot -t $curr_time"
98 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
100 if [ $retCode -ne 0 ]; then
101 echo "Backup Failed!!!"
103 clearSnapshot $curr_time
104 echo "Failed" > /backup/backup.log
108 backup_dir=/backup/temp
109 {{ range $i, $e := until (int .Values.replicaCount) }}
110 for d in $backup_dir/cassandra-{{ $i }}/data/*/ ; do
111 d=$(echo $d | sed 's:/*$::')
112 keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }')
113 if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then
114 /app/restore.sh -b $backup_dir/cassandra-{{ $i }}/data -s /onap-data/cassandra-{{ $i }}/data/$keyspace_name -k $keyspace_name -t $curr_time &
122 if [ $? -ne 0 ]; then
124 echo "Creation of Backup Failed!!!"
125 clearSnapshot $curr_time
126 echo "Failed" > /backup/backup.log
131 clearSnapshot $curr_time
134 if [ $exit_code -ne 0 ]; then
136 echo "Backup Failed!!!"
137 echo "Failed" > /backup/backup.log
141 mv /backup/temp /backup/backup-${curr_time}
142 echo "Success" > /backup/backup.log
143 echo "Cassandra Backup Succeeded"
149 fieldPath: metadata.namespace
151 {{- range $i := until (int .Values.replicaCount)}}
152 - mountPath: /onap-data/cassandra-{{ $i }}
153 name: data-dir-{{ $i }}
158 mountPath: /app/restore.sh
161 mountPath: /app/exec.py
164 - name: cassandra-backup-validate
165 image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.image }}
166 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
176 backup_result=`cat /backup/backup.log`
177 rm -rf /backup/backup.log
179 if [ "$backup_result" == "Failed" ]; then
180 echo "Backup Failed!!! So Validation Failed!!!";
184 target_dir=$(ls -td -- /backup/*/ | head -n 1)
185 chown -R cassandra.cassandra $target_dir
187 {{ range $i, $e := until (int .Values.replicaCount) }}
188 dbSize=$(du -ks $target_dir/cassandra-{{ $i }}/data|awk -F " " '{ printf $1 }')
189 minDbSize={{ (int $root.Values.backup.dbSize) }}
190 if [ $dbSize -lt $minDbSize ]; then
191 remove_dir $target_dir
192 echo "Validation Failed!!! dbSize ($dbSize) is less than minimum size (1)!!!"
195 rm -rf /var/lib/cassandra/*
196 cp -Ra $target_dir/cassandra-{{ $i }}/data /var/lib/cassandra
197 export CASSANDRA_LISTEN_ADDRESS="127.0.0.1"
198 /docker-entrypoint.sh -Dcassandra.ignore_dc=true -Dcassandra.ignore_rack=true &
202 for d in $target_dir/cassandra-{{ $i }}/data/*/; do
203 d=$(echo $d | sed 's:/*$::')
204 keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }')
205 if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then
206 echo "Verifying the data for $keyspace_name "
207 nodetool verify -e $keyspace_name
209 if [ $ret -ne 0 ]; then
210 remove_dir $target_dir
211 echo "Validation Failed!!!"
218 echo "Validation Successful!!!"
220 totalFiles=`ls -t | grep "backup-" | wc -l`
221 if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
222 filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
223 ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
226 - name: CASSANDRA_CLUSTER_NAME
227 value: {{ .Values.config.clusterName }}
228 - name: MAX_HEAP_SIZE
229 value: {{ .Values.config.heap.max }}
231 value: {{ .Values.config.heap.min }}
235 fieldPath: status.podIP
242 name: {{ include "common.fullname" . }}-configmap
244 {{- range $i := until (int .Values.replicaCount)}}
245 - name: data-dir-{{ $i }}
246 persistentVolumeClaim:
247 claimName: {{ include "common.fullname" . }}-data-{{ $i }}
250 persistentVolumeClaim:
251 claimName: {{ include "common.fullname" . }}-backup-data