mount.sh 973 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. while read -p "Folder need mount: " dir
  3. do if [[ -d $dir ]]; then
  4. break
  5. else
  6. echo "Folder $dir not found, try again"
  7. fi
  8. done
  9. disk=`parted -l 2>&1 > /dev/null | awk -F ':' {'print $2'} | grep -Ev '[0-9]$|Warning|Read-only'`
  10. if [[ -n "$disk" ]]; then
  11. echo "External disk ready to format and mount"
  12. else
  13. echo "No external disk found"
  14. sleep 2 && exit
  15. fi
  16. #Start mount data to external disk
  17. mkdir -p /backup
  18. mkfs.ext4 $disk > /dev/null 2>&1
  19. mount $disk /backup
  20. echo "======================================================================"
  21. echo "Start sync data to external disk"
  22. rsync -a --info=progress2 $dir /backup
  23. umount $disk
  24. rm -rf $dir/* /backup
  25. mount $disk $dir
  26. echo "$disk $dir ext4 defaults 0 1" >> /etc/fstab
  27. read -p "The process mount successful. Do you want to restart the server? [Y/n]: " boot
  28. if [[ "$boot" = "Y" ]] || [[ "$boot" = "y" ]] || [[ -z "$boot" ]]; then
  29. reboot
  30. fi
  31. #Destroy disk
  32. #wipefs -a /dev/vdb