123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #!/bin/bash
- cd "$(dirname "$0")"
- # Load environment variables from .env file
- source .env
- # Function to handle errors
- handle_error() {
- echo "Error occurred in script: $1"
- echo "Waiting before unmounting HDD..."
- sleep 10
- # Trigger the cleanup process
- cleanup
- exit 1
- }
- # Function to clean up (unmount and power off HDD)
- cleanup() {
- echo "Cleaning up..."
- echo "Unmounting HDD..."
- ./unmount_hdd.sh -f || echo "Failed to unmount HDD"
- sleep 10
- echo "Turning off HDD power..."
- ./power_off_hdd.sh || echo "Failed to power off HDD"
- }
- # Set trap to call cleanup on script exit or error
- trap 'cleanup' EXIT
- # Check battery level
- echo "Checking battery level..."
- ./check_battery.sh || handle_error "check_battery.sh"
- # Power on HDD
- echo "Turning on HDD power..."
- ./power_on_hdd.sh || handle_error "power_on_hdd.sh"
- # Delay before mounting
- echo "Waiting before mounting HDD..."
- sleep 30
- # Mount HDD
- echo "Mounting HDD..."
- ./mount_hdd.sh || handle_error "mount_hdd.sh"
- # Run backup
- echo "Running backup..."
- ./borg_create.sh || handle_error "borg_create.sh"
- # Log stats to influxdb
- echo "Logging backup to influxdb..."
- ./log_to_influx.sh || echo "Error occurred in script: log_to_influx.sh"
- # Log stats to influxdb
- echo "Pruning old backups..."
- ./borg_prune.sh || echo "Error occurred in script: borg_prune.sh"
- # Log stats to influxdb
- echo "Compacting archives..."
- ./borg_compact.sh || echo "Error occurred in script: borg_compact.sh"
- # Delay before unmounting
- echo "Waiting before unmounting HDD..."
- sleep 5
- # Unmount HDD
- echo "Unmounting HDD..."
- ./unmount_hdd.sh || handle_error "unmount_hdd.sh"
- # Delay before turning off
- echo "Waiting before turning off HDD power..."
- sleep 10
- # Power off HDD
- echo "Turning off HDD power..."
- ./power_off_hdd.sh || handle_error "power_off_hdd.sh"
- echo "Backup process completed successfully."
- trap - EXIT
- exit 0
|