#!/usr/bin/env bash # backup-restore-test.sh — verify the most recent backup is usable. # # Failure of this script is an alert. It does NOT touch the live # database or data dir. # # Steps: # 1. Pick the newest archive in $BACKUP_DIR. # 2. Extract into a scratch tmpdir. # 3. Run sqlite3 ".schema" against the snapshotted DB; non-empty # output means the file is readable as SQLite. # 4. Run tar -tzf on notes.tar.gz; we expect at least one entry. # 5. Print the info.txt header so logs include backup metadata. set -euo pipefail BACKUP_DIR="${BACKUP_DIR:-/var/backups/librenotes}" archive="$(ls -1t "$BACKUP_DIR"/librenotes-*.tar.gz 2>/dev/null | head -n 1 || true)" if [ -z "$archive" ]; then echo "no archives in $BACKUP_DIR" >&2 exit 1 fi work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT tar -C "$work" -xzf "$archive" if [ ! -f "$work/librenotes.db" ]; then echo "archive missing librenotes.db: $archive" >&2 exit 1 fi schema="$(sqlite3 "$work/librenotes.db" ".schema" || true)" if [ -z "$schema" ]; then echo "sqlite .schema returned empty for $archive" >&2 exit 1 fi if [ ! -f "$work/notes.tar.gz" ]; then echo "archive missing notes.tar.gz: $archive" >&2 exit 1 fi n="$(tar -tzf "$work/notes.tar.gz" | wc -l)" if [ "$n" -lt 1 ]; then echo "notes.tar.gz contains no entries: $archive" >&2 exit 1 fi echo "verified $archive" [ -f "$work/info.txt" ] && cat "$work/info.txt"