Skip to content

Commit e596980

Browse files
zhangeamonzhangeamon
authored andcommitted
chore(postgresql): add backup cronjob
1 parent 1428eb5 commit e596980

8 files changed

Lines changed: 154 additions & 15 deletions

File tree

Dockerfile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ ARG PYTHON_VERSION="3.13" \
1111
ENV HOME=/data \
1212
PG_MAJOR=18 \
1313
PG_MINOR=1 \
14-
WAL_G_VERSIONR="3.0.5" \
15-
RCLONE_VERSION="1.71.1"
14+
S3_RCLONE_VERSION="1.71.1"
1615

17-
ENV PGDATA $HOME/$PG_MAJOR
16+
ENV PGDATA=$HOME/$PG_MAJOR
1817

19-
RUN install-packages vim gcc \
18+
RUN install-packages vim gcc pigz jq\
2019
&& install-stack python $PYTHON_VERSION \
2120
&& install-stack postgresql $PG_MAJOR.$PG_MINOR \
22-
&& install-stack wal-g $WAL_G_VERSIONR \
23-
&& install-stack rclone $RCLONE_VERSION \
21+
&& install-stack rclone $S3_RCLONE_VERSION \
2422
&& install-stack postgres_exporter $POSTGRES_EXPORTER_VERSION \
2523
&& . init-stack \
2624
&& set -eux; pip3 install --disable-pip-version-check --no-cache-dir psycopg[binary] patroni[kubernetes] 2>/dev/null; set +eux \

charts/database/templates/_helper.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{- define "database.envs" }}
22
env:
3-
- name: USE_WALG
4-
value: "true"
3+
- name: RETAIN_BACKUPS_AGE
4+
value: "{{.Values.cronjob.retainBackupsAge}}"
55
{{- if (.Values.storageEndpoint) }}
66
- name: "DRYCC_STORAGE_BUCKET"
77
valueFrom:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{- if .Values.cronjob.enabled }}
2+
apiVersion: {{ include "common.capabilities.cronjob.apiVersion" . }}
3+
kind: CronJob
4+
metadata:
5+
name: drycc-database-cronjob-backup
6+
labels:
7+
heritage: drycc
8+
annotations:
9+
component.drycc.cc/version: {{ .Values.imageTag }}
10+
11+
spec:
12+
schedule: "{{ .Values.cronjob.scheduleCronJob }}"
13+
failedJobsHistoryLimit: 1
14+
successfulJobsHistoryLimit: 1
15+
jobTemplate:
16+
spec:
17+
template:
18+
spec:
19+
restartPolicy: OnFailure
20+
containers:
21+
- name: drycc-database-cronjob-backup
22+
image: {{.Values.imageRegistry}}/{{.Values.imageOrg}}/database:{{.Values.imageTag}}
23+
imagePullPolicy: {{.Values.imagePullPolicy}}
24+
command:
25+
- init-stack
26+
args:
27+
- /usr/share/scripts/backup.sh
28+
{{- include "database.envs" . | indent 12 }}
29+
{{- end -}}

charts/database/values-test.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
imageOrg: "drycc"
2+
imagePullPolicy: "Always"
3+
imageTag: "test001"
4+
imageRegistry: "registry.drycc.cc"
5+
6+
# The following parameters will no longer use the built-in storage component.
7+
8+
storageBucket: "database1"
9+
storageEndpoint: "http://10.1.80.61:9000"
10+
storageAccesskey: "admin"
11+
storageSecretkey: "admin123"
12+
storagePathStyle: "true"
13+
14+
storage:
15+
enabled: false
16+
17+
cronjob:
18+
enabled: true
19+
scheduleCronJob: "*/10 * * * *"
20+
retainBackupsAge: 1h

charts/database/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ storageAccesskey: ""
5656
storageSecretkey: ""
5757
storagePathStyle: "true"
5858

59+
storage:
60+
enabled: true
61+
62+
cronjob:
63+
enabled: true
64+
scheduleCronJob: "0 3 * * *"
65+
retainBackupsAge: 30d
66+
5967
# Service
6068
service:
6169
# Provide any additional service annotations

rootfs/entrypoint.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#!/bin/bash
2-
3-
/bin/create_bucket
4-
52
if [[ $UID -ge 10000 ]]; then
63
GID=$(id -g)
74
sed -e "s/^postgres:x:[^:]*:[^:]*:/postgres:x:$UID:$GID:/" /etc/passwd > /tmp/passwd
@@ -21,14 +18,14 @@ bootstrap:
2118
use_pg_rewind: true
2219
use_slots: true
2320
initdb:
24-
- auth-host: md5
21+
- auth-host: scram-sha-256
2522
- auth-local: trust
2623
- encoding: UTF8
2724
- locale: ${LANG}
2825
- data-checksums
2926
pg_hba:
30-
- host all all 0.0.0.0/0 md5
31-
- host replication ${DRYCC_DATABASE_REPLICATOR} ${PATRONI_KUBERNETES_POD_IP}/16 md5
27+
- host all all 0.0.0.0/0 scram-sha-256
28+
- host replication ${DRYCC_DATABASE_REPLICATOR} ${PATRONI_KUBERNETES_POD_IP}/16 scram-sha-256
3229
post_bootstrap: /usr/share/scripts/patroni/post_init.sh
3330
restapi:
3431
connect_address: '${PATRONI_KUBERNETES_POD_IP}:8008'
@@ -45,13 +42,15 @@ postgresql:
4542
max_prepared_transactions: 0
4643
max_locks_per_transaction: 64
4744
wal_log_hints: "on"
45+
wal_level: logical
4846
track_commit_timestamp: "off"
4947
archive_mode: "on"
5048
archive_timeout: 300s
51-
archive_command: "/usr/share/scripts/wal-g/wal_push.sh %p"
49+
archive_command: "/bin/true"
5250
log_min_duration_statement: 1000
5351
log_lock_waits: on
5452
log_statement: 'ddl'
53+
jit: off
5554
connect_address: '${PATRONI_KUBERNETES_POD_IP}:5432'
5655
authentication:
5756
superuser:

rootfs/usr/share/scripts/backup.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Setup Rclone
5+
/usr/share/scripts/create_bucket
6+
7+
# Backup PostgreSQL databases to S3-compatible object storage
8+
export PGPASSWORD=$DRYCC_DATABASE_SUPERUSER_PASSWORD
9+
PGPORT=$DRYCC_DATABASE_REPLICA_SERVICE_PORT_POSTGRES
10+
PGUSER=postgres
11+
POSTGRES_HOST=$DRYCC_DATABASE_REPLICA_SERVICE_HOST
12+
RETAIN_BACKUPS_AGE=$RETAIN_BACKUPS_AGE
13+
14+
# PostgreSQL global objects backup
15+
BACKUP_PATH="${DRYCC_STORAGE_BUCKET}/$(date +%Y%m%d%H%M)"
16+
echo "DB: ${POSTGRES_USER}@${POSTGRES_HOST}"
17+
echo "S3 Storage: storage:${BACKUP_PATH}"
18+
# Backup global objects
19+
if pg_dumpall -g -U "${POSTGRES_USER}" -h "${POSTGRES_HOST}" \
20+
| pigz -c -p 4 -6 \
21+
| rclone rcat \
22+
"storage:${BACKUP_PATH}/roles_globals.sql.gz" \
23+
--bwlimit 10M \
24+
--transfers 4 \
25+
--s3-chunk-size 64M \
26+
--stats 5s \
27+
--progress \
28+
--retries 3; then
29+
echo "✅ PostgreSQL global objects backup complated!"
30+
fi
31+
# fetch the list of databases
32+
DATABASES=$(psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
33+
34+
# backup each database individually
35+
for DB in $DATABASES; do
36+
echo "Backing up $DB to $MINIO_PATH/$DB.sql.gz"
37+
if pg_dump -U "${POSTGRES_USER}" -h "${POSTGRES_HOST}" "$DB" \
38+
| pigz -c -p 4 -6 \
39+
| rclone rcat \
40+
"storage:${BACKUP_PATH}/$DB.sql.gz" \
41+
--bwlimit 10M \
42+
--transfers 4 \
43+
--s3-chunk-size 64M \
44+
--stats 5s \
45+
--progress \
46+
--retries 3; then
47+
echo "✅ PostgreSQL $DB global objects backup completed!"
48+
fi
49+
done
50+
51+
echo "Backup process completed!"
52+
53+
echo "delete storage before ${RETAIN_BACKUPS_AGE} ..."
54+
rclone delete "storage:${DRYCC_STORAGE_BUCKET}" \
55+
--min-age ${RETAIN_BACKUPS_AGE} \
56+
--include "*.sql.gz" \
57+
--dry-run \
58+
-v || true
59+
60+
rclone delete "storage:${DRYCC_STORAGE_BUCKET}" \
61+
--min-age ${RETAIN_BACKUPS_AGE} \
62+
--include "*.sql.gz" \
63+
|| true
64+
echo "delete completed."
65+
66+
echo "=== backup completed: $(date) ==="
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
mkdir -p ~/.config/rclone
6+
touch ~/.config/rclone/rclone.conf
7+
rclone config create storage s3 \
8+
provider=Other \
9+
access_key_id="${DRYCC_STORAGE_ACCESSKEY}" \
10+
secret_access_key="${DRYCC_STORAGE_SECRETKEY}" \
11+
endpoint="${DRYCC_STORAGE_ENDPOINT}" \
12+
force_path_style="${DRYCC_STORAGE_PATH_STYLE:-true}" --no-output
13+
14+
if ! rclone lsd storage: > /dev/null 2>&1; then
15+
sleep 9s
16+
echo "waiting for object storage to become ready..."
17+
fi
18+
19+
rclone mkdir "storage:${DRYCC_STORAGE_BUCKET}"

0 commit comments

Comments
 (0)