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