#!/bin/bash # ============================================================================= # NETGESCON - SINCRONIZZAZIONE COMPLETA A MASTER # ============================================================================= # Script per sincronizzare TUTTO il progetto NetGescon verso MASTER # Mantiene struttura identica tra locale e remoto # # Creato: 19/07/2025 # Uso: ./sync-complete-netgescon.sh [opzioni] # ============================================================================= # Configurazione colori RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Configurazione base SOURCE_BASE="$HOME/netgescon/" TARGET_MASTER="netgescon@192.168.0.201:/var/www/netgescon/" LOG_FILE="$HOME/netgescon/log/sync-complete-$(date +%Y%m%d-%H%M%S).log" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') # Crea directory log se non esiste mkdir -p "$HOME/netgescon/log" # Funzione di logging log() { local level=$1 shift local message="$*" echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message" | tee -a "$LOG_FILE" } # Funzione di stampa colorata print_status() { local color=$1 local message=$2 echo -e "${color}$message${NC}" log "INFO" "$message" } # Funzione di controllo prerequisiti check_prerequisites() { print_status "$BLUE" "๐Ÿ” Controllo prerequisiti sincronizzazione completa..." # Verifica rsync if ! command -v rsync &> /dev/null; then print_status "$RED" "โŒ ERRORE: rsync non installato" exit 1 fi # Verifica directory sorgente if [ ! -d "$SOURCE_BASE" ]; then print_status "$RED" "โŒ ERRORE: Directory NetGescon non trovata: $SOURCE_BASE" exit 1 fi # Verifica connettivitร  SSH local host=$(echo "$TARGET_MASTER" | cut -d':' -f1) ssh -o ConnectTimeout=5 -o BatchMode=yes "$host" exit 2>/dev/null if [ $? -ne 0 ]; then print_status "$RED" "โŒ ERRORE: Connessione SSH fallita verso $host" print_status "$YELLOW" " Verificare connettivitร  e chiavi SSH" exit 1 fi print_status "$GREEN" "โœ… Tutti i prerequisiti soddisfatti" } # Funzione di statistiche pre-sync show_source_stats() { print_status "$BLUE" "๐Ÿ“Š Statistiche progetto NetGescon locale:" local total_files=$(find "$SOURCE_BASE" -type f | wc -l) local total_dirs=$(find "$SOURCE_BASE" -type d | wc -l) local md_files=$(find "$SOURCE_BASE" -name "*.md" | wc -l) local php_files=$(find "$SOURCE_BASE" -name "*.php" | wc -l) local js_files=$(find "$SOURCE_BASE" -name "*.js" | wc -l) local img_files=$(find "$SOURCE_BASE" -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.gif" | wc -l) local total_size=$(du -sh "$SOURCE_BASE" | cut -f1) echo " ๐Ÿ“„ File totali: $total_files" echo " ๐Ÿ“ Directory: $total_dirs" echo " ๐Ÿ“ Markdown: $md_files" echo " ๐Ÿ˜ PHP: $php_files" echo " ๐Ÿ“œ JavaScript: $js_files" echo " ๐Ÿ–ผ๏ธ Immagini: $img_files" echo " ๐Ÿ’พ Dimensione totale: $total_size" echo "" log "STATS" "Files: $total_files, Dirs: $total_dirs, MD: $md_files, PHP: $php_files, Size: $total_size" } # Funzione per mostrare struttura che verrร  sincronizzata show_sync_structure() { print_status "$BLUE" "๐Ÿ—‚๏ธ Struttura che verrร  sincronizzata:" echo "" echo "LOCALE โ†’ MASTER:" echo " ~/netgescon/netgescon-laravel/ โ†’ /var/www/netgescon/netgescon-laravel/" echo " ~/netgescon/docs/ โ†’ /var/www/netgescon/docs/" echo " ~/netgescon/scripts/ โ†’ /var/www/netgescon/scripts/" echo " ~/netgescon/sync-docs-rsync.sh โ†’ /var/www/netgescon/sync-docs-rsync.sh" echo " ~/netgescon/sync-docs-config.env โ†’ /var/www/netgescon/sync-docs-config.env" echo " ~/netgescon/[altri file] โ†’ /var/www/netgescon/[altri file]" echo "" print_status "$YELLOW" "โš ๏ธ TUTTO il contenuto di ~/netgescon/ sarร  sincronizzato" } # Funzione di sincronizzazione principale perform_complete_sync() { print_status "$YELLOW" "๐Ÿš€ Avvio sincronizzazione completa NetGescon..." echo "" # Conferma dell'utente if [[ "${FORCE_MODE:-}" != "1" ]]; then print_status "$YELLOW" "โš ๏ธ ATTENZIONE: Sincronizzazione completa verso MASTER" print_status "$BLUE" " Sorgente: $SOURCE_BASE" print_status "$BLUE" " Destinazione: $TARGET_MASTER" echo "" read -p "Continuare? (y/N): " confirm if [[ "$confirm" != [yY] ]]; then print_status "$YELLOW" "โน๏ธ Sincronizzazione annullata" exit 0 fi fi # Backup remoto prima della sincronizzazione print_status "$BLUE" "๐Ÿ’พ Backup ambiente remoto..." local remote_host=$(echo "$TARGET_MASTER" | cut -d':' -f1) local remote_path=$(echo "$TARGET_MASTER" | cut -d':' -f2) ssh "$remote_host" " if [ -d '$remote_path' ]; then sudo cp -r '$remote_path' '${remote_path}-backup-$(date +%Y%m%d-%H%M%S)' echo 'Backup remoto completato' else sudo mkdir -p '$remote_path' echo 'Directory remota creata' fi " # Opzioni rsync per sincronizzazione completa local rsync_opts="-avz --progress --delete" rsync_opts+=" --exclude='.git'" rsync_opts+=" --exclude='node_modules'" rsync_opts+=" --exclude='vendor'" rsync_opts+=" --exclude='storage/logs/*'" rsync_opts+=" --exclude='.env'" rsync_opts+=" --exclude='*.tmp'" rsync_opts+=" --exclude='*~'" rsync_opts+=" --exclude='.DS_Store'" rsync_opts+=" --exclude='Thumbs.db'" rsync_opts+=" --exclude='__pycache__'" # Esclusioni specifiche (materiali Windows e backup process) rsync_opts+=" --exclude='docs/04-materiali-windows/'" rsync_opts+=" --exclude='docs/05-backup-unificazione/scripts-processo/'" print_status "$BLUE" "๐Ÿ”„ Sincronizzazione in corso..." local sync_start=$(date +%s) if eval rsync $rsync_opts "$SOURCE_BASE" "$TARGET_MASTER" 2>&1 | tee -a "$LOG_FILE"; then local sync_end=$(date +%s) local sync_duration=$((sync_end - sync_start)) print_status "$GREEN" "โœ… Sincronizzazione completata con successo!" print_status "$BLUE" " Durata: ${sync_duration}s" log "SUCCESS" "Sync completa completata in ${sync_duration}s" return 0 else print_status "$RED" "โŒ ERRORE durante sincronizzazione" log "ERROR" "Sync completa fallita" return 1 fi } # Funzione di verifica post-sync verify_sync() { print_status "$BLUE" "๐Ÿ” Verifica post-sincronizzazione..." local remote_host=$(echo "$TARGET_MASTER" | cut -d':' -f1) local remote_path=$(echo "$TARGET_MASTER" | cut -d':' -f2) # Verifica presenza file chiave local key_files=( "netgescon-laravel/composer.json" "docs/00-PIANO-LAVORO-MASTER.md" "docs/00-COPILOT-HANDOFF-MASTER.md" "sync-docs-rsync.sh" ) print_status "$BLUE" " Verifica file chiave sul MASTER:" local all_ok=true for file in "${key_files[@]}"; do if ssh "$remote_host" "[ -f '$remote_path$file' ]"; then echo " โœ… $file" else echo " โŒ $file [MANCANTE]" all_ok=false fi done if $all_ok; then print_status "$GREEN" "โœ… Verifica completata - tutti i file chiave presenti" # Statistiche remote local remote_files=$(ssh "$remote_host" "find '$remote_path' -type f | wc -l") local remote_size=$(ssh "$remote_host" "du -sh '$remote_path' | cut -f1") print_status "$BLUE" "๐Ÿ“Š Statistiche MASTER dopo sync:" echo " ๐Ÿ“„ File: $remote_files" echo " ๐Ÿ’พ Dimensione: $remote_size" return 0 else print_status "$RED" "โŒ Verifica fallita - alcuni file mancanti" return 1 fi } # Funzione di setup post-sync post_sync_setup() { print_status "$BLUE" "โš™๏ธ Setup post-sincronizzazione..." local remote_host=$(echo "$TARGET_MASTER" | cut -d':' -f1) local remote_path=$(echo "$TARGET_MASTER" | cut -d':' -f2) # Imposta permessi corretti ssh "$remote_host" " cd '$remote_path' # Permessi script sudo chmod +x sync-docs-rsync.sh sudo chmod +x docs/03-scripts-automazione/*.sh # Permessi Laravel if [ -d 'netgescon-laravel' ]; then sudo chown -R www-data:www-data netgescon-laravel/storage sudo chown -R www-data:www-data netgescon-laravel/bootstrap/cache sudo chmod -R 775 netgescon-laravel/storage sudo chmod -R 775 netgescon-laravel/bootstrap/cache fi echo 'Permessi impostati correttamente' " print_status "$GREEN" "โœ… Setup post-sync completato" } # === COMMIT AUTOMATICO PRIMA DELLA SINCRONIZZAZIONE === git_auto_commit() { echo -e "${BLUE}๐Ÿ“ฆ Commit automatico modifiche Git...${NC}" | tee -a "$LOG_FILE" cd "$SOURCE_BASE" if [ -n "$(git status --porcelain)" ]; then git add . git commit -m "๐Ÿ”„ Auto-sync $(date '+%Y-%m-%d %H:%M:%S') ๐Ÿ“‹ Sincronizzazione automatica prima di push al master ๐Ÿ”„ Modifiche: $(git status --porcelain | wc -l) file ๐Ÿ“… $(date '+%Y-%m-%d %H:%M:%S')" log "SUCCESS" "Commit automatico creato" echo -e " ${GREEN}โœ… Commit automatico creato${NC}" | tee -a "$LOG_FILE" else log "INFO" "Nessuna modifica Git da committare" echo -e " ${GREEN}โœ… Repository Git giร  aggiornato${NC}" | tee -a "$LOG_FILE" fi } # === SETUP GIT REMOTO DOPO SINCRONIZZAZIONE === setup_git_remote() { echo -e "${BLUE}๐Ÿ”ง Setup Git su server master...${NC}" | tee -a "$LOG_FILE" ssh netgescon@192.168.0.201 << 'EOF' cd /var/www/netgescon # Configura Git se necessario if ! git config user.name >/dev/null 2>&1; then git config user.name "NetGescon Master Server" git config user.email "server@netgescon.local" echo "Git configurato su master server" fi # Reset al commit sincronizzato (soft reset per preservare working directory) if [ -d ".git" ]; then git add . git status echo "Repository Git sincronizzato su master" fi EOF log "SUCCESS" "Git configurato su server master" echo -e " ${GREEN}โœ… Git configurato su master${NC}" | tee -a "$LOG_FILE" } # Funzione di aiuto show_help() { cat << EOF NETGESCON - Sincronizzazione Completa verso MASTER UTILIZZO: $0 [opzione] OPZIONI: --help, -h Mostra questo aiuto --stats Mostra solo statistiche senza sincronizzare --check Controlla solo prerequisiti --dry-run Simula sincronizzazione senza modificare file --force Sincronizza senza conferme interattive DESCRIZIONE: Sincronizza TUTTO il progetto NetGescon mantenendo struttura identica: LOCALE: ~/netgescon/ MASTER: /var/www/netgescon/ Include: - netgescon-laravel/ (applicazione web) - docs/ (documentazione completa) - scripts/ (tool e automazione) - sync-docs-rsync.sh (script sincronizzazione) - Altri file progetto Esclude: - .git (repository Git) - node_modules (dipendenze JS) - vendor (dipendenze PHP) - File temporanei e cache ESEMPI: $0 # Sincronizzazione normale con conferme $0 --stats # Solo statistiche $0 --dry-run # Test senza modifiche $0 --force # Sincronizzazione automatica LOG: I log vengono salvati in: $HOME/netgescon/log/ EOF } # Main - Parsing argomenti e esecuzione main() { case "${1:-}" in --help|-h) show_help exit 0 ;; --stats) check_prerequisites show_source_stats show_sync_structure exit 0 ;; --check) check_prerequisites print_status "$GREEN" "โœ… Sistema pronto per sincronizzazione completa" exit 0 ;; --dry-run) print_status "$YELLOW" "๐Ÿ” MODALITร€ DRY-RUN (nessuna modifica effettiva)" export DRY_RUN_MODE=1 check_prerequisites show_source_stats show_sync_structure print_status "$BLUE" "In modalitร  dry-run - per test effettivo aggiungere --dry-run a rsync" exit 0 ;; --force) print_status "$YELLOW" "โšก MODALITร€ FORCE (senza conferme)" export FORCE_MODE=1 ;; "") # Modalitร  normale ;; *) echo "Opzione non riconosciuta: $1" echo "Usare --help per vedere le opzioni disponibili" exit 1 ;; esac # Esecuzione normale print_status "$BLUE" "๐Ÿ AVVIO SINCRONIZZAZIONE COMPLETA NETGESCON" echo "==================================================================" check_prerequisites show_source_stats show_sync_structure git_auto_commit if perform_complete_sync; then verify_sync post_sync_setup setup_git_remote print_status "$GREEN" "๐ŸŽ‰ SINCRONIZZAZIONE COMPLETA RIUSCITA!" print_status "$BLUE" "๐Ÿ“‹ RIEPILOGO:" echo " ๐ŸŽฏ Progetto NetGescon completamente sincronizzato" echo " ๐Ÿ“ Struttura identica locale โ†” MASTER" echo " ๐Ÿค– AI remoto ha accesso completo a documentazione" echo " โš™๏ธ Script e tool disponibili su MASTER" echo " ๐Ÿ“ Log completo: $LOG_FILE" else print_status "$RED" "โŒ Sincronizzazione fallita" exit 1 fi print_status "$BLUE" "๐Ÿ Sincronizzazione terminata" } # Esegui se chiamato direttamente if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi