#!/bin/sh # Trouve-KA — snapshot quotidien OpenSearch (node search M2M32b) # Author: Simon-Pierre Boucher # Contact: contact@spboucher.ai # # Snapshots natifs OpenSearch (incrémentaux entre eux) dans un dossier hôte # monté dans le container (path.repo=/mnt/snapshots). Rotation 7 jours. # Restauration : POST _snapshot/trouveka-fs//_restore # (index fermé ou renommé via rename_pattern au besoin). PATH="$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" export PATH LOG="$HOME/trouveka-backups/backup.log" STAMP=$(date '+%Y%m%d') OS=http://localhost:9200 mkdir -p "$HOME/trouveka-backups" log() { echo "$(date '+%F %T') [os] $*" >> "$LOG"; } # Dépôt de snapshots (idempotent) curl -s -m 20 -X PUT "$OS/_snapshot/trouveka-fs" -H 'Content-Type: application/json' \ -d '{"type":"fs","settings":{"location":"/mnt/snapshots","compress":true}}' >/dev/null 2>&1 RES=$(curl -s -m 590 -X PUT "$OS/_snapshot/trouveka-fs/daily-$STAMP?wait_for_completion=true" \ -H 'Content-Type: application/json' \ -d '{"indices":"trouveka-docs-v2","include_global_state":false}') case "$RES" in *'"state":"SUCCESS"'*) log "snapshot daily-$STAMP OK" ;; *already*exists*) log "snapshot daily-$STAMP déjà présent" ;; *) log "ÉCHEC snapshot : $(echo "$RES" | head -c 200)"; exit 1 ;; esac # Rotation : supprimer les snapshots > 7 jours (via l'API, jamais le filesystem) CUTOFF=$(date -v-7d '+%Y%m%d' 2>/dev/null || date -d '7 days ago' '+%Y%m%d') for snap in $(curl -s -m 30 "$OS/_snapshot/trouveka-fs/_all" | grep -o '"snapshot":"daily-[0-9]*"' | grep -o 'daily-[0-9]*'); do d=${snap#daily-} if [ "$d" -lt "$CUTOFF" ] 2>/dev/null; then curl -s -m 60 -X DELETE "$OS/_snapshot/trouveka-fs/$snap" >/dev/null log "rotation : $snap supprimé" fi done exit 0