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:
Romulus21andClaude Opus 5.5 committed 2026-11-09 16:32:49 +01:00
commit f6053bb459
8 files changed
+267

No files matched your search

+67
View File
@@ -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"
}