Skip to content

Commit 0f46e61

Browse files
committed
[HWORKS-2433] Configure cleanup for old backups
1 parent 6d0cca9 commit 0f46e61

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2024-2025 Hopsworks AB. All rights reserved.
4+
5+
set -e
6+
7+
{{- include "rondb.backups.defineBackupIdEnv" . }}
8+
9+
REMOTE_BACKUP_BASE_DIR={{ include "rondb.rcloneBackupRemoteName" . }}:{{ include "rondb.backups.bucketName" (dict "backupConfig" .Values.backups "global" .Values.global) }}/{{ include "rondb.takeBackupPathPrefix" . }}
10+
11+
if [ -z "$TTL" ]; then
12+
echo "No TTL configuration found."
13+
exit 0
14+
fi
15+
16+
echo "Check expired backups in $REMOTE_BACKUP_BASE_DIR with TTL $TTL "
17+
18+
TTL_EXPIRED=$(
19+
rclone lsjson --recursive --files-only "$REMOTE_BACKUP_BASE_DIR" --min-age "$TTL" \
20+
| jq -r '.[].Path | split("/") | .[0]' \
21+
| sort -u
22+
)
23+
24+
if [ -z "$TTL_EXPIRED" ]; then
25+
echo "No TTL expired backups found."
26+
exit 0
27+
fi
28+
29+
echo "TTL expired backups detected:"
30+
echo "$TTL_EXPIRED"
31+
32+
echo "Deleting TTL-expired backups from object storage"
33+
34+
{{- if include "rondb.backups.metadataStore.configMapName" . }}
35+
CONFIGMAPS=$(kubectl get cm -n {{ .Release.Namespace }} \
36+
-l "app=backups-metadata,service=rondb,managed-by=cronjob" \
37+
-o jsonpath='{.items[*].metadata.name}')
38+
{{- end }}
39+
40+
for id in $TTL_EXPIRED; do
41+
if [ "$id" = "$BACKUP_ID" ]; then
42+
echo "Skipping $id since this is the last active backup"
43+
continue
44+
fi
45+
BACKUP_PATH="$REMOTE_BACKUP_BASE_DIR/$id"
46+
echo "Deleting $BACKUP_PATH"
47+
rclone delete -v "$BACKUP_PATH" --rmdirs
48+
49+
{{- if include "rondb.backups.metadataStore.configMapName" . }}
50+
PATCH_JSON="{\"data\": {\"$id\": null}}"
51+
for cm in $CONFIGMAPS; do
52+
echo "Cleaning metadata from ConfigMap: $cm with $PATCH_JSON"
53+
kubectl patch cm "$cm" -n {{ .Release.Namespace }} --type merge -p "$PATCH_JSON" || true
54+
done
55+
{{- end }}
56+
done

templates/backups/create.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ spec:
7171
- name: backup-id
7272
mountPath: /home/hopsworks/backup-id
7373
readOnly: true
74+
{{- if not (include "rondb.backups.ttl" .) }}
7475
containers:
76+
{{- end }}
7577
- name: upload-native-backups
7678
image: {{ include "image_address" (dict "image" $.Values.images.toolbox) }}
7779
imagePullPolicy: {{ $.Values.imagePullPolicy }}
@@ -86,6 +88,32 @@ spec:
8688
- name: backup-id
8789
mountPath: /home/hopsworks/backup-id
8890
readOnly: true
91+
{{- if include "rondb.backups.ttl" . }}
92+
containers:
93+
- name: cleanup-old-backups
94+
image: {{ include "image_address" (dict "image" $.Values.images.toolbox) }}
95+
imagePullPolicy: {{ $.Values.imagePullPolicy }}
96+
{{ include "rondb.ContainerSecurityContext" $ | indent 12 }}
97+
workingDir: /home/hopsworks
98+
command:
99+
- /bin/bash
100+
- -c
101+
- |
102+
{{ tpl (.Files.Get "files/scripts/backups/cleanup_old_backups.sh") . | indent 14 }}
103+
env:
104+
- name: TTL
105+
value: {{ include "rondb.backups.ttl" . }}
106+
- name: RCLONE_CONFIG
107+
value: /home/hopsworks/rclone.conf
108+
{{- include "rondb.backup.credentials" (dict "backupConfig" $.Values.backups "namespace" $.Release.Namespace "global" $.Values.global) | indent 12 }}
109+
volumeMounts:
110+
- name: rclone-configs
111+
mountPath: /home/hopsworks/rclone.conf
112+
subPath: rclone.conf
113+
- name: backup-id
114+
mountPath: /home/hopsworks/backup-id
115+
readOnly: true
116+
{{- end }}
89117
volumes:
90118
- name: rclone-configs
91119
configMap:

templates/shared_templates/_helpers.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,11 @@ endpoint = {{ .global._hopsworks.managedObjectStorage.s3.endpoint }}
528528
{{- "s3:/" -}}
529529
{{- end -}}
530530
{{- end -}}
531+
532+
{{- define "rondb.backups.ttl" -}}
533+
{{- if .Values.backups.ttl -}}
534+
{{- .Values.backups.ttl -}}
535+
{{- else if and (include "rondb.global.backupsEnabled" .) .Values.global._hopsworks.backups.ttl -}}
536+
{{- .Values.global._hopsworks.backups.ttl -}}
537+
{{- end -}}
538+
{{- end -}}

values.schema.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,15 @@
16341634
"description": "The name of the configmap to be used to store the backups metadata information.",
16351635
"type": ["string", "null"],
16361636
"default": null
1637+
},
1638+
"ttl": {
1639+
"default": null,
1640+
"description": "time to live to control when to clean up backups. It is a number followed by either d (days) or h (hours) suffix.",
1641+
"pattern": "^\\d+[dh]$",
1642+
"type": [
1643+
"string",
1644+
"null"
1645+
]
16371646
}
16381647
}
16391648
},

values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ backups:
2222
name: null
2323
serverSideEncryption: null
2424
schedule: null
25+
ttl: null
2526
benchmarking:
2627
dbt2:
2728
numWarehouses: 4

0 commit comments

Comments
 (0)