Advertisement
TringaliLuca

clone-system.sh

Oct 7th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. dryrun="false"
  4. if [ ! -z $1 ]; then
  5. if [ $1 == "dryrun" ]; then
  6. dryrun="true"
  7. fi
  8. fi
  9.  
  10. if [ "$(whoami)" == "root" ] ; then
  11.     echo "Sei root, bene."
  12. else
  13.     echo "Per favore, esegui lo script con sudo"
  14.     exit 1
  15. fi
  16.  
  17. apt-get install dialog
  18.  
  19. rootorig=""
  20. ischroot
  21. if [ $? -eq 0 ]; then
  22.     echo "Sei in un chroot, considero / come percorso di origine."
  23. else
  24.     rootorig=$(/usr/bin/dialog --stdout  --title " Seleziona cartella di origine" --clear --dselect "/" 20 30 10)
  25.     if [ ! $? -eq 0 ]; then
  26.         echo "Chiudo il programma"
  27.         exit 0
  28.     fi
  29. fi
  30.  
  31.  
  32.  
  33. serverslist=$(/usr/bin/dialog --stdout  --title " Scegli cosa copiare" --clear --checklist "Che tipo di server vuoi realizzare? \n(spostati con freccia su e giù, scegli con Spazio, conferma con Invio)" 20 30 10 \
  34. "Utenti" "" off \
  35. "Cartella_Home" "" off \
  36. "FStab" "" off \
  37. "CUPS" "" off \
  38. "SANED" "" off \
  39. "Apache" "" off \
  40. "MySQL" "" off \
  41. "SAMBA" "" off \
  42. "Rete" "" off \
  43. "Lista_pacchetti" "" off \
  44. )
  45.  
  46. #cron, fail2ban, systemd
  47.  
  48. if [ ! $? -eq 0 ]; then
  49.     echo "Chiudo il programma"
  50.     exit 0
  51. fi
  52.  
  53.  
  54. rootdest=$(/usr/bin/dialog --stdout  --title " Seleziona cartella di destinazione" --clear --dselect "/" 20 30 10)
  55.  
  56. if [ ! $? -eq 0 ]; then
  57.     echo "Chiudo il programma"
  58.     exit 0
  59. fi
  60.  
  61.  
  62. /usr/bin/dialog --stdout  --title " Conferma" --clear --yesno "Vuoi controllare la dimensione delle cartelle?" 6 30
  63. check=$?
  64.  
  65.  
  66. for opzioneserver in $serverslist
  67. do
  68.    echo "Hai scelto: $opzioneserver"
  69.    
  70.    excl="--exclude="
  71.    declare -a source=()
  72.    
  73.    if [ $opzioneserver == "Utenti" ] ; then
  74.    source=( "${source[@]}" "$rootorig/etc/passwd" )
  75.    source=( "${source[@]}" "$rootorig/etc/shadow" )
  76.    source=( "${source[@]}" "$rootorig/etc/group" )
  77.    source=( "${source[@]}" "$rootorig/etc/gshadow" )
  78.    source=( "${source[@]}" "$rootorig/etc/sudoers" )
  79.    source=( "${source[@]}" "$rootorig/etc/sudoers.d" )
  80.    excl=""
  81.    fi
  82.    
  83.    if [ $opzioneserver == "Cartella_Home" ] ; then
  84.    source=( "${source[@]}" "$rootorig/home/" )
  85.    excl=""
  86.    fi
  87.    
  88.    if [ $opzioneserver == "FStab" ] ; then
  89.    source=( "${source[@]}" "$rootorig/etc/fstab" )
  90.    excl=""
  91.    fi
  92.    
  93.    if [ $opzioneserver == "CUPS" ] ; then
  94.    source=( "${source[@]}" "$rootorig/etc/cups" )
  95.    excl=""
  96.    fi
  97.    
  98.    if [ $opzioneserver == "SANED" ] ; then
  99.    source=( "${source[@]}" "$rootorig/etc/sane.d" )
  100.    excl=""
  101.    fi
  102.    
  103.    if [ $opzioneserver == "Apache" ] ; then
  104.    source=( "${source[@]}" "$rootorig/etc/apache2" )
  105.    source=( "${source[@]}" "$rootorig/var/www" )
  106.    excl=""
  107.    fi
  108.    
  109.    if [ $opzioneserver == "MySQL" ] ; then
  110.    source=( "${source[@]}" "$rootorig/var/lib/mysql" )
  111.    source=( "${source[@]}" "$rootorig/etc/mysql" )
  112.    excl=""
  113.    fi
  114.    
  115.    if [ $opzioneserver == "SAMBA" ] ; then
  116.    source=( "${source[@]}" "$rootorig/etc/samba/" )
  117.    excl=""
  118.    fi
  119.    
  120.    if [ $opzioneserver == "Rete" ] ; then
  121.    source=( "${source[@]}" "$rootorig/etc/hosts" )
  122.    source=( "${source[@]}" "$rootorig/etc/network" )
  123.    source=( "${source[@]}" "$rootorig/etc/resolv.conf" )
  124.    excl=""
  125.    fi
  126.    
  127.    if [ $opzioneserver == "Lista_pacchetti" ] ; then
  128.    source=( "${source[@]}" "$rootorig/etc/apt" )
  129.    ischroot
  130.    if [ $? -eq 0 ]; then
  131.       dpkg --get-selections > "$rootdest/packages.txt"
  132.    else
  133.       echo "Non sei in un chroot, non posso estrarre la lista dei pacchetti."
  134.    fi
  135.    excl=""
  136.    fi
  137.    
  138.    for s in "${source[@]}"
  139.    do
  140.    dest=$rootdest$s
  141.    if [[ $check -eq 0 && $s != "" && $rootdest != "" ]] ; then
  142.       if [[ $(du -k $s | cut -f1) -gt $(df -Pk $rootdest | tail -1 | awk '{print $4}') ]] ; then   #Confronto in KB
  143.          echo "Spazio non sufficiente"
  144.          exit 1
  145.       fi
  146.    fi
  147.    if [ $dryrun == "true" ]; then
  148.       echo "rsync -avr --progress --delete $s $dest"
  149.    else
  150.       if [[ -d $s ]]; then
  151.          mkdir -p $dest
  152.       else
  153.          mkdir -p $(dirname "$dest")
  154.       fi
  155.       rsync -avr --progress --delete $s $dest
  156.    fi
  157.    done
  158.    
  159. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement