Files
agence66-borgmatic/install.sh
2025-12-16 04:41:26 +01:00

154 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
#
# Script d'installation de Borgmatic pour Agence66
# Usage: sudo ./install.sh
#
set -e
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}==================================================${NC}"
echo -e "${GREEN}Installation de Borgmatic - Agence66${NC}"
echo -e "${GREEN}==================================================${NC}"
echo ""
# Vérifier que le script est exécuté en root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}❌ Ce script doit être exécuté en root (sudo)${NC}"
exit 1
fi
# Détecter la distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
echo -e "${RED}❌ Impossible de détecter la distribution${NC}"
exit 1
fi
# 1. Installation de Borgmatic et Borg
echo -e "${YELLOW}📦 Installation de Borgmatic et Borg...${NC}"
case $OS in
ubuntu|debian)
apt-get update
apt-get install -y borgbackup borgmatic curl
;;
arch|manjaro)
pacman -Syu --noconfirm borgmatic borg curl
;;
fedora|rhel|centos)
dnf install -y borgbackup borgmatic curl
;;
*)
echo -e "${RED}❌ Distribution non supportée: $OS${NC}"
exit 1
;;
esac
echo -e "${GREEN}✅ Borgmatic et Borg installés${NC}"
# 2. Créer les répertoires nécessaires
echo -e "${YELLOW}📁 Création des répertoires...${NC}"
mkdir -p /etc/borgmatic
mkdir -p /etc/borgmatic/hooks
mkdir -p /var/log/borgmatic
echo -e "${GREEN}✅ Répertoires créés${NC}"
# 3. Copier les fichiers de configuration
echo -e "${YELLOW}📋 Copie des fichiers de configuration...${NC}"
# Config principale
cp config.yaml /etc/borgmatic/config.yaml
chmod 600 /etc/borgmatic/config.yaml
# Scripts de hooks
cp hooks/ntfy-success.sh /etc/borgmatic/hooks/
cp hooks/ntfy-error.sh /etc/borgmatic/hooks/
chmod +x /etc/borgmatic/hooks/*.sh
echo -e "${GREEN}✅ Fichiers de configuration copiés${NC}"
# 4. Configuration des variables d'environnement
echo -e "${YELLOW}🔐 Configuration des variables d'environnement...${NC}"
if [ -f .env ]; then
cp .env /etc/borgmatic/.env
chmod 600 /etc/borgmatic/.env
echo -e "${GREEN}✅ Fichier .env copié${NC}"
else
echo -e "${YELLOW}⚠️ Fichier .env non trouvé${NC}"
echo -e "${YELLOW} Copie du template .env.example...${NC}"
cp .env.example /etc/borgmatic/.env
chmod 600 /etc/borgmatic/.env
echo -e "${RED}⚠️ IMPORTANT: Éditez /etc/borgmatic/.env avec vos vraies valeurs !${NC}"
fi
# 5. Installation des services systemd
echo -e "${YELLOW}⚙️ Installation des services systemd...${NC}"
cp systemd/borgmatic.service /etc/systemd/system/
cp systemd/borgmatic.timer /etc/systemd/system/
systemctl daemon-reload
systemctl enable borgmatic.timer
echo -e "${GREEN}✅ Services systemd installés et activés${NC}"
# 6. Vérification de la configuration
echo -e "${YELLOW}🔍 Validation de la configuration...${NC}"
if borgmatic --version > /dev/null 2>&1; then
echo -e "${GREEN}✅ Borgmatic est installé correctement${NC}"
borgmatic --version
else
echo -e "${RED}❌ Erreur: Borgmatic n'est pas installé correctement${NC}"
exit 1
fi
# Test de la configuration (sans exécuter de backup)
if borgmatic config validate > /dev/null 2>&1; then
echo -e "${GREEN}✅ Configuration valide${NC}"
else
echo -e "${YELLOW}⚠️ La configuration nécessite des ajustements${NC}"
echo -e "${YELLOW} Vérifiez /etc/borgmatic/config.yaml${NC}"
fi
# 7. Affichage des prochaines étapes
echo ""
echo -e "${GREEN}==================================================${NC}"
echo -e "${GREEN}✅ Installation terminée !${NC}"
echo -e "${GREEN}==================================================${NC}"
echo ""
echo -e "${YELLOW}📝 Prochaines étapes:${NC}"
echo ""
echo "1. Éditez les configurations:"
echo " - ${YELLOW}/etc/borgmatic/.env${NC} (variables d'environnement)"
echo " - ${YELLOW}/etc/borgmatic/config.yaml${NC} (configuration Borg)"
echo ""
echo "2. Initialisez votre repository Borg (si nouveau):"
echo " ${YELLOW}borg init --encryption=repokey-blake2 /path/to/repo${NC}"
echo ""
echo "3. Testez votre configuration:"
echo " ${YELLOW}borgmatic --dry-run --verbosity 2${NC}"
echo ""
echo "4. Exécutez un premier backup manuel:"
echo " ${YELLOW}borgmatic --verbosity 1${NC}"
echo ""
echo "5. Vérifiez le timer systemd:"
echo " ${YELLOW}systemctl status borgmatic.timer${NC}"
echo " ${YELLOW}systemctl list-timers | grep borgmatic${NC}"
echo ""
echo "6. Démarrez le timer:"
echo " ${YELLOW}systemctl start borgmatic.timer${NC}"
echo ""
echo -e "${GREEN}Le backup s'exécutera automatiquement tous les jours à 3h du matin${NC}"
echo ""