run_backup_process.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/bash
  2. cd "$(dirname "$0")"
  3. # Load environment variables from .env file
  4. source .env
  5. # Function to handle errors
  6. handle_error() {
  7. echo "Error occurred in script: $1"
  8. echo "Waiting before unmounting HDD..."
  9. sleep 10
  10. # Trigger the cleanup process
  11. cleanup
  12. exit 1
  13. }
  14. # Function to clean up (unmount and power off HDD)
  15. cleanup() {
  16. echo "Cleaning up..."
  17. echo "Unmounting HDD..."
  18. ./unmount_hdd.sh -f || echo "Failed to unmount HDD"
  19. sleep 10
  20. echo "Turning off HDD power..."
  21. ./power_off_hdd.sh || echo "Failed to power off HDD"
  22. }
  23. # Set trap to call cleanup on script exit or error
  24. trap 'cleanup' EXIT
  25. # Check battery level
  26. echo "Checking battery level..."
  27. ./check_battery.sh || handle_error "check_battery.sh"
  28. # Power on HDD
  29. echo "Turning on HDD power..."
  30. ./power_on_hdd.sh || handle_error "power_on_hdd.sh"
  31. # Delay before mounting
  32. echo "Waiting before mounting HDD..."
  33. sleep 30
  34. # Mount HDD
  35. echo "Mounting HDD..."
  36. ./mount_hdd.sh || handle_error "mount_hdd.sh"
  37. # Run backup
  38. echo "Running backup..."
  39. ./run_backup.sh || handle_error "run_backup.sh"
  40. # Log stats to influxdb
  41. echo "Logging backup to influxdb..."
  42. ./log_to_influx.sh || handle_error "log_to_influx.sh"
  43. # Delay before unmounting
  44. echo "Waiting before unmounting HDD..."
  45. sleep 5
  46. # Unmount HDD
  47. echo "Unmounting HDD..."
  48. ./unmount_hdd.sh || handle_error "unmount_hdd.sh"
  49. # Delay before turning off
  50. echo "Waiting before turning off HDD power..."
  51. sleep 10
  52. # Power off HDD
  53. echo "Turning off HDD power..."
  54. ./power_off_hdd.sh || handle_error "power_off_hdd.sh"
  55. echo "Backup process completed successfully."