Ensaio de restauro sem CREATEDB para o utilizador da aplicação

RESTORE_AS=postgres faz o ensaio pelo superutilizador local. Instalado e
verificado no servidor a 01/12/2026 (cópia diária 03:15, ensaio semanal).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 07:26:11 +02:00
co-authored by Claude Opus 5.5
parent 20fed7d21e
commit 6c9d405e39
3 changed files with 28 additions and 7 deletions
+12 -2
View File
@@ -29,12 +29,22 @@ load_database() {
[ -n "$PGDATABASE" ] || fail "Nome da base de dados vazio."
}
# Comandos PostgreSQL. Com RESTORE_AS=<utilizador do sistema> (ex.: postgres) correm por esse
# utilizador e pelo socket local, para o ensaio de restauro não exigir CREATEDB ao utilizador da aplicação.
pg() {
if [ -n "${RESTORE_AS:-}" ]; then
runuser -u "$RESTORE_AS" -- env -u PGHOST -u PGPORT -u PGUSER -u PGPASSWORD -u PGDATABASE "$@"
else
"$@"
fi
}
# Contagem de linhas das tabelas principais, uma por linha: "tabela contagem".
table_counts() {
local database="$1" table
for table in $KEY_TABLES; do
if [ "$(psql -X -At -d "$database" -c "select to_regclass('public.$table') is not null")" = "t" ]; then
printf '%s %s\n' "$table" "$(psql -X -At -d "$database" -c "select count(*) from public.$table")"
if [ "$(pg psql -X -At -d "$database" -c "select to_regclass('public.$table') is not null")" = "t" ]; then
printf '%s %s\n' "$table" "$(pg psql -X -At -d "$database" -c "select count(*) from public.$table")"
fi
done
}