Script de backup restic pour Lisette et Bermite
Pull depuis ce PC via SSH : snapshot SQLite à chaud (API backup de Python, sans fichier temporaire sur le serveur), vérification d'intégrité locale, puis restic backup taggé par service. Rétention configurable, isolation des échecs par service, commande `list`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f6053bb459
8 files changed
+267
No files matched your search
@@ -0,0 +1,67 @@
|
||||
# Fonctions partagées — sourcé par backup.sh.
|
||||
|
||||
log() { printf '\033[1;34m[%s]\033[0m %s\n' "$(date +%H:%M:%S)" "$*" >&2; }
|
||||
warn() { printf '\033[1;33m[warn]\033[0m %s\n' "$*" >&2; }
|
||||
die() { printf '\033[1;31m[erreur]\033[0m %s\n' "$*" >&2; exit 1; }
|
||||
|
||||
load_config() {
|
||||
local cfg="$ROOT/config.env"
|
||||
[[ -f "$cfg" ]] || die "config.env manquant : cp config.env.example config.env"
|
||||
# shellcheck source=/dev/null
|
||||
source "$cfg"
|
||||
: "${RESTIC_REPOSITORY:?}" "${RESTIC_PASSWORD_FILE:?}" "${STAGING_DIR:?}"
|
||||
export RESTIC_REPOSITORY RESTIC_PASSWORD_FILE
|
||||
}
|
||||
|
||||
# ssh_sqlite_snapshot <hôte> <chemin distant .db> <fichier local de destination>
|
||||
ssh_sqlite_snapshot() {
|
||||
local host=$1 src=$2 dst=$3
|
||||
log "snapshot SQLite $host:$src"
|
||||
ssh -o BatchMode=yes "$host" python3 - "$(printf '%q' "$src")" \
|
||||
< "$ROOT/lib/sqlite_snapshot.py" > "$dst"
|
||||
verify_sqlite "$dst"
|
||||
}
|
||||
|
||||
# verify_sqlite <fichier .db local>
|
||||
verify_sqlite() {
|
||||
local res
|
||||
res=$(sqlite3 "$1" 'PRAGMA integrity_check;') || die "lecture impossible : $1"
|
||||
[[ "$res" == "ok" ]] || die "intégrité KO pour $1 : $res"
|
||||
}
|
||||
|
||||
# pull_files <hôte> <dossier local> <chemin distant>...
|
||||
pull_files() {
|
||||
local host=$1 dest=$2; shift 2
|
||||
local srcs=() p
|
||||
for p in "$@"; do srcs+=("$host:$p"); done
|
||||
log "rsync ${srcs[*]}"
|
||||
rsync -a --protect-args "${srcs[@]}" "$dest/"
|
||||
}
|
||||
|
||||
# restic_store <service> <hôte> <dossier staging>
|
||||
restic_store() {
|
||||
local service=$1 host=$2 dir=$3
|
||||
log "restic backup ($service)"
|
||||
( cd "$dir" && restic backup --quiet --host "$host" --tag "$service" . )
|
||||
}
|
||||
|
||||
restic_init() {
|
||||
if [[ ! -f "$RESTIC_PASSWORD_FILE" ]]; then
|
||||
mkdir -p "$(dirname "$RESTIC_PASSWORD_FILE")"
|
||||
( umask 077; head -c 32 /dev/urandom | base64 > "$RESTIC_PASSWORD_FILE" )
|
||||
warn "mot de passe restic généré dans $RESTIC_PASSWORD_FILE"
|
||||
warn "=> COPIE-LE AILLEURS (Vaultwarden…) : sans lui le dépôt est irrécupérable."
|
||||
fi
|
||||
if restic cat config >/dev/null 2>&1; then
|
||||
log "dépôt déjà initialisé : $RESTIC_REPOSITORY"
|
||||
else
|
||||
mkdir -p "$RESTIC_REPOSITORY"
|
||||
restic init
|
||||
fi
|
||||
}
|
||||
|
||||
restic_retention() {
|
||||
log "rétention (d=$KEEP_DAILY w=$KEEP_WEEKLY m=$KEEP_MONTHLY)"
|
||||
restic forget --quiet --prune --group-by host,tags \
|
||||
--keep-daily "$KEEP_DAILY" --keep-weekly "$KEEP_WEEKLY" --keep-monthly "$KEEP_MONTHLY"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Snapshot cohérent d'une base SQLite, écrit sur stdout.
|
||||
|
||||
Exécuté côté serveur via : ssh hôte python3 - <chemin.db> < sqlite_snapshot.py
|
||||
Utilise l'API backup de SQLite : fonctionne à chaud, respecte le WAL.
|
||||
Uniquement la stdlib (le serveur n'a pas forcément le binaire sqlite3).
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
src = sys.argv[1]
|
||||
if not os.path.isfile(src):
|
||||
sys.exit(f"base introuvable : {src}")
|
||||
|
||||
# mode=rw : ne jamais créer une base vide si le chemin est faux.
|
||||
source = sqlite3.connect(f"file:{src}?mode=rw", uri=True)
|
||||
fd, tmp = tempfile.mkstemp(suffix=".db")
|
||||
os.close(fd)
|
||||
try:
|
||||
dest = sqlite3.connect(tmp)
|
||||
with dest:
|
||||
source.backup(dest)
|
||||
dest.close()
|
||||
source.close()
|
||||
with open(tmp, "rb") as f:
|
||||
shutil.copyfileobj(f, sys.stdout.buffer)
|
||||
sys.stdout.buffer.flush()
|
||||
finally:
|
||||
os.unlink(tmp)
|
||||
Reference in new issue
Block a user