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.k8ssandraOperator.enabled }}
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
53 - name: "cassandra-backup-init"
54 image: {{ include "repositoryGenerator.image.readiness" . }}
55 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
62 echo "Clearing snapshots!!!"
63 command="nodetool clearsnapshot -t $curr_time"
64 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
72 {{ range $i, $e := until (int .Values.replicaCount) }}
73 target_dir=/backup/temp/cassandra-{{ $i }}
75 cp -Ra /onap-data/cassandra-{{ $i }}/data/ $target_dir/
78 echo "Executing cleanup!!"
79 command="nodetool cleanup"
80 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
81 echo "Cleaned Node!! Backing up database now!!!"
83 command="nodetool snapshot -t $curr_time"
84 /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
86 if [ $retCode -ne 0 ]; then
87 echo "Backup Failed!!!"
89 clearSnapshot $curr_time
90 echo "Failed" > /backup/backup.log
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
100 /app/restore.sh -b $backup_dir/cassandra-{{ $i }}/data -s /onap-data/cassandra-{{ $i }}/data/$keyspace_name -k $keyspace_name -t $curr_time &
108 if [ $? -ne 0 ]; then
110 echo "Creation of Backup Failed!!!"
111 clearSnapshot $curr_time
112 echo "Failed" > /backup/backup.log
117 clearSnapshot $curr_time
120 if [ $exit_code -ne 0 ]; then
122 echo "Backup Failed!!!"
123 echo "Failed" > /backup/backup.log
127 mv /backup/temp /backup/backup-${curr_time}
128 echo "Success" > /backup/backup.log
129 echo "Cassandra Backup Succeeded"
135 fieldPath: metadata.namespace
137 - mountPath: /etc/localtime
140 {{- range $i := until (int .Values.replicaCount)}}
141 - mountPath: /onap-data/cassandra-{{ $i }}
142 name: data-dir-{{ $i }}
147 mountPath: /app/restore.sh
150 mountPath: /app/exec.py
153 - name: cassandra-backup-validate
154 image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.image }}
155 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
165 backup_result=`cat /backup/backup.log`
166 rm -rf /backup/backup.log
168 if [ "$backup_result" == "Failed" ]; then
169 echo "Backup Failed!!! So Validation Failed!!!";
173 target_dir=$(ls -td -- /backup/*/ | head -n 1)
174 chown -R cassandra.cassandra $target_dir
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)!!!"
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 &
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
198 if [ $ret -ne 0 ]; then
199 remove_dir $target_dir
200 echo "Validation Failed!!!"
207 echo "Validation Successful!!!"
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
215 - name: CASSANDRA_CLUSTER_NAME
216 value: {{ .Values.config.clusterName }}
217 - name: MAX_HEAP_SIZE
218 value: {{ .Values.config.heap.max }}
220 value: {{ .Values.config.heap.min }}
224 fieldPath: status.podIP
229 mountPath: /etc/localtime
237 name: {{ include "common.fullname" . }}-configmap
239 {{- range $i := until (int .Values.replicaCount)}}
240 - name: data-dir-{{ $i }}
241 persistentVolumeClaim:
242 claimName: {{ include "common.fullname" . }}-data-{{ $i }}
245 persistentVolumeClaim:
246 claimName: {{ include "common.fullname" . }}-backup-data