From a2049ea10a4735ddc3b97db65efa496518368936 Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Tue, 14 Jul 2026 23:27:11 +0330 Subject: [PATCH] fix(scripts): ensure postgres env aliases during restore --- README.md | 12 ++++++++++ scripts/backup.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/restore.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) diff --git a/README.md b/README.md index 6312c15..cc34be6 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,18 @@ cd ~/guilan-ace-deployment docker compose up -d --build ``` +If a new server fails with `Database is uninitialized and superuser password is not specified`, +make sure `backend/guilan-ace-backend/.env` contains `POSTGRES_DB`, `POSTGRES_USER`, and +`POSTGRES_PASSWORD` matching `DB_NAME`, `DB_USER`, and `DB_PASSWORD`. Then remove the failed +empty database volume and rerun the restore: +```bash +cd ~/guilan-ace-deployment +docker compose down +docker volume rm east-guilan_postgres_data +./scripts/restore-from-s3.sh latest +docker compose up -d --build +``` + Restore only selected parts by setting skip flags: ```bash RESTORE_SKIP_ENV=1 ./scripts/restore.sh backups/guilan-ace-backup-YYYYMMDD-HHMMSS.tar.gz diff --git a/scripts/backup.sh b/scripts/backup.sh index f85d10d..25b4fe2 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -54,6 +54,59 @@ copy_env_if_exists() { fi } +env_value() { + local key="$1" + local file="$2" + + awk -v key="$key" ' + $0 ~ "^[[:space:]]*" key "=" { + sub(/^[^=]*=/, "") + print + exit + } + ' "$file" +} + +set_env_value() { + local key="$1" + local value="$2" + local file="$3" + local tmp_path + + tmp_path="$(mktemp)" + awk -v key="$key" -v value="$value" ' + $0 ~ "^[[:space:]]*" key "=" { + print key "=" value + found = 1 + next + } + { print } + END { + if (!found) { + print key "=" value + } + } + ' "$file" > "$tmp_path" + mv "$tmp_path" "$file" + chmod 600 "$file" || true +} + +sync_postgres_env_aliases() { + local db_name db_user db_password + + db_name="$(env_value DB_NAME "$BACKEND_ENV")" + db_user="$(env_value DB_USER "$BACKEND_ENV")" + db_password="$(env_value DB_PASSWORD "$BACKEND_ENV")" + + [[ -n "$db_name" ]] || fail "DB_NAME is missing or empty in $BACKEND_ENV" + [[ -n "$db_user" ]] || fail "DB_USER is missing or empty in $BACKEND_ENV" + [[ -n "$db_password" ]] || fail "DB_PASSWORD is missing or empty in $BACKEND_ENV" + + set_env_value POSTGRES_DB "$db_name" "$BACKEND_ENV" + set_env_value POSTGRES_USER "$db_user" "$BACKEND_ENV" + set_env_value POSTGRES_PASSWORD "$db_password" "$BACKEND_ENV" +} + if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 @@ -69,6 +122,9 @@ require_file "$DEPLOY_ROOT/docker-compose.yml" require_file "$DEPLOY_ENV" require_file "$BACKEND_ENV" +log "Ensuring PostgreSQL environment aliases exist" +sync_postgres_env_aliases + mkdir -p "$OUTPUT_DIR" WORK_DIR="$(mktemp -d)" trap 'rm -rf "$WORK_DIR"' EXIT diff --git a/scripts/restore.sh b/scripts/restore.sh index 1b4064e..90792c0 100644 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -62,6 +62,59 @@ restore_env_if_present() { fi } +env_value() { + local key="$1" + local file="$2" + + awk -v key="$key" ' + $0 ~ "^[[:space:]]*" key "=" { + sub(/^[^=]*=/, "") + print + exit + } + ' "$file" +} + +set_env_value() { + local key="$1" + local value="$2" + local file="$3" + local tmp_path + + tmp_path="$(mktemp)" + awk -v key="$key" -v value="$value" ' + $0 ~ "^[[:space:]]*" key "=" { + print key "=" value + found = 1 + next + } + { print } + END { + if (!found) { + print key "=" value + } + } + ' "$file" > "$tmp_path" + mv "$tmp_path" "$file" + chmod 600 "$file" || true +} + +sync_postgres_env_aliases() { + local db_name db_user db_password + + db_name="$(env_value DB_NAME "$BACKEND_ENV")" + db_user="$(env_value DB_USER "$BACKEND_ENV")" + db_password="$(env_value DB_PASSWORD "$BACKEND_ENV")" + + [[ -n "$db_name" ]] || fail "DB_NAME is missing or empty in $BACKEND_ENV" + [[ -n "$db_user" ]] || fail "DB_USER is missing or empty in $BACKEND_ENV" + [[ -n "$db_password" ]] || fail "DB_PASSWORD is missing or empty in $BACKEND_ENV" + + set_env_value POSTGRES_DB "$db_name" "$BACKEND_ENV" + set_env_value POSTGRES_USER "$db_user" "$BACKEND_ENV" + set_env_value POSTGRES_PASSWORD "$db_password" "$BACKEND_ENV" +} + if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 @@ -99,6 +152,9 @@ fi require_file "$BACKEND_ENV" require_file "$DEPLOY_ENV" +log "Ensuring PostgreSQL environment aliases exist" +sync_postgres_env_aliases + log "Checking Docker Compose configuration" compose config -q