run_backup_process.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. # Load environment variables from .env file
  3. source .env
  4. # Function to handle errors
  5. handle_error() {
  6. echo "Error occurred in script: $1"
  7. # Trigger the cleanup process
  8. cleanup
  9. exit 1
  10. }
  11. # Function to clean up (unmount and power off HDD)
  12. cleanup() {
  13. echo "Cleaning up..."
  14. echo "Unmounting HDD..."
  15. ./unmount_hdd.sh -f || echo "Failed to unmount HDD"
  16. echo "Turning off HDD power..."
  17. ./power_off_hdd.sh || echo "Failed to power off HDD"
  18. }
  19. # Function to retry unmounting
  20. retry_umount() {
  21. local max_retries=5
  22. local count=0
  23. while [ $count -lt $max_retries ]; do
  24. if ./unmount_hdd.sh; then
  25. echo "Successfully unmounted HDD."
  26. return 0
  27. else
  28. echo "Unmount failed. Retrying ($((count + 1))/$max_retries)..."
  29. sleep 15
  30. count=$((count + 1))
  31. fi
  32. done
  33. return 1
  34. }
  35. # Set trap to call cleanup on script exit or error
  36. trap 'cleanup' EXIT
  37. # Check battery level
  38. echo "Checking battery level..."
  39. ./check_battery.sh || handle_error "check_battery.sh"
  40. # Power on HDD
  41. echo "Turning on HDD power..."
  42. ./power_on_hdd.sh || handle_error "power_on_hdd.sh"
  43. # Delay before mounting
  44. echo "Waiting before mounting HDD..."
  45. sleep 30
  46. # Mount HDD
  47. echo "Mounting HDD..."
  48. ./mount_hdd.sh || handle_error "mount_hdd.sh"
  49. # Run backup
  50. echo "Running backup..."
  51. ./run_backup.sh || handle_error "run_backup.sh"
  52. # Delay before unmounting
  53. echo "Waiting before unmounting HDD..."
  54. sleep 5
  55. # Unmount HDD
  56. echo "Unmounting HDD..."
  57. retry_umount || handle_error "unmount_hdd.sh"
  58. # Power off HDD
  59. echo "Turning off HDD power..."
  60. ./power_off_hdd.sh || handle_error "power_off_hdd.sh"
  61. echo "Backup process completed successfully."