# 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 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 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 ... 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 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" }