run_backup_process.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ./borg_create.sh || handle_error "borg_create.sh"
  40. # Log stats to influxdb
  41. echo "Logging backup to influxdb..."
  42. ./log_to_influx.sh || echo "Error occurred in script: log_to_influx.sh"
  43. # Log stats to influxdb
  44. echo "Pruning old backups..."
  45. ./borg_prune.sh || echo "Error occurred in script: borg_prune.sh"
  46. # Log stats to influxdb
  47. echo "Compacting archives..."
  48. ./borg_compact.sh || echo "Error occurred in script: borg_compact.sh"
  49. # Delay before unmounting
  50. echo "Waiting before unmounting HDD..."
  51. sleep 5
  52. # Unmount HDD
  53. echo "Unmounting HDD..."
  54. ./unmount_hdd.sh || handle_error "unmount_hdd.sh"
  55. # Delay before turning off
  56. echo "Waiting before turning off HDD power..."
  57. sleep 10
  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."
  62. trap - EXIT
  63. exit 0