aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/common/cassandra/templates/backup/cronjob.yaml
blob: 8773cd3e4bffb41f58c38e5aa93fce5f0febb836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{{/*
# Copyright © 2019 Amdocs, Bell Canada
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/}}
{{- if .Values.backup.enabled }}
{{- if .Values.global.cassandra.useOperator }}
{{ else }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "common.fullname" . }}-backup
  namespace: {{ include "common.namespace" . }}
  labels: {{- include "common.labels" . | nindent 4 }}
spec:
  schedule: {{ .Values.backup.cron | quote }}
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 120
  jobTemplate:
    spec:
      template:
        metadata:
          labels: {{- include "common.labels" . | nindent 12 }}
          annotations: {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" .) | nindent 12 }}
          name: {{ include "common.name" . }}
        spec:
          restartPolicy: Never
          initContainers:
            - command:
              - /app/ready.py
              args:
              - --app-name
              - {{ include "common.name" . }}
              env:
              - name: NAMESPACE
                valueFrom:
                  fieldRef:
                    apiVersion: v1
                    fieldPath: metadata.namespace
              image: {{ include "repositoryGenerator.image.readiness" . }}
              imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
              name: {{ include "common.name" . }}-readiness
              resources:
                limits:
                  cpu: "100m"
                  memory: "0.5Gi"
                requests:
                  cpu: "3m"
                  memory: "0.02Gi"
            - name: "cassandra-backup-init"
              image: {{ include "repositoryGenerator.image.readiness" . }}
              imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
              resources:
                limits:
                  cpu: "100m"
                  memory: "0.5Gi"
                requests:
                  cpu: "3m"
                  memory: "0.02Gi"
              command:
              - /bin/bash
              - -c
              - |
                clearSnapshot(){
                  curr_time=$1
                  echo "Clearing snapshots!!!"
                  command="nodetool clearsnapshot -t $curr_time"
                  /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
                }
                {{ $root := . }}
                curr_time=`date +%s`
                pids=""
                set -x

                echo "Copying data"
                {{ range $i, $e := until (int .Values.replicaCount) }}
                  target_dir=/backup/temp/cassandra-{{ $i }}
                  mkdir -p $target_dir
                  cp -Ra /onap-data/cassandra-{{ $i }}/data/ $target_dir/
                {{- end }}

                echo "Executing cleanup!!"
                command="nodetool cleanup"
                /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
                echo "Cleaned Node!! Backing up database now!!!"

                command="nodetool snapshot -t $curr_time"
                /app/exec.py -p "{{ include "common.name" . }}" -c "$command"
                retCode=$?
                if [ $retCode -ne 0 ]; then
                  echo "Backup Failed!!!"
                  rm -rf /backup/temp
                  clearSnapshot $curr_time
                  echo "Failed" > /backup/backup.log
                  exit 0
                fi

                backup_dir=/backup/temp
                {{ range $i, $e := until (int .Values.replicaCount) }}
                  for d in $backup_dir/cassandra-{{ $i }}/data/*/ ; do
                    d=$(echo $d | sed 's:/*$::')
                    keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }')
                    if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then
                      /app/restore.sh -b $backup_dir/cassandra-{{ $i }}/data -s  /onap-data/cassandra-{{ $i }}/data/$keyspace_name -k $keyspace_name -t $curr_time &
                      pids="$pids $!"
                    fi
                  done
                {{- end }}

                for p in $pids; do
                  wait $p
                  if [ $? -ne 0 ]; then
                    rm -rf /backup/temp
                    echo "Creation of Backup Failed!!!"
                    clearSnapshot $curr_time
                    echo "Failed" > /backup/backup.log
                    exit 0
                  fi
                done

                clearSnapshot $curr_time

                exit_code=$?
                if [ $exit_code -ne 0 ]; then
                  rm -rf /backup/temp
                  echo "Backup Failed!!!"
                  echo "Failed" > /backup/backup.log
                  exit 0
                fi

                mv /backup/temp /backup/backup-${curr_time}
                echo "Success" > /backup/backup.log
                echo "Cassandra Backup Succeeded"
              env:
              - name: NAMESPACE
                valueFrom:
                  fieldRef:
                    apiVersion: v1
                    fieldPath: metadata.namespace
              volumeMounts:
              - mountPath: /etc/localtime
                name: localtime
                readOnly: true
              {{- range $i := until (int .Values.replicaCount)}}
              - mountPath: /onap-data/cassandra-{{ $i }}
                name: data-dir-{{ $i }}
              {{- end }}
              - mountPath: /backup
                name: backup-dir
              - name: scripts
                mountPath: /app/restore.sh
                subPath: restore.sh
              - name: scripts
                mountPath: /app/exec.py
                subPath: exec.py
          containers:
            - name: cassandra-backup-validate
              image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.image }}
              imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
              command:
              - /bin/bash
              - -c
              - |
                remove_dir(){
                  dirToRemove=$1
                  rm -rf $dirToRemove
                }

                backup_result=`cat /backup/backup.log`
                rm -rf /backup/backup.log

                if [ "$backup_result" == "Failed" ]; then
                  echo "Backup Failed!!! So Validation Failed!!!";
                  exit 0
                fi

                target_dir=$(ls -td -- /backup/*/ | head -n 1)
                chown -R cassandra.cassandra $target_dir
                {{- $root := . -}}
                {{ range $i, $e := until (int .Values.replicaCount) }}
                  dbSize=$(du -ks $target_dir/cassandra-{{ $i }}/data|awk -F " " '{ printf $1 }')
                  minDbSize={{ (int $root.Values.backup.dbSize) }}
                  if [ $dbSize -lt $minDbSize ]; then
                    remove_dir $target_dir
                    echo "Validation Failed!!! dbSize ($dbSize) is less than minimum size (1)!!!"
                    exit 0
                  fi
                  rm -rf /var/lib/cassandra/*
                  cp -Ra $target_dir/cassandra-{{ $i }}/data /var/lib/cassandra
                  export CASSANDRA_LISTEN_ADDRESS="127.0.0.1"
                  /docker-entrypoint.sh -Dcassandra.ignore_dc=true -Dcassandra.ignore_rack=true &
                  CASS_PID=$!
                  sleep 45

                  for d in $target_dir/cassandra-{{ $i }}/data/*/; do
                      d=$(echo $d | sed 's:/*$::')
                      keyspace_name=$(echo "$d" | awk -F/ '{ print $NF }')
                      if [ 1 ] {{- range $t, $keyspace := $root.Values.backup.keyspacesToSkip }} && [ "{{ $keyspace.name }}" != "$keyspace_name" ] {{- end }}; then
                        echo "Verifying the data for  $keyspace_name "
                        nodetool verify -e $keyspace_name
                        ret=$?
                        if [ $ret -ne 0 ]; then
                          remove_dir $target_dir
                          echo "Validation Failed!!!"
                          exit 0
                        fi
                      fi
                  done
                  kill -9 $CASS_PID
                {{- end }}
                echo "Validation Successful!!!"
                cd /backup
                totalFiles=`ls -t | grep "backup-" | wc -l`
                if [ $totalFiles -gt {{ .Values.backup.retentionPeriod }} ]; then
                  filestoDelete=`expr $totalFiles - {{ .Values.backup.retentionPeriod }}`
                  ls -tr | grep backup | head -$filestoDelete | xargs rm -rf
                fi
              env:
              - name: CASSANDRA_CLUSTER_NAME
                value: {{ .Values.config.clusterName }}
              - name: MAX_HEAP_SIZE
                value: {{ .Values.config.heap.max }}
              - name: HEAP_NEWSIZE
                value: {{ .Values.config.heap.min }}
              - name: HOST_IP
                valueFrom:
                  fieldRef:
                    fieldPath: status.podIP
              volumeMounts:
              - name: backup-dir
                mountPath: /backup
              - name: localtime
                mountPath: /etc/localtime
                readOnly: true
          volumes:
          - name: localtime
            hostPath:
              path: /etc/localtime
          - name: scripts
            configMap:
              name: {{ include "common.fullname" . }}-configmap
              defaultMode: 0755
          {{- range $i := until (int .Values.replicaCount)}}
          - name: data-dir-{{ $i }}
            persistentVolumeClaim:
              claimName: {{ include "common.fullname" . }}-data-{{ $i }}
          {{- end }}
          - name: backup-dir
            persistentVolumeClaim:
              claimName: {{ include "common.fullname" . }}-backup-data
{{- end -}}
{{- end -}}