HomeLab/infra_conf/protonsync.sh
griffix 58ed343aa7
backups pi5
maj des backups + discord notification
2025-09-23 19:52:45 +02:00

115 lines
No EOL
3 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
# -------------------------------------------------
# Rclone sync + Discord webhook (via ENV)
# -------------------------------------------------
# ---------- Vérification de la variable ----------
: "${DISCORD_WEBHOOK:?La variable d'environnement DISCORD_WEBHOOK n'est pas définie.}"
# ---------- Configuration ----------
SRC="/media/Seagate/archives/"
DST="remote:/backups/"
LOG_DIR="/media/Seagate/backups/"
mkdir -p "$LOG_DIR"
DATE=$(date +"%Y%m%d_%H%M%S")
TXT_LOG="${LOG_DIR}sync_${DATE}.log"
TMP_OUT=$(mktemp)
# ---------- Options Rclone ----------
RCLONE_OPTS=(
--transfers 1
--drive-chunk-size 32M
--protondrive-replace-existing-draft=true
--tpslimit 5
--timeout 30s
--low-level-retries 10
--stats 1s
--stats-one-line
)
# ---------- Lancement ----------
START_EPOCH=$(date +%s)
START_HUMAN=$(date +"%Y-%m-%d %H:%M:%S")
echo "▶️ Démarrage : $START_HUMAN"
rclone sync "$SRC" "$DST" "${RCLONE_OPTS[@]}" >"$TMP_OUT" 2>&1
RC=$?
END_EPOCH=$(date +%s)
END_HUMAN=$(date +"%Y-%m-%d %H:%M:%S")
DURATION=$((END_EPOCH-START_EPOCH))
# ---------- Extraction des stats ----------
STATS_LINE=$(grep -i "Transferred:" "$TMP_OUT" | tail -n1 || true)
[[ -z "$STATS_LINE" ]] && STATS_LINE="Aucune statistique disponible."
# ---------- Construction du rapport ----------
REPORT=$(cat <<EOF
**🗂️ Rapport de sauvegarde Rclone**
**Début** : $START_HUMAN
**Fin** : $END_HUMAN
**Durée** : ${DURATION}s
**État** : $(if [[ $RC -eq 0 ]]; then echo "✅ Succès"; else echo "❌ Erreur (code $RC)"; fi)
**Statistiques**
\`\`\`
$STATS_LINE
\`\`\`
EOF
)
# Enregistrement du rapport texte
echo "$REPORT" >"$TXT_LOG"
# ---------- Envoi vers Discord ----------
# Valeur par défaut au cas où RC ne serait pas défini
RC=${RC:-1}
# Couleur selon le résultat (vert = succès, rouge = échec)
COLOR=$([ "$RC" -eq 0 ] && echo 3066993 || echo 15158332)
# Construction sécurisée du JSON avec jq
payload=$(jq -n \
--arg username "Rclone-Backup" \
--arg description "$REPORT" \
--argjson color "$COLOR" \
'
{
username: $username,
avatar_url: $avatar,
allowed_mentions: { parse: [] },
embeds: [
{
title: "Rapport de sauvegarde",
description: $description,
color: $color
}
]
}
')
# Envoi avec curl, on capture le code HTTP pour le logging
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST -H "Content-Type: application/json" \
-d "$payload" "$DISCORD_WEBHOOK")
if [[ "$http_code" != "204" ]]; then
echo "$(date) Webhook Discord échoué (HTTP $http_code)" >> ${LOG_DIR}/discord_error.log
echo $DISCORD_WEBHOOK >> ${LOG_DIR}/discord_error.log
echo $payload >> ${LOG_DIR}/discord_error.log
fi
# ---------- Affichage final ----------
echo "$REPORT"
echo "📁 Log complet → $TXT_LOG"
# Nettoyage
rm -f "$TMP_OUT"
echo "🧹 Suppression des logs de plus de 30jours…"
find "${LOG_DIR}" -type f -name "*.log" -mtime +30 -print -delete
exit $RC