Skip to content

Commit 215f6d7

Browse files
authored
chore(postgresql-cluster): add postgresql-logicalbackup
1 parent 59d2c89 commit 215f6d7

7 files changed

Lines changed: 159 additions & 1 deletion

File tree

addons/postgresql-cluster/15/chart/postgresql-cluster/templates/_helpers.tpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ Return true if a cronjob object should be created for Postgresql HA patroni ## T
5252
{{- end -}}
5353
{{- end -}}
5454

55+
{{/*
56+
Return true if a cronjob object should be created for Postgresql HA patroni ## TODO feature
57+
*/}}
58+
{{- define "patroni.createLogicalBackupCronJob" -}}
59+
{{- if and .Values.logicalbackup.enabled }}
60+
{{- true -}}
61+
{{- else -}}
62+
{{- end -}}
63+
{{- end -}}
64+
5565
{{/*
5666
Return true if a configmap object should be created for Postgresql HA patroni
5767
*/}}

addons/postgresql-cluster/15/chart/postgresql-cluster/templates/cm-backup.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ metadata:
1414
data:
1515
backup.env: |-
1616
{{- include "common.tplvalues.render" ( dict "value" .Values.backupEnv "context" $ ) | nindent 4 }}
17-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{- if (include "patroni.createLogicalBackupCronJob" .) }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: {{ template "common.names.fullname" . }}-logicalbackup
6+
namespace: {{ include "common.names.namespace" . | quote }}
7+
labels: {{- include "common.labels.standard" . | nindent 4 }}
8+
{{- if .Values.commonLabels }}
9+
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
10+
{{- end }}
11+
cluster-name: {{ template "patroni.fullname" . }}
12+
{{- if .Values.commonAnnotations }}
13+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
14+
{{- end }}
15+
data:
16+
logicalbackup.sh: |-
17+
{{- include "common.tplvalues.render" ( dict "value" .Values.logicalbackupScript "context" $ ) | nindent 4 }}
18+
19+
{{- end }}

addons/postgresql-cluster/15/chart/postgresql-cluster/templates/cm -postgresql.yaml renamed to addons/postgresql-cluster/15/chart/postgresql-cluster/templates/cm-postgresql.yaml

File renamed without changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{{- /*
2+
Copyright Drycc Community.
3+
SPDX-License-Identifier: APACHE-2.0
4+
*/}}
5+
{{- if (include "patroni.createLogicalBackupCronJob" .) }}
6+
apiVersion: {{ include "common.capabilities.cronjob.apiVersion" . }}
7+
kind: CronJob
8+
metadata:
9+
name: {{ include "patroni.fullname" . }}-logicalbackup
10+
namespace: {{ include "common.names.namespace" . | quote }}
11+
labels: {{- include "common.labels.standard" . | nindent 4 }}
12+
{{- if .Values.commonLabels }}
13+
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
14+
{{- end }}
15+
application: {{ template "patroni.fullname" . }}
16+
cluster-name: {{ template "patroni.fullname" . }}
17+
{{- if .Values.commonAnnotations }}
18+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
19+
{{- end }}
20+
21+
spec:
22+
schedule: "{{ .Values.logicalbackup.scheduleCronJob }}"
23+
failedJobsHistoryLimit: 1
24+
successfulJobsHistoryLimit: 1
25+
jobTemplate:
26+
spec:
27+
template:
28+
spec:
29+
restartPolicy: OnFailure
30+
containers:
31+
- name: {{ .Chart.Name }}-logicalbackup
32+
image: "{{ .Values.logicalbackupImages.repository }}:{{ .Values.logicalbackupImages.tag }}"
33+
imagePullPolicy: {{ .Values.logicalbackupImages.pullPolicy | quote }}
34+
command:
35+
- /usr/bin/env
36+
- bash
37+
- -c
38+
- |
39+
sh /opt/drycc/logicalbackup/logicalbackup.sh
40+
env:
41+
- name: PGHOST
42+
value: {{ include "patroni.fullname" . }}-repl
43+
- name: PGPORT
44+
value: "5432"
45+
- name: PGUSER
46+
value: postgres
47+
- name: PGPASSWORD
48+
valueFrom:
49+
secretKeyRef:
50+
name: {{ template "patroni.fullname" . }}
51+
key: password-superuser
52+
- name: MINIO_BUCKET
53+
value: {{ .Values.logicalbackup.minio.bucket }}
54+
- name: MINIO_HOST
55+
value: {{ .Values.logicalbackup.minio.endpoint }}
56+
- name: MINIO_ACCESS_KEY
57+
value: {{ .Values.logicalbackup.minio.access_key }}
58+
- name: MINIO_SECRET_KEY
59+
value: {{ .Values.logicalbackup.minio.secret_key }}
60+
61+
volumeMounts:
62+
- mountPath: "/opt/drycc/logicalbackup/"
63+
name: logicalbackup-config
64+
65+
volumes:
66+
- name: logicalbackup-config
67+
configMap:
68+
name: {{ template "common.names.fullname" . }}-logicalbackup
69+
{{- end -}}

addons/postgresql-cluster/15/chart/postgresql-cluster/values.yaml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ image:
1313
# IfNotPresent , Always
1414
pullPolicy: 'IfNotPresent'
1515

16+
logicalbackupImages:
17+
repository: registry.drycc.cc/drycc-addons/postgresql-logicalbackup
18+
tag: 15
19+
# IfNotPresent , Always
20+
pullPolicy: 'IfNotPresent'
21+
1622
# Credentials used by Patroni , passwd
1723
# https://github.com/zalando/patroni/blob/master/docs/SETTINGS.rst#postgresql
1824
# https://github.com/zalando/spilo/blob/master/ENVIRONMENT.rst
@@ -147,6 +153,42 @@ backupEnv: |
147153
export AWS_S3_FORCE_PATH_STYLE={{ .Values.backup.s3.awsS3ForcePathStyle | quote }}
148154
export AWS_REGION={{ .Values.backup.s3.awsRegion | quote }}
149155
156+
logicalbackupScript: |
157+
#!/bin/bash
158+
159+
# PostgreSQL 设置
160+
# POSTGRES_USER="postgres"
161+
# POSTGRES_HOST="127.0.0.1"
162+
163+
# MinIO 设置
164+
# MINIO_BUCKET="pgbackup"
165+
# MINIO_HOST="http://localhost:9000"
166+
# MINIO_ACCESS_KEY="admin123"
167+
# MINIO_SECRET_KEY="admin123"
168+
169+
# 设置 MinIO 客户端别名
170+
mc alias set myminio $MINIO_HOST $MINIO_ACCESS_KEY $MINIO_SECRET_KEY
171+
172+
# 创建以当前日期和时间命名的备份目录
173+
BACKUP_DIR="$(date +%Y%m%d%H%M)"
174+
MINIO_PATH="myminio/$MINIO_BUCKET/$BACKUP_DIR"
175+
176+
# 备份全局对象
177+
echo "Backing up global objects to $MINIO_PATH/roles_globals.sql.gz"
178+
pg_dumpall -g -U "$POSTGRES_USER" -h "$POSTGRES_HOST" | pigz | mc pipe "$MINIO_PATH/roles_globals.sql.gz"
179+
180+
# 获取所有非模板数据库的列表
181+
DATABASES=$(psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
182+
183+
# 为每个数据库执行备份
184+
for DB in $DATABASES; do
185+
echo "Backing up $DB to $MINIO_PATH/$DB.sql.gz"
186+
pg_dump -U "$POSTGRES_USER" -h "$POSTGRES_HOST" "$DB" | pigz | mc pipe "$MINIO_PATH/$DB.sql.gz"
187+
done
188+
189+
echo "Backup process completed!"
190+
191+
150192
postgresql:
151193
config: |-
152194
log_min_duration_statement = 1000
@@ -255,6 +297,19 @@ metrics:
255297
requests:
256298
cpu: 100m
257299
memory: 512Mi
300+
301+
logicalbackup:
302+
enabled: false
303+
scheduleCronJob: "22 0 * * 0"
304+
minio:
305+
used: true
306+
buckect: "s3://xx"
307+
access_key: ""
308+
secret_key: ""
309+
endpoint: "http://xxxx:9000"
310+
awsS3ForcePathStyle: "true"
311+
awsRegion: dx-1
312+
258313
backup:
259314
# Specifies whether Wal-G should be enabled
260315
enabled: false
@@ -279,6 +334,9 @@ backup:
279334
awsS3ForcePathStyle: "true"
280335
awsRegion: dx-1
281336

337+
logicalBackup:
338+
enabled: false
339+
282340
## persistentVolumeClaimRetentionPolicy
283341
## ref: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#persistentvolumeclaim-retention
284342
## @param persistentVolumeClaimRetentionPolicy.enabled Controls if and how PVCs are deleted during the lifecycle of a StatefulSet

addons/postgresql-cluster/15/meta.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ allow_parameters:
2424
- name: "backup"
2525
required: false
2626
description: "Whether to use S3 for backup your data. default false . ps: Make sure there is a available S3 "
27+
- name: "logicalbackup"
28+
required: false
29+
description: "Whether to use S3 for logical backup your data. default false . ps: Make sure there is a available S3 "
2730
archive: false

0 commit comments

Comments
 (0)