Skip to content

Commit

Permalink
feat:finish backup and restore (#2968)
Browse files Browse the repository at this point in the history
* backup and restore

* delete sh

* modify config
  • Loading branch information
buzhimingyonghu authored Dec 9, 2024
1 parent 937ebde commit 713e6fb
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 28 deletions.
9 changes: 9 additions & 0 deletions tools/kubeblocks_helm/BackupRepo_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
kbcli backuprepo create my-repo \
--provider s3 \
--region ${S3_REGION} \
--bucket ${S3_BUCKET} \
--endpoint ${S3_ENDPOINT} \
--access-key-id ${S3_ACCESS_KEY} \
--secret-access-key ${S3_SECRET_KEY} \
--access-method ${S3_ACCESS_METHOD} \
--default
15 changes: 15 additions & 0 deletions tools/kubeblocks_helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@ redis-cli -p 9221
```bash
helm uninstall pika-master-slave-cluster
helm uninstall pika-master-slave
```
### Back up and restore a cluster
Ensure that the default BackupRepo is defined
Fellow the kubeblock docs [kubeblocks](https://www.kubeblocks.io/docs/preview/user_docs/maintenance/backup-and-restore/backup/backup-repo)
create backup
```bash
kbcli cluster backup pika-master-slave-cluster --method datafile
```
Select a backup and create a cluster.
```bash
kbcli cluster restore <clusterName> --backup <backup-name>
```
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
helm uninstall pika && helm uninstall pika-cluster
sleep 5
helm install pika ./pika && helm install pika-cluster ./pika-cluster
1 change: 1 addition & 0 deletions tools/kubeblocks_helm/install_ms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
helm install pika-master-slave ./pika-master-slave && helm install pika-master-slave-cluster ./pika-master-slave-cluster
4 changes: 0 additions & 4 deletions tools/kubeblocks_helm/installdebug.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ metadata:
labels:
{{ include "pika-cluster.labels" . | nindent 4 }}
rules:
- apiGroups:
- ""
- apiGroups: [""]
resources:
- events
verbs:
- create
- apiGroups: ["apps.kubeblocks.io"]
resources:
- clusters
verbs:
- get
- list
- watch
- apiGroups: [""]
resources:
- pods
verbs:
- get
- list
- watch
39 changes: 39 additions & 0 deletions tools/kubeblocks_helm/pika-master-slave/dataprotection/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
set -o pipefail
function handle_exit() {
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "failed with exit code $exit_code"
exit 1
fi
}
trap handle_exit EXIT
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"

connect_url="redis-cli -h ${DP_DB_HOST} -p ${DP_DB_PORT}"
last_save=$(${connect_url} LASTSAVE)

echo "INFO: start BGSAVE"
${connect_url} BGSAVE

echo "INFO: wait for saving dump successfully"
while true; do
end_save=$(${connect_url} LASTSAVE)
if [ $end_save -ne $last_save ];then
break
fi
sleep 1
done

cd ${DATA_DIR}

if [ -d "log" ] || [ -d "db" ]; then
tar -cvf - ./log ./db | datasafed push -z zstd-fastest - "${DP_BACKUP_NAME}.tar.zst"
else
echo "no log db"
exit 1
fi
echo "INFO: save data file successfully"
TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')
echo "{\"totalSize\":\"$TOTAL_SIZE\"}" >"${DP_BACKUP_INFO_FILE}" && sync
21 changes: 21 additions & 0 deletions tools/kubeblocks_helm/pika-master-slave/dataprotection/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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}

backupFile="${DP_BACKUP_NAME}.tar.zst"
if [ "$(datasafed list ${backupFile})" == "${backupFile}" ]; then
datasafed pull -d zstd-fastest "${backupFile}" - | tar -xvf - -C ${DATA_DIR}
else
datasafed pull "${DP_BACKUP_NAME}.tar.gz" - | tar -xzvf - -C ${DATA_DIR}
fi
rm -rf ${data_protection_file} && sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: dataprotection.kubeblocks.io/v1alpha1
kind: ActionSet
metadata:
name: pika-backup
labels:
clusterdefinition.kubeblocks.io/name: pika-master-slave-cluster
{{- include "pika.labels" . | nindent 4 }}
spec:
backupType: Full
env:
- name: DATA_DIR
value: /data
- name: DP_DB_PORT
value: "9221"
backup:
preBackup: []
postBackup: []
backupData:
image: {{ include "redis.image" . }}
runOnTargetPodNode: true
syncProgress:
enabled: true
intervalSeconds: 5
command:
- bash
- -c
- |
{{- .Files.Get "dataprotection/backup.sh" | nindent 8 }}
restore:
prepareData:
image: {{ include "redis.image" . }}
command:
- bash
- -c
- |
{{- .Files.Get "dataprotection/restore.sh" | nindent 8 }}
postReady: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps.kubeblocks.io/v1alpha1
kind: BackupPolicyTemplate
metadata:
name: pika-backup-policy-template
labels:
clusterdefinition.kubeblocks.io/name: pika-master-slave-cluster
{{- include "pika.labels" . | nindent 4 }}
spec:
backupPolicies:
- componentDefs:
- pika
target:
role: master
backupMethods:
- name: datafile
snapshotVolumes: false
actionSetName: pika-backup
targetVolumes:
volumeMounts:
- name: data
mountPath: /data
schedules:
- backupMethod: datafile
enabled: true
cronExpression: "0 18 * * 0"
retentionPeriod: 3m
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ spec:
volumeName: config
volumes:
- name: data
roles:
- name: master
serviceable: true
writable: true
- name: slave
serviceable: false
writable: false
lifecycleActions:
postProvision:
roleProbe:
builtinHandler: custom
customHandler:
image: {{ include "redis.image" .}}
exec:
command:
- /bin/bash
- -c
- |
IFS=',' read -r -a pod_name_array <<< ${KB_CLUSTER_COMPONENT_POD_NAME_LIST}
# Ergodic name array
headless=".pika-master-slave-cluster-pika-headless"
for i in "${!pod_name_array[@]}"; do
pod_name=${pod_name_array[${i}]}
# Extracting the digital portion of the pod
pod_number=$(echo "${pod_name}" | grep -o '[0-9]*')
# If the pod number is greater than 0
if (( pod_number > 0 )); then
pod0_svc=${pod_name_array[0]}${headless}
echo "exec:redis-cli -h ${pod_name}${headless} -p 9221 slaveof ${pod0_svc}:9221"
redis-cli -h "${pod_name}${headless}" -p 9221 slaveof "${pod0_svc}" 9221
fi
done
preCondition: ComponentReady
- /bin/bash
- -c
- |
replication_info=$(redis-cli -h 127.0.0.1 -p 9221 info replication)
role=$(echo "${replication_info}" | awk -F':' '/^role:/ {print $2}' | tr -d '[:space:]')
if [ "$role"x = "master"x ]; then
echo -n "master"
else
echo -n "slave"
fi
periodSeconds: 1
timeoutSeconds: 1
runtime:
initContainers:
- name: init-config
Expand Down Expand Up @@ -86,4 +88,26 @@ spec:
- "/pika/bin/pika"
args:
- "-c"
- "/data/pika.conf"
- "/data/pika.conf"
- name: redis-cli-runner
image: {{ include "redis.image" . }}
imagePullPolicy: {{ include "pika.imagePullPolicy" . }}
command:
- /bin/bash
- -c
- |
while true; do
hostname=$KB_POD_NAME
if [[ "$hostname" =~ -([0-9]+)$ ]]; then
ordinal=${BASH_REMATCH[1]}
if [[ $ordinal -ge 1 && $ordinal -le 9 ]]; then
replication_info=$(redis-cli -h 127.0.0.1 -p 9221 info replication)
role=$(echo "${replication_info}" | awk -F':' '/^role:/ {print $2}' | tr -d '[:space:]')
if [ "$role"x = "master"x ]; then
redis-cli -h 127.0.0.1 -p 9221 slaveof pika-master-slave-cluster-pika-0.pika-master-slave-cluster-pika-headless 9221
fi
fi
fi
sleep 10
done
1 change: 1 addition & 0 deletions tools/kubeblocks_helm/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
helm uninstall pika-cluster && helm uninstall pika
1 change: 1 addition & 0 deletions tools/kubeblocks_helm/uninstall_ms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
helm uninstall pika-master-slave-cluster && helm uninstall pika-master-slave

0 comments on commit 713e6fb

Please sign in to comment.