Native VM install kit: systemd units, launcher, install and upgrade scripts; backups, restore and doctor without docker exec; DEPLOY.md VM, NPM and NFS sections

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-30 18:08:48 -06:00
co-authored by Claude Opus 5.5
parent f025479358
commit 45a1d1a42a
22 changed files with 2357 additions and 413 deletions
+64 -89
View File
@@ -1,75 +1,73 @@
#!/usr/bin/env bash
# Packet Station restore. Runs on the docker host. Restores a ps-backup.sh dump into a fresh
# database on a stack's postgres container, optionally applies the globals file, copies assets
# back into an assets volume, and prints the row count of every table at the end.
# Packet Station restore on a native VM. Run by root. Restores a ps-backup dump into a fresh
# database, optionally applies the globals file, copies stored files back into an assets folder,
# checks them against the database, and prints the row count of every table at the end.
#
# It never stops, restarts or reconfigures a stack. Pointing the app at the restored database
# (or renaming it into place) is a separate, deliberate step: see scripts/backup/README.md.
# It never stops, starts or reconfigures a service. Putting the restored database into service
# (renaming it into place) is a separate, deliberate step: see docs/DEPLOY.md, "Restore on a VM".
#
# Usage:
# ps-restore.sh --container NAME | --project PROJECT
# [--dump FILE --db NAME [--replace]]
# [--globals FILE]
# [--assets DIR_OR_TAR --assets-volume VOLUME]
# [--verify-assets --db NAME --assets-volume VOLUME]
# [--counts-only --db NAME]
# ps-restore [--dump FILE --db NAME [--replace]]
# [--globals FILE]
# [--assets DIR_OR_TAR --assets-dir DIR]
# [--verify-assets --db NAME --assets-dir DIR]
# [--counts-only --db NAME]
#
# --container / --project the postgres container, by name or by compose project.
# --dump FILE a ps-*.dump file (pg_dump -Fc).
# --db NAME the database to restore into. It must not exist, unless --replace,
# which drops it first. DROP DATABASE refuses while anyone is connected.
# --owner ROLE owner of the new database (default ps_migrate, as initdb makes it).
# --replace is refused for packetstation, the live database.
# --owner ROLE owner of the new database (default ps_migrate, as install.sh makes it).
# --app-role ROLE role given CONNECT and TEMPORARY on it (default ps_app).
# --locale NAME the new database's locale (default en_US.UTF-8, as install.sh makes it).
# --globals FILE a ps-*.globals.sql file. Roles that already exist are reported and
# left alone. The file holds no passwords: set them from the secrets.
# left alone. The file holds no passwords: install.sh sets them again.
# --assets SRC a folder, or a .tar / .tar.gz / .tgz, holding the files that belong
# under ASSETS_DIR. Copied into --assets-volume through a throwaway
# container (no network) that runs the postgres image already on the host.
# Existing files are never overwritten.
# --assets-volume VOLUME the docker volume to copy into (for example packetstation-test_assets).
# --assets-owner UID:GID owner for the copied files (default 10001:10001, the image's ps user).
# --verify-assets compare sha256 of every file in --assets-volume with assets.sha256 in --db.
# under ASSETS_DIR. Existing files are never overwritten.
# --assets-dir DIR the folder to copy into or check (/var/lib/packetstation/assets is the
# live one; a scratch folder for a rehearsal).
# --assets-owner U:G owner for the copied files (default packetstation:packetstation).
# --verify-assets compare sha256 of every file in --assets-dir with assets.sha256 in --db.
# --counts-only only print the row counts of --db.
#
# Connects inside the container as the postgres superuser over the local socket. No password
# is read, passed or printed.
# psql and pg_restore run as the postgres OS user on the local socket (peer authentication). The
# files are opened by root and handed over on stdin. No password is read, passed or printed.
set -euo pipefail
umask 077
PG_USER="${PG_USER:-postgres}"
DOCKER="${DOCKER:-docker}"
PG_BINDIR="${PG_BINDIR-/usr/lib/postgresql/16/bin}"
read -r -a PG_AS <<< "${PG_AS:-setpriv --reuid=postgres --regid=postgres --init-groups --reset-env --}"
LIVE_DB=packetstation
log() { printf '%s ps-restore: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >&2; }
die() { log "$*"; exit 1; }
usage() { sed -n '2,36p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
usage() { sed -n '2,33p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
container=""
project=""
dump=""
db=""
replace=0
owner="ps_migrate"
app_role="ps_app"
locale_name="en_US.UTF-8"
globals=""
assets=""
assets_volume=""
assets_owner="10001:10001"
assets_dir=""
assets_owner="packetstation:packetstation"
verify_assets=0
counts_only=0
while [[ $# -gt 0 ]]; do
case "$1" in
--container) container="${2:?}"; shift 2 ;;
--project) project="${2:?}"; shift 2 ;;
--dump) dump="${2:?}"; shift 2 ;;
--db) db="${2:?}"; shift 2 ;;
--replace) replace=1; shift ;;
--owner) owner="${2:?}"; shift 2 ;;
--app-role) app_role="${2:?}"; shift 2 ;;
--locale) locale_name="${2:?}"; shift 2 ;;
--globals) globals="${2:?}"; shift 2 ;;
--assets) assets="${2:?}"; shift 2 ;;
--assets-volume) assets_volume="${2:?}"; shift 2 ;;
--assets-dir) assets_dir="${2:?}"; shift 2 ;;
--assets-owner) assets_owner="${2:?}"; shift 2 ;;
--verify-assets) verify_assets=1; shift ;;
--counts-only) counts_only=1; shift ;;
@@ -82,32 +80,31 @@ ident_re='^[a-z_][a-z0-9_]{0,62}$'
[[ -z "$db" || "$db" =~ $ident_re ]] || die "--db must be a plain lower-case name (letters, digits, _)"
[[ "$owner" =~ $ident_re ]] || die "--owner must be a plain lower-case role name"
[[ "$app_role" =~ $ident_re ]] || die "--app-role must be a plain lower-case role name"
[[ "$assets_owner" =~ ^[0-9]+:[0-9]+$ ]] || die "--assets-owner must be UID:GID"
if [[ -n "$project" ]]; then
[[ -z "$container" ]] || die "use --container or --project, not both"
container="$("$DOCKER" ps -q \
--filter "label=com.docker.compose.project=$project" \
--filter "label=com.docker.compose.service=postgres")"
[[ -n "$container" ]] || die "no running postgres container for compose project $project"
[[ "$(printf '%s\n' "$container" | wc -l)" -eq 1 ]] || die "more than one postgres container for $project"
fi
[[ -n "$container" ]] || die "--container or --project is required"
[[ "$locale_name" =~ ^[A-Za-z0-9_.@-]+$ ]] || die "--locale must be a locale name like en_US.UTF-8"
[[ "$assets_owner" =~ ^[a-z_0-9][a-z0-9_-]*:[a-z_0-9][a-z0-9_-]*$ ]] || die "--assets-owner must be USER:GROUP"
if [[ -n "$dump" || $counts_only -eq 1 || $verify_assets -eq 1 ]]; then
[[ -n "$db" ]] || die "--db is required"
fi
if [[ -n "$assets" || $verify_assets -eq 1 ]]; then
[[ -n "$assets_volume" ]] || die "--assets-volume is required"
[[ -n "$assets_dir" ]] || die "--assets-dir is required"
[[ -d "$assets_dir" ]] || die "$assets_dir is not a folder"
fi
if [[ -z "$dump" && -z "$globals" && -z "$assets" && $verify_assets -eq 0 && $counts_only -eq 0 ]]; then
die "nothing to do: give --dump, --globals, --assets, --verify-assets or --counts-only"
fi
if [[ -n "$dump" && $replace -eq 1 && "$db" == "$LIVE_DB" ]]; then
die "--replace is refused for $LIVE_DB. Restore into another name and rename it into place (docs/DEPLOY.md)."
fi
# psql inside the container, reading SQL from stdin. Variables go through -v and :'name'.
pgbin() {
if [[ -n "$PG_BINDIR" ]]; then printf '%s/%s\n' "$PG_BINDIR" "$1"; else printf '%s\n' "$1"; fi
}
# psql as postgres, reading SQL from stdin. Variables go through -v and :'name'.
psql_c() {
local dbname="$1"; shift
"$DOCKER" exec -i "$container" psql -X -q -v ON_ERROR_STOP=1 -U "$PG_USER" -d "$dbname" "$@"
"${PG_AS[@]}" "$(pgbin psql)" -X -q -v ON_ERROR_STOP=1 -d "$dbname" "$@"
}
db_exists() {
@@ -143,22 +140,16 @@ ORDER BY c.relname;
SQL
}
# A throwaway container on no network, running the image the postgres container already uses,
# so nothing is downloaded.
helper_image() {
"$DOCKER" inspect -f '{{.Config.Image}}' "$container"
}
# ------------------------------------------------------------------ globals
if [[ -n "$globals" ]]; then
[[ -r "$globals" ]] || die "cannot read $globals"
if grep -qiE "PASSWORD[[:space:]]+'(SCRAM|md5)" "$globals"; then
die "$globals holds password hashes; ps-backup.sh writes globals without them. Refusing."
die "$globals holds password hashes; ps-backup writes globals without them. Refusing."
fi
log "applying globals from $(basename -- "$globals") (existing roles are left as they are)"
# Without ON_ERROR_STOP: CREATE ROLE for a role that exists fails and the rest goes on.
out="$("$DOCKER" exec -i "$container" psql -X -q -U "$PG_USER" -d postgres < "$globals" 2>&1 || true)"
out="$("${PG_AS[@]}" "$(pgbin psql)" -X -q -d postgres < "$globals" 2>&1 || true)"
exists="$(printf '%s\n' "$out" | grep -c 'already exists' || true)"
other="$(printf '%s\n' "$out" | grep 'ERROR' | grep -vc 'already exists' || true)"
log "globals: $exists statements skipped (already exists), $other other errors"
@@ -180,12 +171,13 @@ if [[ -n "$dump" ]]; then
SELECT format('DROP DATABASE %I', :'name') \gexec
SQL
fi
role_exists "$owner" || die "role $owner does not exist (apply --globals or start the stack once)"
role_exists "$owner" || die "role $owner does not exist (apply --globals, or run deploy/vm/install.sh)"
log "creating database $db owned by $owner"
# The same shape as deploy/postgres/initdb/01-roles.sh.
psql_c postgres -v name="$db" -v owner="$owner" <<'SQL'
SELECT format('CREATE DATABASE %I OWNER %I ENCODING %L TEMPLATE template0', :'name', :'owner', 'UTF8') \gexec
# The same shape as deploy/vm/install.sh makes the live one.
psql_c postgres -v name="$db" -v owner="$owner" -v loc="$locale_name" <<'SQL'
SELECT format('CREATE DATABASE %I OWNER %I ENCODING %L LOCALE %L TEMPLATE template0',
:'name', :'owner', 'UTF8', :'loc') \gexec
SELECT format('REVOKE ALL ON DATABASE %I FROM PUBLIC', :'name') \gexec
SQL
if role_exists "$app_role"; then
@@ -198,8 +190,8 @@ SQL
log "restoring $(basename -- "$dump") ($(stat -c %s "$dump") bytes) into $db"
# One transaction: a restore that fails leaves an empty database, never a half-restored one.
"$DOCKER" exec -i "$container" pg_restore -U "$PG_USER" -d "$db" --exit-on-error --single-transaction \
< "$dump" || die "pg_restore failed; database $db is left empty. Drop it with --replace on the next try."
"${PG_AS[@]}" "$(pgbin pg_restore)" -d "$db" --exit-on-error --single-transaction < "$dump" \
|| die "pg_restore failed; database $db is left empty. Drop it with --replace on the next try."
log "database restored in $(( SECONDS - started )) s"
fi
@@ -207,26 +199,21 @@ fi
if [[ -n "$assets" ]]; then
[[ -e "$assets" ]] || die "$assets does not exist"
"$DOCKER" volume inspect "$assets_volume" >/dev/null 2>&1 || die "volume $assets_volume does not exist"
image="$(helper_image)"
started=$SECONDS
src="$(cd -- "$(dirname -- "$assets")" && pwd)/$(basename -- "$assets")"
if [[ -d "$assets" ]]; then
log "copying assets from folder $src into volume $assets_volume"
"$DOCKER" run --rm --network none --user 0:0 --entrypoint sh \
-v "$assets_volume:/dst" -v "$src:/src:ro" "$image" -c \
'set -eu; cp -a -n /src/. /dst/; chown -R "$1" /dst' sh "$assets_owner"
log "copying assets from folder $assets into $assets_dir"
cp -a -n -- "$assets/." "$assets_dir/"
else
case "$assets" in
*.tar.gz|*.tgz) tar_flags="-xzf" ;;
*.tar) tar_flags="-xf" ;;
*) die "--assets must be a folder, .tar, .tar.gz or .tgz" ;;
esac
log "copying assets from $(basename -- "$src") into volume $assets_volume"
"$DOCKER" run --rm --network none --user 0:0 --entrypoint sh \
-v "$assets_volume:/dst" -v "$src:/src.tar:ro" "$image" -c \
'set -eu; tar "$1" /src.tar -C /dst --skip-old-files --no-same-owner; chown -R "$2" /dst' \
sh "$tar_flags" "$assets_owner"
log "copying assets from $(basename -- "$assets") into $assets_dir"
tar "$tar_flags" "$assets" -C "$assets_dir" --skip-old-files --no-same-owner
fi
if [[ "$(id -u)" == "0" ]]; then
chown -R -- "$assets_owner" "$assets_dir"
fi
log "assets copied in $(( SECONDS - started )) s"
fi
@@ -234,8 +221,6 @@ fi
# ------------------------------------------------------------------ verify assets
if [[ $verify_assets -eq 1 ]]; then
"$DOCKER" volume inspect "$assets_volume" >/dev/null 2>&1 || die "volume $assets_volume does not exist"
image="$(helper_image)"
work="$(mktemp -d)"
trap 'rm -rf -- "$work"' EXIT
# sha256sum check format: "<hex> <path>".
@@ -243,21 +228,11 @@ if [[ $verify_assets -eq 1 ]]; then
SELECT encode(sha256, 'hex') || ' ' || storage_key FROM assets ORDER BY storage_key;
SQL
rows="$(wc -l < "$work/assets.sha256")"
chmod 0755 "$work"
chmod 0644 "$work/assets.sha256"
log "checking $rows asset rows of $db against volume $assets_volume"
set +e
"$DOCKER" run --rm --network none --user 0:0 --entrypoint sh \
-v "$assets_volume:/dst:ro" -v "$work:/work:ro" "$image" -c '
cd /dst || exit 3
sha256sum -c /work/assets.sha256 2>/dev/null
find . -type f ! -path "./tmp/*" | sed "s|^\./||" | sort > /tmp/files
cut -c67- /work/assets.sha256 | sort > /tmp/rows
comm -23 /tmp/files /tmp/rows | sed "s|^|ORPHAN: |"
exit 0' > "$work/result"
rc=$?
set -e
[[ $rc -eq 0 ]] || die "the check container failed (exit $rc)"
log "checking $rows asset rows of $db against $assets_dir"
( cd -- "$assets_dir" && sha256sum -c "$work/assets.sha256" 2>/dev/null ) > "$work/result" || true
( cd -- "$assets_dir" && find . -type f ! -path './tmp/*' | sed 's|^\./||' | LC_ALL=C sort ) > "$work/files"
cut -c67- "$work/assets.sha256" | LC_ALL=C sort > "$work/rows"
LC_ALL=C comm -23 "$work/files" "$work/rows" | sed 's|^|ORPHAN: |' >> "$work/result"
ok="$(grep -c ': OK$' "$work/result" || true)"
bad="$(grep -c ': FAILED$' "$work/result" || true)"
missing="$(grep -c ': FAILED open or read$' "$work/result" || true)"