subDesTagesMitExtraKaese 3 luni în urmă
comite
7de806afa0
9 a modificat fișierele cu 232 adăugiri și 0 ștergeri
  1. 21 0
      .env.example
  2. 2 0
      .gitignore
  3. 16 0
      check_battery.sh
  4. 30 0
      mount_hdd.sh
  5. 12 0
      power_off_hdd.sh
  6. 12 0
      power_on_hdd.sh
  7. 38 0
      run_backup.sh
  8. 75 0
      run_backup_process.sh
  9. 26 0
      unmount_hdd.sh

+ 21 - 0
.env.example

@@ -0,0 +1,21 @@
+# Home Assistant Details
+HA_URL="http://homeassistant.local:8123"
+HA_TOKEN="your_home_assistant_token_here"
+HDD_SWITCH_ENTITY="switch.external_hdd_power"
+BATTERY_SENSOR_ENTITY="sensor.battery_percentage"
+MIN_BATTERY_LEVEL=80
+
+# Server Details
+SERVER_USER="your_user"
+SERVER_IP="your_server_ip"
+SSH_KEY="~/.ssh/id_rsa"
+SSH_PORT=22
+
+# HDD Mount Details
+HDD_DEVICE="/dev/sdb1"
+MOUNT_POINT="/mnt/external_hdd"
+
+# Borg Backup Details
+REPOSITORY="/mnt/external_hdd/backup_repo"
+SOURCE_DIRECTORIES="/path/to/your/data"
+PASSPHRASE="your_borg_passphrase"

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.env
+keys/

+ 16 - 0
check_battery.sh

@@ -0,0 +1,16 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Query Home Assistant API for battery level
+battery_level=$(curl -s -X GET "$HA_URL/api/states/$BATTERY_SENSOR_ENTITY" -H "Authorization: Bearer $HA_TOKEN" | jq '.state' | tr -d '"')
+
+# Check if battery is sufficiently charged
+if [ "$battery_level" -ge "$MIN_BATTERY_LEVEL" ]; then
+    echo "Battery level is sufficient: $battery_level%"
+    exit 0
+else
+    echo "Battery level is too low: $battery_level%"
+    exit 1
+fi

+ 30 - 0
mount_hdd.sh

@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Execute mount command on the server via SSH and capture output
+OUTPUT=$(ssh -i "$SSH_KEY" -p "$SSH_PORT" "$SERVER_USER@$SERVER_IP" << EOF
+    if mountpoint -q "$MOUNT_POINT"; then
+        echo "HDD is already mounted."
+    else
+        # Try to mount the HDD and handle errors
+        sudo /bin/mount "$HDD_DEVICE" "$MOUNT_POINT"
+        if [ \$? -ne 0 ]; then
+            echo "Failed to mount HDD at $MOUNT_POINT."
+            exit 1
+        else
+            echo "HDD mounted at $MOUNT_POINT."
+        fi
+    fi
+EOF
+)
+
+# Check if the SSH command was successful
+if [ $? -ne 0 ]; then
+    echo "SSH command failed."
+    exit 1
+fi
+
+# Print only the last line of the output
+echo "$OUTPUT" | tail -n 1

+ 12 - 0
power_off_hdd.sh

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Turn off the external HDD
+response=$(curl -s -X POST "$HA_URL/api/services/switch/turn_off" \
+    -H "Authorization: Bearer $HA_TOKEN" \
+    -H "Content-Type: application/json" \
+    -d "{\"entity_id\": \"$HDD_SWITCH_ENTITY\"}")
+
+echo "Powering off external HDD: $response"

+ 12 - 0
power_on_hdd.sh

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Turn on the external HDD
+response=$(curl -s -X POST "$HA_URL/api/services/switch/turn_on" \
+    -H "Authorization: Bearer $HA_TOKEN" \
+    -H "Content-Type: application/json" \
+    -d "{\"entity_id\": \"$HDD_SWITCH_ENTITY\"}")
+
+echo "Powering on external HDD: $response"

+ 38 - 0
run_backup.sh

@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Export Borg passphrase
+export BORG_PASSPHRASE=$PASSPHRASE
+export BORG_RSH="ssh -i $SSH_KEY -p $SSH_PORT"
+
+# Build the list of existing directories
+EXISTING_DIRECTORIES=$(for DIR in $SOURCE_DIRECTORIES; do
+    if [ -d "$DIR" ]; then
+        echo "$DIR"
+    else
+        echo "Directory does not exist: $DIR"
+    fi
+done)
+
+# If we have valid directories, run the backup
+if [ -n "$EXISTING_DIRECTORIES" ]; then
+    echo "Backing up directories: $EXISTING_DIRECTORIES"
+    sudo -E borg create --stats "$SERVER_USER@$SERVER_IP:$REPOSITORY::{hostname}-{now:%Y-%m-%d}" $EXISTING_DIRECTORIES
+    if [ $? -ne 0 ]; then
+        echo "Borg backup failed."
+        exit 1
+    fi
+else
+    echo "No valid directories to back up."
+    exit 1
+fi
+
+# Prune old backups using a custom SSH port
+borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 "$SERVER_USER@$SERVER_IP:$REPOSITORY"
+
+if [ $? -ne 0 ]; then
+    echo "Borg prune failed."
+    exit 1
+fi

+ 75 - 0
run_backup_process.sh

@@ -0,0 +1,75 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Function to handle errors
+handle_error() {
+    echo "Error occurred in script: $1"
+    # 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"
+    echo "Turning off HDD power..."
+    ./power_off_hdd.sh || echo "Failed to power off HDD"
+}
+
+# Function to retry unmounting
+retry_umount() {
+    local max_retries=5
+    local count=0
+    while [ $count -lt $max_retries ]; do
+        if ./unmount_hdd.sh; then
+            echo "Successfully unmounted HDD."
+            return 0
+        else
+            echo "Unmount failed. Retrying ($((count + 1))/$max_retries)..."
+            sleep 15
+            count=$((count + 1))
+        fi
+    done
+    return 1
+}
+
+# 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..."
+./run_backup.sh || handle_error "run_backup.sh"
+
+# Delay before unmounting
+echo "Waiting before unmounting HDD..."
+sleep 5
+
+# Unmount HDD
+echo "Unmounting HDD..."
+retry_umount || handle_error "unmount_hdd.sh"
+
+# Power off HDD
+echo "Turning off HDD power..."
+./power_off_hdd.sh || handle_error "power_off_hdd.sh"
+
+echo "Backup process completed successfully."

+ 26 - 0
unmount_hdd.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Load environment variables from .env file
+source .env
+
+# Execute unmount command on the server via SSH and capture output
+OUTPUT=$(ssh -i "$SSH_KEY" -p "$SSH_PORT" "$SERVER_USER@$SERVER_IP" << EOF
+    # Try to unmount the HDD and handle errors
+    sudo /bin/umount "$MOUNT_POINT" $1
+    if [ \$? -ne 0 ]; then
+        echo "Failed to unmount HDD from $MOUNT_POINT."
+        exit 1
+    else
+        echo "HDD unmounted from $MOUNT_POINT."
+    fi
+EOF
+)
+
+# Check if the SSH command was successful
+if [ $? -ne 0 ]; then
+    echo "SSH command failed."
+    exit 1
+fi
+
+# Print only the last line of the output
+echo "$OUTPUT" | tail -n 1