Skip to content

Commit def9c33

Browse files
committed
Create backup functionality.
1 parent 2e52e85 commit def9c33

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function get_current_time() {
2+
curr_time=$(psql -U ${DP_DB_USER} -d halo0root -t -c "SELECT now() AT TIME ZONE 'UTC'")
3+
echo $curr_time
4+
}
5+
6+
function stat_and_save_backup_info() {
7+
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
8+
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
9+
START_TIME=$1
10+
STOP_TIME=$2
11+
if [ -z $STOP_TIME ]; then
12+
STOP_TIME=$(get_current_time)
13+
fi
14+
START_TIME=$(date -d "${START_TIME}" -u '+%Y-%m-%dT%H:%M:%SZ')
15+
STOP_TIME=$(date -d "${STOP_TIME}" -u '+%Y-%m-%dT%H:%M:%SZ')
16+
TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')
17+
echo "{\"totalSize\":\"$TOTAL_SIZE\",\"timeRange\":{\"start\":\"${START_TIME}\",\"end\":\"${STOP_TIME}\"}}" >"${DP_BACKUP_INFO_FILE}"
18+
}
19+
20+
# if the script exits with a non-zero exit code, touch a file to indicate that the backup failed,
21+
# the sync progress container will check this file and exit if it exists
22+
function handle_exit() {
23+
exit_code=$?
24+
if [ $exit_code -ne 0 ]; then
25+
echo "failed with exit code $exit_code"
26+
touch "${DP_BACKUP_INFO_FILE}.exit"
27+
exit 1
28+
fi
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set -e
2+
set -o pipefail
3+
export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
4+
export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
5+
export PGPASSWORD=${DP_DB_PASSWORD}
6+
7+
trap handle_exit EXIT
8+
9+
START_TIME=`get_current_time`
10+
echo ${DP_DB_PASSWORD} | pg_basebackup -Ft -Pv -c fast -Xf -D - -h ${DP_DB_HOST} -U ${DP_DB_USER} -W | datasafed push -z zstd-fastest - "/${DP_BACKUP_NAME}.tar.zst"
11+
12+
# stat and save the backup information
13+
stat_and_save_backup_info $START_TIME
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: dataprotection.kubeblocks.io/v1alpha1
2+
kind: ActionSet
3+
metadata:
4+
name: pg-basebackup
5+
labels:
6+
clusterdefinition.kubeblocks.io/name: halo
7+
{{- include "halo.labels" . | nindent 4 }}
8+
spec:
9+
backupType: Full
10+
env:
11+
- name: DATA_DIR
12+
value: {{ .Values.dataMountPath }}
13+
- name: DP_DB_USER
14+
value: halo
15+
- name: DP_BACKUP_BASE_PATH
16+
value: /u01/app/halo/product/dbms/14/bin
17+
- name: DP_DB_HOST
18+
value: localhost
19+
backup:
20+
preBackup: []
21+
postBackup: []
22+
backupData:
23+
image: {{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}
24+
command:
25+
- bash
26+
- -c
27+
- |
28+
{{- .Files.Get "dataprotection/backup-info-collector.sh" | nindent 8 -}}
29+
{{- .Files.Get "dataprotection/halo-basebackup-backup.sh" | nindent 8 -}}
30+
syncProgress:
31+
enabled: true
32+
intervalSeconds: 5
33+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: apps.kubeblocks.io/v1alpha1
2+
kind: BackupPolicyTemplate
3+
metadata:
4+
name: halo-backup-policy-template
5+
labels:
6+
clusterdefinition.kubeblocks.io/name: halo
7+
{{- include "halo.labels" . | nindent 4 }}
8+
spec:
9+
clusterDefinitionRef: halo
10+
backupPolicies:
11+
- componentDefRef: halo
12+
backupMethods:
13+
- name: pg-basebackup
14+
snapshotVolumes: false
15+
actionSetName: pg-basebackup
16+
targetVolumes:
17+
volumeMounts:
18+
- name: data
19+
mountPath: {{ .Values.dataMountPath }}
20+
schedules:
21+
- backupMethod: pg-basebackup
22+
enabled: false
23+
cronExpression: "*/5 * * * *"
24+
retentionPeriod: 7d
25+

0 commit comments

Comments
 (0)