#!/bin/bash # # Script de test des notifications (ntfy et Uptime Kuma) # Usage: ./test-notifications.sh # # Couleurs RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Charger les variables d'environnement if [ -f /etc/borgmatic/.env ]; then source /etc/borgmatic/.env elif [ -f .env ]; then source .env else echo -e "${RED}❌ Fichier .env non trouvé${NC}" echo "Créez un fichier .env avec NTFY_URL et NTFY_USER" exit 1 fi if [ -z "$NTFY_URL" ]; then echo -e "${RED}❌ NTFY_URL non défini dans .env${NC}" exit 1 fi echo -e "${BLUE}==================================================${NC}" echo -e "${BLUE}Test des notifications ntfy${NC}" echo -e "${BLUE}==================================================${NC}" echo "" echo -e "${YELLOW}Configuration:${NC}" echo " URL: $NTFY_URL" echo " Auth: $([ -n "$NTFY_USER" ] && echo "✅ Configurée" || echo "❌ Non configurée")" echo "" # Test 1: Notification simple echo -e "${YELLOW}📤 Test 1: Notification simple...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" -u "$NTFY_USER" \ -H "Title: Test Borgmatic" \ -H "Priority: default" \ -H "Tags: test" \ -d "Ceci est un test de notification depuis le script de configuration Borgmatic" \ "$NTFY_URL") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Notification simple envoyée avec succès${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" echo "$RESPONSE" exit 1 fi sleep 2 # Test 2: Notification de succès (simule un backup réussi) echo -e "${YELLOW}📤 Test 2: Notification de succès de backup...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" -u "$NTFY_USER" \ -H "Title: ✅ Backup réussi" \ -H "Priority: default" \ -H "Tags: white_check_mark,backup" \ -d "Test de notification de succès Archive: test-backup-$(date +%Y%m%d-%H%M) Date: $(date '+%Y-%m-%d %H:%M:%S') Taille: 15.2 GB" \ "$NTFY_URL") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Notification de succès envoyée avec succès${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" exit 1 fi sleep 2 # Test 3: Notification d'erreur (simule un backup échoué) echo -e "${YELLOW}📤 Test 3: Notification d'erreur de backup...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" -u "$NTFY_USER" \ -H "Title: ❌ Backup échoué" \ -H "Priority: high" \ -H "Tags: x,backup,alert" \ -d "Test de notification d'erreur Erreur: Ceci est un test - pas de vraie erreur ! Date: $(date '+%Y-%m-%d %H:%M:%S') Vérifiez les logs: journalctl -u borgmatic.service -n 50" \ "$NTFY_URL") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Notification d'erreur envoyée avec succès${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" exit 1 fi echo "" echo -e "${BLUE}==================================================${NC}" echo -e "${BLUE}Test des notifications Uptime Kuma${NC}" echo -e "${BLUE}==================================================${NC}" echo "" # Vérifier si Uptime Kuma est configuré if [ -z "$UPTIME_KUMA_PUSH_URL" ]; then echo -e "${YELLOW}⚠️ UPTIME_KUMA_PUSH_URL non configuré${NC}" echo "Ignoré - configurez UPTIME_KUMA_PUSH_URL dans .env pour tester Uptime Kuma" else echo -e "${YELLOW}Configuration:${NC}" echo " URL: ${UPTIME_KUMA_PUSH_URL}" echo "" # Test 4: Ping Uptime Kuma (succès) echo -e "${YELLOW}📤 Test 4: Ping Uptime Kuma (status=up)...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" "${UPTIME_KUMA_PUSH_URL}?status=up&msg=Test%20success&ping=") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Ping Uptime Kuma (success) envoyé avec succès${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" echo "$RESPONSE" fi sleep 2 # Test 5: Ping Uptime Kuma (erreur) echo -e "${YELLOW}📤 Test 5: Ping Uptime Kuma (status=down)...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" "${UPTIME_KUMA_PUSH_URL}?status=down&msg=Test%20error&ping=") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Ping Uptime Kuma (error) envoyé avec succès${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" fi sleep 2 # Test 6: Remettre le statut à "up" echo -e "${YELLOW}📤 Test 6: Remettre Uptime Kuma à up...${NC}" RESPONSE=$(curl -s -w "\n%{http_code}" "${UPTIME_KUMA_PUSH_URL}?status=up&msg=Test%20complete&ping=") HTTP_CODE=$(echo "$RESPONSE" | tail -1) if [ "$HTTP_CODE" -eq 200 ]; then echo -e "${GREEN}✅ Statut remis à up${NC}" else echo -e "${RED}❌ Échec (HTTP $HTTP_CODE)${NC}" fi fi echo "" echo -e "${GREEN}==================================================${NC}" echo -e "${GREEN}✅ Tous les tests de notification ont réussi !${NC}" echo -e "${GREEN}==================================================${NC}" echo "" echo -e "${YELLOW}Vérifications:${NC}" echo "" echo -e "${BLUE}Ntfy:${NC}" echo " 1. Notification de test simple" echo " 2. Notification de succès de backup" echo " 3. Notification d'erreur de backup" echo "" if [ -n "$UPTIME_KUMA_PUSH_URL" ]; then echo -e "${BLUE}Uptime Kuma:${NC}" echo " 4. Monitor devrait montrer un changement up → down → up" echo " 5. Vérifiez l'historique dans le dashboard Uptime Kuma" echo "" fi