Files
easyai-ai-gateway/scripts/cluster/restore-backup-drill.sh
T
wangbo 7c142f5960 fix(deploy): 补齐集群备份兼容与切换前门禁
阿里云 OSS 的 S3 兼容层要求 virtual-hosted addressing,且 boto3 需要使用 Signature V2。本提交为 Barman 的归档、备份、保留与恢复统一挂载 AWS 配置,并将实际 WAL 归档和近期全备设为切换硬门禁。

同时补充 CNPG/etcdutl 固定版本安装、显式主库切换、三节点逐台停机、OSS 快照下载校验与远端临时 Secret 清理。已通过 ShellCheck、Kustomize、服务端 dry-run、迁移测试、发布脚本测试、敏感信息扫描及生产 precutover 验收。
2026-07-28 06:46:09 +08:00

112 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=scripts/cluster/common.sh
source "$script_dir/common.sh"
[[ ${1:-} == --execute ]] || {
echo 'usage: restore-backup-drill.sh --execute' >&2
exit 64
}
load_cluster_env
cluster_ssh "$CLUSTER_NINGBO_HOST" 'bash -s' <<'REMOTE'
set -euo pipefail
kubectl='k3s kubectl'
suffix=$(date -u '+%m%d%H%M%S')
restore_cluster=easyai-restore-drill-"$suffix"
cleanup() {
$kubectl delete cluster "$restore_cluster" -n easyai \
--ignore-not-found=true --wait=true --timeout=300s || true
$kubectl delete pvc -n easyai -l "cnpg.io/cluster=$restore_cluster" \
--ignore-not-found=true --wait=true --timeout=300s || true
}
trap cleanup EXIT HUP INT TERM
source_primary=$($kubectl get cluster easyai-postgres -n easyai -o jsonpath='{.status.currentPrimary}')
acceptance_sql=$(mktemp)
trap 'rm -f -- "$acceptance_sql"; cleanup' EXIT HUP INT TERM
cat >"$acceptance_sql" <<'SQL'
\pset tuples_only on
\pset format unaligned
SELECT 'tables=' || count(*) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE c.relkind='r' AND n.nspname='public';
SELECT 'tasks=' || count(*) FROM gateway_tasks;
SELECT 'attempts=' || count(*) FROM gateway_task_attempts;
SELECT 'wallet=' || count(*) FROM gateway_wallet_transactions;
SELECT 'users=' || count(*) FROM gateway_users;
SELECT 'sample=' || md5(COALESCE(string_agg(id::text, ',' ORDER BY id),''))
FROM (SELECT id FROM gateway_tasks ORDER BY id LIMIT 100) rows;
SQL
source_snapshot=$($kubectl exec -i -n easyai "$source_primary" -c postgres -- \
psql -X -v ON_ERROR_STOP=1 -U postgres -d easyai_ai_gateway <"$acceptance_sql")
cat <<EOF | $kubectl apply -f - >/dev/null
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: $restore_cluster
namespace: easyai
labels:
easyai.io/acceptance-drill: backup-restore
spec:
instances: 1
imageName: ghcr.io/cloudnative-pg/postgresql:18.4-standard-trixie@sha256:4587df73024408f5b2be9b4dd6ba2ccee8c9e5dc0c9a87c274c292291cc8a68c
bootstrap:
recovery:
source: origin
database: easyai_ai_gateway
owner: easyai
secret:
name: easyai-postgres-app
externalClusters:
- name: origin
plugin:
name: barman-cloud.cloudnative-pg.io
parameters:
barmanObjectName: easyai-postgres-oss
serverName: easyai-postgres
storage:
storageClass: local-path
size: 20Gi
walStorage:
storageClass: local-path
size: 5Gi
affinity:
nodeSelector:
easyai.io/site: ningbo
resources:
requests:
cpu: 250m
memory: 512Mi
limits:
cpu: "2"
memory: 3Gi
projectedVolumeTemplate:
defaultMode: 0444
sources:
- configMap:
name: easyai-barman-aws-config
items:
- key: config
path: barman-aws/config
EOF
$kubectl wait --for=condition=Ready "cluster/$restore_cluster" -n easyai --timeout=1200s
restore_primary=$($kubectl get cluster "$restore_cluster" -n easyai -o jsonpath='{.status.currentPrimary}')
restored_snapshot=$($kubectl exec -i -n easyai "$restore_primary" -c postgres -- \
psql -X -v ON_ERROR_STOP=1 -U postgres -d easyai_ai_gateway <"$acceptance_sql")
[[ $restored_snapshot == "$source_snapshot" ]] || {
diff -u <(printf '%s\n' "$source_snapshot") <(printf '%s\n' "$restored_snapshot")
exit 1
}
$kubectl exec -n easyai "$restore_primary" -c postgres -- \
psql -X -v ON_ERROR_STOP=1 -U postgres -d easyai_ai_gateway -At -c \
"SELECT current_database();" | grep -qx 'easyai_ai_gateway'
cleanup
trap - EXIT HUP INT TERM
rm -f -- "$acceptance_sql"
echo "backup_restore_drill=PASS cluster=$restore_cluster deleted=true"
REMOTE