Files
loogle-scripts/scripts/pi2-backup/auto-git-backup.sh

95 lines
3.4 KiB
Bash
Executable File
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.
#!/bin/bash
# ==============================================================================
# 💾 AUTO GIT BACKUP - DINAMICO
# Sincronizza automaticamente tutti gli script .sh e .py dal Pi2 e dal Pi1
# verso il repository Gitea locale, poi esegue il push.
# ==============================================================================
# --- CONFIGURAZIONE ---
REPO_DIR="/home/daniely/loogle-repo"
LOG_FILE="/var/log/git-backup.log"
DATE=$(date +"%Y-%m-%d %H:%M")
# IP del Pi1 (Master) da cui prelevare i file remoti
PI1_IP="192.168.128.80"
PI1_USER="daniely"
# Redirige tutto l'output (stdout e stderr) nel log
exec >> $LOG_FILE 2>&1
echo "=== Inizio Backup Git: $DATE ==="
# 1. PREPARAZIONE REPOSITORY
# --------------------------
if [ -d "$REPO_DIR" ]; then
cd "$REPO_DIR" || { echo "❌ Errore: Impossibile entrare in $REPO_DIR"; exit 1; }
else
echo "❌ Errore: La cartella $REPO_DIR non esiste."; exit 1;
fi
# Aggiorna il repository locale (pull) per evitare conflitti
echo "🔄 Eseguo Git Pull..."
git pull origin main
# Assicuriamoci che le cartelle di destinazione esistano
mkdir -p ./scripts/pi2-backup
mkdir -p ./services/telegram-bot
mkdir -p ./scripts/pi1-master
mkdir -p ./configs
# 2. RACCOLTA FILE DAL PI-2 (LOCALE)
# ----------------------------------
echo "📂 Raccolta dinamica file locali (Pi-2)..."
# A. Script nella Home (solo .sh) -> scripts/pi2-backup
# --include='*.sh' prende gli script, --exclude='*' ignora tutto il resto
rsync -av --include='*.sh' --exclude='*' /home/daniely/ "$REPO_DIR/scripts/pi2-backup/"
# B. Script del Bot (tutti .py e .sh) -> services/telegram-bot
rsync -av --include='*.py' --include='*.sh' --exclude='*' /home/daniely/docker/telegram-bot/ "$REPO_DIR/services/telegram-bot/"
# C. File di Configurazione specifici (Keepalived)
if [ -f "/etc/keepalived/keepalived.conf" ]; then
sudo cp /etc/keepalived/keepalived.conf ./configs/keepalived_pi2.conf
sudo chown daniely:daniely ./configs/keepalived_pi2.conf
echo "✅ Configurazione Keepalived Pi2 copiata."
fi
# 3. RACCOLTA FILE DAL PI-1 (REMOTO)
# ----------------------------------
echo "📡 Raccolta dinamica file remoti (Pi-1)..."
# A. Script nella Home remota (.sh e .py) -> scripts/pi1-master
# Nota: Richiede che le chiavi SSH siano configurate per non chiedere password
rsync -av -e "ssh -q" --include='*.sh' --include='*.py' --exclude='*' $PI1_USER@$PI1_IP:/home/daniely/ "$REPO_DIR/scripts/pi1-master/"
# B. Recupero file specifici fuori dalla home (Legacy da vecchio script)
# Se dhcp-alert.sh esiste ancora in /usr/local/bin, lo prendiamo
scp -q $PI1_USER@$PI1_IP:/usr/local/bin/dhcp-alert.sh ./scripts/pi1-master/ 2>/dev/null || echo "⚠️ dhcp-alert.sh non trovato su Pi1 (ignorato)"
# C. Configurazione Keepalived Remota
scp -q $PI1_USER@$PI1_IP:/etc/keepalived/keepalived.conf ./configs/keepalived_pi1.conf 2>/dev/null
if [ $? -eq 0 ]; then
echo "✅ Configurazione Keepalived Pi1 scaricata."
else
echo "⚠️ Impossibile scaricare Keepalived conf da Pi1."
fi
# 4. GIT PUSH
# -----------
# Verifica se ci sono cambiamenti reali
if [[ `git status --porcelain` ]]; then
echo "📝 Rilevati cambiamenti. Eseguo Commit e Push..."
git add .
git commit -m "Backup automatico script del $DATE"
git push -u origin main
echo "✅ Push completato con successo."
else
echo " Nessun cambiamento rilevato. Repository già aggiornato."
fi
echo "=== Fine Backup Git ==="