Files
kvitt/infra/backup/backup.sh
T
jacobh2 c885aba313 M2c-dev: development compose stack, backup job, Firebase setup docs
infra/dev: compose stack on the Mac (server kvitt-server:dev, on-demand backup),
.env.example, build/up/logs/backup/copy-google-services scripts. No Caddy, no TLS
(debug builds accept http to private LAN addresses, AC-2.8). Bind-mounted data.
infra/backup: alpine 3.24.2 image and backup.sh (SPEC 10.9; Drive upload skipped
when RCLONE_FOLDER_ID is empty). infra/README.md for the dev stack and Firebase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 21:18:36 +02:00

60 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
# Kvitt backup job (SPEC §10.9). Runs inside the backup image with /data (server data) and /backups mounted.
# 1. online, consistent snapshot of /data/kvitt.sqlite with sqlite3 .backup
# 2. PRAGMA integrity_check on the snapshot must print ok, else abort
# 3. gzip -9 the snapshot and tar the pictures, both with the same UTC stamp
# 4. Drive upload: skipped when RCLONE_FOLDER_ID is empty (the development stack, D-001);
# the upload itself and the D-002 local deletion are built by M2c-pi
# 5. write /backups/last-success with the stamp
# Logs carry file names and sizes only, never database content (SPEC §8.7).
set -eu
DATA_DIR=${KVITT_DATA_DIR:-/data}
BACKUP_DIR=${BACKUP_DIR:-/backups}
DB="$DATA_DIR/kvitt.sqlite"
STAGING="$BACKUP_DIR/staging"
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) backup: $*"; }
fail() { log "FAILED: $*"; rm -f "$STAGING/kvitt.sqlite"; exit 1; }
[ -f "$DB" ] || fail "no database at $DB"
mkdir -p "$STAGING"
rm -f "$STAGING/kvitt.sqlite"
# 1. Snapshot
sqlite3 -batch "$DB" ".backup '$STAGING/kvitt.sqlite'" || fail "sqlite3 .backup returned an error"
# 2. Integrity check
result=$(sqlite3 -batch -noheader -list "$STAGING/kvitt.sqlite" "PRAGMA integrity_check") || fail "integrity_check could not run"
[ "$result" = "ok" ] || fail "integrity_check did not print ok"
# 3. Compress with one UTC stamp
stamp=$(date -u +%Y%m%dT%H%M%SZ)
db_out="$BACKUP_DIR/kvitt-$stamp.sqlite.gz"
pics_out="$BACKUP_DIR/pictures-$stamp.tar.gz"
gzip -9 -c "$STAGING/kvitt.sqlite" > "$db_out.tmp" || fail "gzip failed"
mv "$db_out.tmp" "$db_out"
rm -f "$STAGING/kvitt.sqlite"
if [ -d "$DATA_DIR/pictures" ]; then
tar -czf "$pics_out.tmp" -C "$DATA_DIR" pictures || fail "tar of pictures failed"
else
log "no pictures directory yet; writing an empty pictures archive"
tar -czf "$pics_out.tmp" -T /dev/null || fail "tar of empty pictures archive failed"
fi
mv "$pics_out.tmp" "$pics_out"
log "wrote $(basename "$db_out") ($(wc -c < "$db_out") bytes) and $(basename "$pics_out") ($(wc -c < "$pics_out") bytes)"
# 4. Drive upload
if [ -z "${RCLONE_FOLDER_ID:-}" ]; then
log "RCLONE_FOLDER_ID is empty: Drive upload skipped, local files kept"
else
# TODO(M2c-pi): rclone copy --immutable to "${RCLONE_REMOTE:-gdrive:}" --drive-root-folder-id, confirm with
# rclone lsf, then delete the local files (D-002). Not part of the development stack (D-001).
fail "RCLONE_FOLDER_ID is set but the Drive upload is not built yet (M2c-pi); local files kept"
fi
# 5. Success marker
echo "$stamp" > "$BACKUP_DIR/last-success.tmp"
mv "$BACKUP_DIR/last-success.tmp" "$BACKUP_DIR/last-success"
log "ok $stamp"