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

115 lines
3.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
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.
# ==============================================================================
set -u
# --- CONFIGURAZIONE ---
REPO_DIR="/home/daniely/loogle-repo"
LOG_FILE="/var/log/git-backup.log"
DATE=$(date +"%Y-%m-%d %H:%M")
export GIT_SSH_COMMAND="ssh -o BatchMode=yes -o ConnectTimeout=10"
# 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 ==="
fail() {
echo "❌ $1"
echo "=== Fine Backup Git (ERRORE) ==="
exit 1
}
# 1. PREPARAZIONE REPOSITORY
# --------------------------
if [ -d "$REPO_DIR" ]; then
cd "$REPO_DIR" || fail "Impossibile entrare in $REPO_DIR"
else
fail "La cartella $REPO_DIR non esiste."
fi
# Aggiorna il repository locale (pull) per evitare conflitti
echo "🔄 Eseguo Git Pull..."
if ! git pull --ff-only origin main; then
echo "⚠️ Git pull fallito (proseguo con commit locale se possibile)."
fi
# 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
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
rsync -av -e "ssh -q" --include='*.sh' --include='*.py' --exclude='*' \
"$PI1_USER@$PI1_IP:/home/daniely/" "$REPO_DIR/scripts/pi1-master/" \
|| echo "⚠️ rsync Pi1 fallito (ignorato)"
# B. Recupero file specifici fuori dalla home (Legacy da vecchio script)
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
if scp -q "$PI1_USER@$PI1_IP:/etc/keepalived/keepalived.conf" ./configs/keepalived_pi1.conf 2>/dev/null; then
echo "✅ Configurazione Keepalived Pi1 scaricata."
else
echo "⚠️ Impossibile scaricare Keepalived conf da Pi1."
fi
# D. Sorgente Loogle MCP (app completa: qui vive solo su Pi1, fuori da git)
# .env resta escluso: contiene JWT secret e token Paperless/Gitea/OpenAI.
mkdir -p "$REPO_DIR/services/loogle-mcp"
rsync -av --delete -e "ssh -q" \
--exclude='.env' --exclude='data/' --exclude='__pycache__/' --exclude='*.pyc' --exclude='.cursor/' \
"$PI1_USER@$PI1_IP:/home/daniely/docker/loogle-mcp/" "$REPO_DIR/services/loogle-mcp/" \
|| echo "⚠️ rsync sorgente MCP fallito (ignorato)"
# 4. GIT PUSH
# -----------
if [[ -n $(git status --porcelain) ]]; then
echo "📝 Rilevati cambiamenti. Eseguo Commit e Push..."
git add .
git commit -m "Backup automatico script del $DATE" || fail "Commit fallito"
if git push origin main; then
echo "✅ Push completato con successo."
else
fail "Push su Gitea fallito (SSH/auth/Gitea down?)."
fi
else
echo "️ Nessun cambiamento rilevato. Repository già aggiornato."
fi
echo "=== Fine Backup Git ==="