Files
planet/scripts/backup_db.sh
T
linkong 5fc698d738
ci / backend (push) Waiting to run
ci / frontend (push) Waiting to run
release / images (push) Waiting to run
release / client_release (push) Waiting to run
release: bump version to 0.78.0
2026-11-23 22:40:11 +08:00

139 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
OUTPUT_DIR="${PLANET_BACKUP_DIR:-./backups/postgres}"
RETENTION_DAYS="${PLANET_BACKUP_RETENTION_DAYS:-14}"
MODE="full"
CONTAINER_NAME=""
VERIFY_GZIP=1
POSTGRES_USER_VALUE="${POSTGRES_USER:-postgres}"
POSTGRES_DB_VALUE="${POSTGRES_DB:-planet_db}"
usage() {
cat <<'EOF'
Usage: scripts/backup_db.sh [options]
Create a compressed PostgreSQL backup and prune expired archives after success.
Options:
--output-dir <path> Backup output directory (default: ./backups/postgres)
--retention-days <days> Delete .sql.gz files older than N days (default: 14)
--mode <full|core> full: complete DB dump (default)
core: schema + core config/users/assessment tables
(skips high-volume collected_data/logs/bgp_observations)
--container <name> Docker Postgres container name (auto-detects running container)
--no-verify Skip gzip integrity verification after dump
-h, --help Show this help message
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--output-dir)
OUTPUT_DIR="$2"
shift 2
;;
--retention-days)
RETENTION_DAYS="$2"
shift 2
;;
--mode)
MODE="$2"
shift 2
;;
--container)
CONTAINER_NAME="$2"
shift 2
;;
--no-verify)
VERIFY_GZIP=0
shift
;;
-h|--help)
usage
exit 0
;;
*)
printf 'Unknown option: %s\n' "$1" >&2
usage >&2
exit 2
;;
esac
done
if [[ "$MODE" != "full" && "$MODE" != "core" ]]; then
printf 'Invalid --mode: %s (expected full or core)\n' "$MODE" >&2
exit 2
fi
if ! [[ "$RETENTION_DAYS" =~ ^[0-9]+$ ]]; then
printf 'Invalid --retention-days: %s\n' "$RETENTION_DAYS" >&2
exit 2
fi
detect_container() {
if [[ -n "$CONTAINER_NAME" ]]; then
printf '%s\n' "$CONTAINER_NAME"
return 0
fi
if command -v docker >/dev/null 2>&1; then
local candidate
for candidate in planet_deploy_postgres planet_postgres; do
if docker ps --format '{{.Names}}' | grep -qx "$candidate"; then
printf '%s\n' "$candidate"
return 0
fi
done
fi
return 1
}
mkdir -p "$OUTPUT_DIR"
STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
TARGET_FILE="${OUTPUT_DIR%/}/planet_${POSTGRES_DB_VALUE}_${MODE}_${STAMP}.sql.gz"
TMP_FILE="${TARGET_FILE}.tmp"
cleanup_tmp() {
rm -f "$TMP_FILE"
}
trap cleanup_tmp EXIT
PG_DUMP_ARGS=(--no-owner --no-privileges --format=plain)
if [[ "$MODE" == "core" ]]; then
PG_DUMP_ARGS+=(
--exclude-table-data=collected_data
--exclude-table-data=bgp_observations
--exclude-table-data=ais_raw_observations
--exclude-table-data=system_logs
--exclude-table-data=observability_events
--exclude-table-data=earth_data_change_events
)
fi
if ACTIVE_CONTAINER="$(detect_container)"; then
docker exec -e PGPASSWORD="${POSTGRES_PASSWORD:-}" "$ACTIVE_CONTAINER" \
pg_dump -U "$POSTGRES_USER_VALUE" -d "$POSTGRES_DB_VALUE" "${PG_DUMP_ARGS[@]}" \
| gzip -c > "$TMP_FILE"
else
DB_URL_RAW="${DATABASE_URL:-}"
if [[ -n "$DB_URL_RAW" ]]; then
DB_URL_NORM="${DB_URL_RAW/postgresql+asyncpg:\/\//postgresql:\/\/}"
pg_dump "$DB_URL_NORM" "${PG_DUMP_ARGS[@]}" | gzip -c > "$TMP_FILE"
else
pg_dump -U "$POSTGRES_USER_VALUE" -d "$POSTGRES_DB_VALUE" "${PG_DUMP_ARGS[@]}" \
| gzip -c > "$TMP_FILE"
fi
fi
if [[ "$VERIFY_GZIP" -eq 1 ]]; then
gzip -t "$TMP_FILE"
fi
mv "$TMP_FILE" "$TARGET_FILE"
printf 'Backup created: %s\n' "$TARGET_FILE"
if [[ "$RETENTION_DAYS" -gt 0 ]]; then
find "$OUTPUT_DIR" -maxdepth 1 -type f -name 'planet_*.sql.gz' -mtime +"$RETENTION_DAYS" -delete
fi