Repository Public
1#!/bin/bash2# Daily backup of the SP TV database (favourites, lists, hidden channels, history, settings, passkeys, catalogue).3# Consistent online copy (sqlite3 .backup), integrity check, gzip, 14 days kept. Run by PM2 (cron) as sp-tv-backup.4# Restore: see DEPLOYMENT.md → Sauvegarde et restauration.5set -euo pipefail6cd "$(dirname "$0")/.."7DATA_DIR="${DATA_DIR:-}"; BACKUP_DIR="${BACKUP_DIR:-}"8if [ -f .env ]; then9 while IFS='=' read -r k v; do10 case "$k" in DATA_DIR|BACKUP_DIR|DATABASE_PATH) v="${v%\"}"; v="${v#\"}"; [ -n "${!k:-}" ] || export "$k=$v" ;; esac11 done < .env12fi13: "${DATA_DIR:?DATA_DIR manquant}"14DB="${DATABASE_PATH:-$DATA_DIR/sp-tv.db}"15BACKUP_DIR="${BACKUP_DIR:-$HOME/backups/sp-tv}"16[ -f "$DB" ] || { echo "base introuvable : $DB" >&2; exit 1; }17umask 077; mkdir -p "$BACKUP_DIR"; chmod 700 "$BACKUP_DIR"18out="$BACKUP_DIR/sp-tv-$(date +%Y%m%d-%H%M%S).db"19/usr/bin/sqlite3 "$DB" ".timeout 20000" ".backup '$out'"20check="$(/usr/bin/sqlite3 "$out" 'PRAGMA integrity_check;')"21[ "$check" = "ok" ] || { echo "sauvegarde corrompue : $check" >&2; rm -f "$out"; exit 1; }22favs="$(/usr/bin/sqlite3 "$out" 'SELECT COUNT(*) FROM list_items;')"23rm -f "$out-wal" "$out-shm" # side files left by the checks above; the copy itself is complete24gzip -f "$out"25/usr/bin/find "$BACKUP_DIR" -name 'sp-tv-*.db.gz' -mtime +14 -delete26/usr/bin/find "$BACKUP_DIR" \( -name 'sp-tv-*.db-wal' -o -name 'sp-tv-*.db-shm' \) -delete27echo "$(date '+%Y-%m-%d %H:%M:%S') sauvegarde ok : $out.gz ($(du -h "$out.gz" | cut -f1), $favs favoris)"28