Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

53 wiersze
2.0 KiB

  1. #!/bin/bash
  2. set -e
  3. # Verifica se è stato passato il file di backup come argomento
  4. if [ -z "$1" ]; then
  5. echo "Errore: Devi specificare il percorso del file di backup .sql.gz"
  6. echo "Uso: $0 /percorso/al/file_backup.sql.gz"
  7. exit 1
  8. fi
  9. BACKUP_FILE="$1"
  10. # Verifica se il file esiste sul file system
  11. if [ ! -f "$BACKUP_FILE" ]; then
  12. echo "Errore: Il file di backup specificato non esiste: $BACKUP_FILE"
  13. exit 1
  14. fi
  15. # Percorso del file delle variabili d'ambiente
  16. ENV_FILE="/data/conf/presence/res_levis_backend/build/env/db.env"
  17. # Estrazione dinamica della password dal file .env
  18. if [ -f "$ENV_FILE" ]; then
  19. DB_PASSWORD=$(grep -E "^POSTGRES_PASSWORD=" "$ENV_FILE" | cut -d'=' -f2)
  20. else
  21. echo "Errore: File d'ambiente non trovato in $ENV_FILE"
  22. exit 1
  23. fi
  24. echo "=========================================="
  25. echo "AVVIO RIPRISTINO DATABASE (DINAMICO)"
  26. echo "ATTENZIONE: Questo sovrascriverà i dati correnti!"
  27. echo "=========================================="
  28. echo "File di origine: $BACKUP_FILE"
  29. echo "------------------------------------------"
  30. # Pulisce le connessioni attive per evitare blocchi sul drop del database
  31. echo "1. Chiusura connessioni attive..."
  32. docker exec -t -e PGPASSWORD="$DB_PASSWORD" db psql -U postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'postgres' AND pid <> pg_backend_pid();" > /dev/null
  33. # Resetta il database postgres e lo ricrea vuoto
  34. echo "2. Reset e ricreazione database..."
  35. docker exec -t -e PGPASSWORD="$DB_PASSWORD" db dropdb -U postgres --if-exists postgres
  36. docker exec -t -e PGPASSWORD="$DB_PASSWORD" db createdb -U postgres postgres
  37. # Decomprime il file e lo inietta direttamente nel psql del container
  38. echo "3. Importazione dati in corso..."
  39. gunzip -c "$BACKUP_FILE" | docker exec -i -e PGPASSWORD="$DB_PASSWORD" db psql -U postgres -d postgres > /dev/null
  40. echo "------------------------------------------"
  41. echo "Ripristino completato con successo!"
  42. echo "=========================================="