fix(scripts): ensure postgres env aliases during restore
Some checks failed
Deployment CI/CD / validate (push) Has been cancelled
Deployment CI/CD / deploy (push) Has been cancelled

This commit is contained in:
2026-07-14 23:27:11 +03:30
parent a617fdf1ec
commit a2049ea10a
3 changed files with 124 additions and 0 deletions

View File

@@ -94,6 +94,18 @@ cd ~/guilan-ace-deployment
docker compose up -d --build 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: Restore only selected parts by setting skip flags:
```bash ```bash
RESTORE_SKIP_ENV=1 ./scripts/restore.sh backups/guilan-ace-backup-YYYYMMDD-HHMMSS.tar.gz RESTORE_SKIP_ENV=1 ./scripts/restore.sh backups/guilan-ace-backup-YYYYMMDD-HHMMSS.tar.gz

View File

@@ -54,6 +54,59 @@ copy_env_if_exists() {
fi 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 if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage usage
exit 0 exit 0
@@ -69,6 +122,9 @@ require_file "$DEPLOY_ROOT/docker-compose.yml"
require_file "$DEPLOY_ENV" require_file "$DEPLOY_ENV"
require_file "$BACKEND_ENV" require_file "$BACKEND_ENV"
log "Ensuring PostgreSQL environment aliases exist"
sync_postgres_env_aliases
mkdir -p "$OUTPUT_DIR" mkdir -p "$OUTPUT_DIR"
WORK_DIR="$(mktemp -d)" WORK_DIR="$(mktemp -d)"
trap 'rm -rf "$WORK_DIR"' EXIT trap 'rm -rf "$WORK_DIR"' EXIT

View File

@@ -62,6 +62,59 @@ restore_env_if_present() {
fi 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 if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage usage
exit 0 exit 0
@@ -99,6 +152,9 @@ fi
require_file "$BACKEND_ENV" require_file "$BACKEND_ENV"
require_file "$DEPLOY_ENV" require_file "$DEPLOY_ENV"
log "Ensuring PostgreSQL environment aliases exist"
sync_postgres_env_aliases
log "Checking Docker Compose configuration" log "Checking Docker Compose configuration"
compose config -q compose config -q