Skip to content

Commit

Permalink
chore: support to rebuild redis instance (#1417)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2b5ae1c)
  • Loading branch information
wangyelei committed Jan 20, 2025
1 parent 8f3192f commit 86dd978
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
26 changes: 26 additions & 0 deletions addons/redis/dataprotection/backup_for_rebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -e
set -o pipefail

# if the script exits with a non-zero exit code, touch a file to indicate that the backup failed,
# the sync progress container will check this file and exit if it exists
function handle_exit() {
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "failed with exit code $exit_code"
touch "${DP_BACKUP_INFO_FILE}.exit"
exit 1
fi
}
trap handle_exit EXIT

export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
cd ${DATA_DIR}
if [ -f ./nodes.conf ]; then
datasafed push ./nodes.conf "nodes.conf"
fi
datasafed push ./users.acl "users.acl"
echo "INFO: save data file successfully"
TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')
echo "{\"totalSize\":\"$TOTAL_SIZE\"}" >"${DP_BACKUP_INFO_FILE}" && sync
20 changes: 20 additions & 0 deletions addons/redis/dataprotection/restore-cluster-users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -e
set -o pipefail
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
mkdir -p ${DATA_DIR}
res=`find ${DATA_DIR} -type f`
data_protection_file=${DATA_DIR}/.kb-data-protection
if [ ! -z "${res}" ] && [ ! -f ${data_protection_file} ]; then
echo "${DATA_DIR} is not empty! Please make sure that the directory is empty before restoring the backup."
exit 1
fi
# touch placeholder file
touch ${data_protection_file}
cd ${DATA_DIR}
datasafed pull "users.acl" "users.acl"
if [ "${REBUILD_CLUSTER_INSTANCE}" == "true" ]; then
touch rebuild.flag
fi
rm -rf ${data_protection_file} && sync
44 changes: 43 additions & 1 deletion addons/redis/templates/backupactionset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ spec:
- |
{{- .Files.Get "dataprotection/backup.sh" | nindent 8 }}
restore:
prepareData:
image: {{ include "redis7.image" . }}
command:
- bash
- -c
- |
{{- .Files.Get "dataprotection/restore-cluster-users.sh" | nindent 8 }}
postReady:
- job:
command:
Expand Down Expand Up @@ -141,4 +148,39 @@ spec:
image: {{ include "redis7.image" . }}
onError: Fail
runOnTargetPodNode: true
baseBackupRequired: false
baseBackupRequired: false
---
apiVersion: dataprotection.kubeblocks.io/v1alpha1
kind: ActionSet
metadata:
name: redis-for-rebuild-instance
labels:
clusterdefinition.kubeblocks.io/name: redis
{{- include "redis.labels" . | nindent 4 }}
spec:
backupType: Full
env:
- name: DATA_DIR
value: {{ .Values.dataMountPath }}
- name: REBUILD_CLUSTER_INSTANCE
value: "false"
backup:
backupData:
image: {{ include "busybox.image" . }}
runOnTargetPodNode: true
syncProgress:
enabled: true
intervalSeconds: 5
command:
- sh
- -c
- |
{{- .Files.Get "dataprotection/backup_for_rebuild.sh" | nindent 8 }}
restore:
prepareData:
image: {{ include "busybox.image" . }}
command:
- sh
- -c
- |
{{- .Files.Get "dataprotection/restore-cluster-users.sh" | nindent 8 }}
6 changes: 6 additions & 0 deletions addons/redis/templates/backuppolicytemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ spec:
targetVolumes:
volumes:
- data
- name: backup-for-rebuild-instance
actionSetName: redis-for-rebuild-instance
targetVolumes:
volumeMounts:
- name: data
mountPath: {{ .Values.dataMountPath }}
schedules:
- backupMethod: datafile
enabled: false
Expand Down
9 changes: 9 additions & 0 deletions addons/redis/templates/backuppolicytemplateforcluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ spec:
volumeMounts:
- name: data
mountPath: {{ .Values.dataMountPath }}
- name: backup-for-rebuild-instance
actionSetName: redis-for-rebuild-instance
env:
- name: REBUILD_CLUSTER_INSTANCE
value: "true"
targetVolumes:
volumeMounts:
- name: data
mountPath: {{ .Values.dataMountPath }}
# TODO: support it when kb support to input the specified component name for sharding cluster.
# - name: volume-snapshot
# snapshotVolumes: true
Expand Down

0 comments on commit 86dd978

Please sign in to comment.