forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiBc44f362Ahmet Kaan Gümüş2 months ago520 commits

backup-postgres.sh26 lines, 1.1 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Dump every database in the hezarfen compose stack to backups/ next to this
3# script. pg_dumpall is required (not pg_dump): each school is its own
4# postgres database, and a control-only dump would lose them all.
5#
6# Keeps: backups/hezarfen-<UTC>.sql.gz, mode 0600.
7# Cron example (daily 04:20):
8# 20 4 * * * $HOME/hezarfen_backend/backup-postgres.sh
9#
10# Restore (from ~/hezarfen_backend):
11# gunzip -c backups/FILE.sql.gz | podman exec -i hezarfen_backend_postgres psql -U hezarfen -d postgres
12set -euo pipefail
13
14dir="$(cd "$(dirname "$0")" && pwd)"
15mkdir -p "$dir/backups"
16
17# POSTGRES_USER lives in the env file beside this script (plain KEY=value
18# lines, no quotes/export); default hezarfen.
19user="$(awk -F= '/^POSTGRES_USER=/{print $2; exit}' "$dir/hezarfen_backend.env" 2>/dev/null || true)"
20user="${user:-hezarfen}"
21
22out="$dir/backups/hezarfen-$(date -u +%Y%m%dT%H%M%SZ).sql.gz"
23umask 077
24# pipefail (set above): a failed pg_dumpall must not leave a "good" empty dump.
25podman exec hezarfen_backend_postgres pg_dumpall -U "$user" | gzip > "$out"
26echo "wrote $out ($(du -h "$out" | cut -f1))"