SPB Git

Repository Public

backup.sh 1.9 KB 31 lines shellscript

1#!/bin/zsh2# Nightly encrypted backup with restore verification.3#   databases   -> pg_dump, AES-256 (key ~/.config/sp-vault/backup.key). Vault content inside is already ciphertext4#                  that this node cannot read; the outer layer protects metadata and the managed-secrets ciphertext.5#   attachments -> rsync snapshot (client-side encrypted chunks)6# The managed-secrets master key is NOT part of the backup (keep its copy elsewhere).7# Verification: each archive is decrypted and restored into a scratch database; row counts are compared.8set -euo pipefail9export PATH="/opt/homebrew/bin:/opt/homebrew/opt/postgresql@17/bin:/usr/bin:/bin:/usr/sbin:/sbin"10DATA="${SPV_DATA_DIR:-$HOME/sp-vault-data}"; OUT="$HOME/backups/sp-vault"; KEY="$HOME/.config/sp-vault/backup.key"11STAMP=$(date -u +%Y%m%dT%H%M%SZ); mkdir -p "$OUT/attachments"12enc() { openssl enc -aes-256-cbc -pbkdf2 -iter 200000 -salt -pass "file:$KEY" "$@"; }13check() { # check <live-db> <scratch-db> <tables…>14  local live="$1" scratch="$2"; shift 215  for t in "$@"; do16    a=$(psql -tAc "select count(*) from $t" "$live"); b=$(psql -tAc "select count(*) from $t" "$scratch")17    if [ "$b" -gt "$a" ] || { [ "$a" -gt 0 ] && [ "$b" -eq 0 ]; }; then echo "BACKUP VERIFICATION FAILED on $live.$t (live $a, restored $b)"; exit 1; fi18  done19}20SCRATCH="sp_vault_verify_$$"; trap 'dropdb --if-exists "$SCRATCH"' EXIT21for DB in sp_vault sp_vault_managed; do22  F="$OUT/$DB-$STAMP.dump.enc"23  pg_dump -Fc "$DB" | enc -out "$F"24  dropdb --if-exists "$SCRATCH"; createdb "$SCRATCH"25  enc -d -in "$F" | pg_restore --no-owner -d "$SCRATCH"26  if [ "$DB" = sp_vault ]; then check "$DB" "$SCRATCH" accounts devices vaults records audit_events; else check "$DB" "$SCRATCH" secret_values; fi27  ls -1t "$OUT"/$DB-*.dump.enc | tail -n +31 | xargs -r rm -f28done29rsync -a --delete "$DATA/attachments/" "$OUT/attachments/"30echo "$(date -u +%FT%TZ) backup ok: $STAMP ($(du -sh "$OUT" | cut -f1) total), restore verified"31
Files main
Menu

Theme