#!/bin/bash # --- Caricamento variabili d'ambiente --- if [ -f .env ]; then export $(grep -v '^#' .env | xargs) else echo "❌ Errore: File .env non trovato." exit 1 fi # --- Configurazione FTP --- FTP_HOST="ftp.canticristiani.it" FTP_USER="canticristiani.it" FTP_PASS="$FTP_PASSWORD" SOURCE_URL="https://libretto.mmcinet.eu/canti/api/get_all_app_tables" REMOTE_PATH="api/canti.json" if [ -z "$FTP_PASS" ]; then echo "❌ Errore: FTP_PASSWORD non definita nel file .env" exit 1 fi # --- Download del file --- echo "⬇️ Scaricamento dati da $SOURCE_URL..." TEMP_FILE=$(mktemp) if ! curl -s -L "$SOURCE_URL" -o "$TEMP_FILE"; then echo "❌ Errore nel download dei dati." rm "$TEMP_FILE" exit 1 fi # --- Rotazione e Caricamento file (FTP o VPS) --- if [ -n "$VPS_HOST" ]; then echo "🔄 Rotazione file su VPS ($VPS_HOST)..." ssh "$VPS_USER@$VPS_HOST" "mkdir -p $VPS_PATH/api && mv $VPS_PATH/api/canti.json $VPS_PATH/api/canti_ex.json 2>/dev/null || true" echo "🚀 Caricamento nuovo file su VPS via SCP..." if scp "$TEMP_FILE" "$VPS_USER@$VPS_HOST:$VPS_PATH/$REMOTE_PATH" && ssh "$VPS_USER@$VPS_HOST" "chmod 644 $VPS_PATH/$REMOTE_PATH"; then echo "✅ Allineamento su VPS completato con successo!" else echo "❌ Errore durante il caricamento via SCP su VPS." rm "$TEMP_FILE" exit 1 fi else echo "🔄 Rotazione file su FTP (canti.json -> canti_ex.json)..." # Rinominiamo canti.json in canti_ex.json. # Usiamo i percorsi assoluti per sicurezza. curl -s -u "$FTP_USER:$FTP_PASS" \ --ftp-pasv \ -Q "*DELE /htdocs/api/canti_ex.json" \ -Q "*RNFR /htdocs/api/canti.json" \ -Q "*RNTO /htdocs/api/canti_ex.json" \ "ftp://$FTP_HOST/" > /dev/null echo "🚀 Caricamento nuovo file su FTP ($FTP_HOST) via FTP..." if curl -s -u "$FTP_USER:$FTP_PASS" --ftp-pasv --ftp-create-dirs -T "$TEMP_FILE" "ftp://$FTP_HOST/$REMOTE_PATH"; then echo "✅ Allineamento su FTP completato con successo!" else echo "❌ Errore durante il caricamento FTP." rm "$TEMP_FILE" exit 1 fi fi # Cleanup rm "$TEMP_FILE"