diff --git a/scripts/backup-db.sh b/scripts/backup-db.sh new file mode 100755 index 0000000..3db04f3 --- /dev/null +++ b/scripts/backup-db.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -euo pipefail + +DB_URL="${GNEXUS_BOOK_DATABASE_URL:-}" +if [ -z "$DB_URL" ]; then + echo "GNEXUS_BOOK_DATABASE_URL is required" >&2 + exit 2 +fi + +DEST="${GNEXUS_BOOK_BACKUP_DIR:-$HOME/NetTera8/service/backups/gnexus-book/db}" +KEEP="${GNEXUS_BOOK_BACKUP_KEEP:-14}" + +if [ -z "${GNEXUS_BOOK_BACKUP_DIR:-}" ] && ! mountpoint -q "$HOME/NetTera8"; then + echo "Off-host mount $HOME/NetTera8 is not available; set GNEXUS_BOOK_BACKUP_DIR explicitly" >&2 + exit 2 +fi + +mkdir -p "$DEST" +STAMP="$(date +%Y-%m-%d_%H%M%S)" +OUT="$DEST/gnexus_book_$STAMP.dump" +PG_URL="${DB_URL/postgresql+psycopg/postgresql}" + +pg_dump --format=custom --no-owner --no-acl --file "$OUT" "$PG_URL" +pg_restore --list "$OUT" > "$OUT.list" +sha256sum "$OUT" > "$OUT.sha256" + +mapfile -t dumps < <(ls -1t "$DEST"/gnexus_book_*.dump 2>/dev/null || true) +if [ "${#dumps[@]}" -gt "$KEEP" ]; then + for old in "${dumps[@]:$KEEP}"; do + rm -f "$old" "$old.list" "$old.sha256" + done +fi + +echo "$OUT" diff --git a/scripts/dev-db.sh b/scripts/dev-db.sh new file mode 100755 index 0000000..d5f8d4c --- /dev/null +++ b/scripts/dev-db.sh @@ -0,0 +1,70 @@ +#!/bin/bash +set -euo pipefail + +BASE_DIR="${GNEXUS_BOOK_PGDATA_ROOT:-$HOME/.local/share/gnexus-book}" +PGDATA="$BASE_DIR/pgdata" +RUNDIR="$BASE_DIR/run" +LOG="$BASE_DIR/postgres.log" +PORT="${GNEXUS_BOOK_PGPORT:-5433}" +DB_USER="${GNEXUS_BOOK_PGUSER:-$(id -un)}" + +database_url() { + printf 'postgresql+psycopg://%s@127.0.0.1:%s/gnexus_book\n' "$DB_USER" "$PORT" +} + +init_cluster() { + mkdir -p "$BASE_DIR" "$RUNDIR" + if [ ! -s "$PGDATA/PG_VERSION" ]; then + initdb -D "$PGDATA" -U "$DB_USER" --auth-local=trust --auth-host=trust >/dev/null + fi +} + +start_cluster() { + init_cluster + if pg_ctl -D "$PGDATA" status >/dev/null 2>&1; then + echo "PostgreSQL already running on port $PORT" + else + pg_ctl -D "$PGDATA" -l "$LOG" \ + -o "-p $PORT -k $RUNDIR -c listen_addresses=127.0.0.1" start >/dev/null + for _ in $(seq 1 40); do + pg_isready -h 127.0.0.1 -p "$PORT" -U "$DB_USER" >/dev/null 2>&1 && break + sleep 0.25 + done + fi + + for db in gnexus_book gnexus_book_test; do + if ! psql -h 127.0.0.1 -p "$PORT" -U "$DB_USER" -d postgres -Atc \ + "select 1 from pg_database where datname='$db'" | grep -q 1; then + createdb -h 127.0.0.1 -p "$PORT" -U "$DB_USER" "$db" + fi + done + + echo "PostgreSQL is ready: $(database_url)" +} + +stop_cluster() { + pg_ctl -D "$PGDATA" stop -m fast +} + +case "${1:-status}" in + init) + init_cluster + echo "Initialized $PGDATA" + ;; + start) + start_cluster + ;; + stop) + stop_cluster + ;; + status) + pg_ctl -D "$PGDATA" status + ;; + url) + database_url + ;; + *) + echo "Usage: $0 {init|start|stop|status|url}" >&2 + exit 2 + ;; +esac diff --git a/scripts/restore-db.sh b/scripts/restore-db.sh new file mode 100755 index 0000000..cfafa76 --- /dev/null +++ b/scripts/restore-db.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -euo pipefail + +if [ $# -ne 2 ]; then + echo "Usage: $0 DUMP TARGET_DATABASE_URL" >&2 + echo "Set GNEXUS_BOOK_RESTORE_YES=1 to confirm; target data will be replaced." >&2 + exit 2 +fi + +DUMP="$1" +TARGET_URL="$2" + +if [ "${GNEXUS_BOOK_RESTORE_YES:-}" != "1" ]; then + echo "Refusing to restore without GNEXUS_BOOK_RESTORE_YES=1" >&2 + exit 2 +fi + +if [ ! -f "$DUMP" ]; then + echo "Dump not found: $DUMP" >&2 + exit 2 +fi + +TARGET_PG="${TARGET_URL/postgresql+psycopg/postgresql}" +pg_restore --clean --if-exists --no-owner --no-acl --dbname "$TARGET_PG" "$DUMP" +echo "Restored $DUMP"