81 lines
4.3 KiB
Bash
81 lines
4.3 KiB
Bash
#!/bin/bash
|
|
|
|
# ==============================================================================
|
|
# Script: hass_backup.sh
|
|
# Description: This script securely backs up Home Assistant to a designated
|
|
# mounted drive using rsync. It includes pre-run checks to ensure
|
|
# the source files and destination are valid, and sends ntfy
|
|
# notifications for each task.
|
|
# Usage: sudo ./hass_backup.sh
|
|
# Notes: This script may require sudo/root privileges to access files.
|
|
# ==============================================================================
|
|
|
|
# ==============================================================================
|
|
# Define Variables for Source and Destination
|
|
# ==============================================================================
|
|
# This makes the script easier to read and modify.
|
|
BACKUP_MOUNT_POINT="/mnt/5TB-Disk1"
|
|
BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup"
|
|
|
|
# Home Assistant backup directories
|
|
HOMEASSISTANT_BACKUP_SOURCE_DIR="/srv/docker/homeassistant/backups"
|
|
HOMEASSISTANT_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/homeassistant"
|
|
|
|
# ntfy Notification Configuration
|
|
# To use this feature, you must have the 'curl' command installed.
|
|
# NTFY_TOPIC is the topic you wish to send notifications to.
|
|
# NTFY_SERVER is the URL of your ntfy server.
|
|
# NTFY_TOKEN is the authorization token for your ntfy server.
|
|
NTFY_TOPIC="Server"
|
|
NTFY_SERVER="https://ntfy.speerfam.net"
|
|
NTFY_TOKEN="tk_3xo9mejjtgemyowzckqw2souo3mux"
|
|
|
|
# ==============================================================================
|
|
# Pre-flight Checks (for the destination drive)
|
|
# ==============================================================================
|
|
|
|
# Check if the mount point directory exists and is a mounted filesystem.
|
|
# This is a critical check to ensure we are writing to the intended air-gapped
|
|
# drive and not to a local directory if the mount failed.
|
|
if ! mountpoint -q "$BACKUP_MOUNT_POINT"; then
|
|
echo "Error: '$BACKUP_MOUNT_POINT' is not a mount point. Please ensure the drive is mounted." >&2
|
|
# Send a failure notification
|
|
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Destination '$BACKUP_MOUNT_POINT' is not mounted." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
|
|
exit 1
|
|
fi
|
|
|
|
# Check if the destination directories exist and create them if they don't.
|
|
# This prevents rsync from failing on the first run.
|
|
mkdir -p "$BACKUP_DESTINATION"
|
|
mkdir -p "$PHOTOS_DESTINATION"
|
|
|
|
# ==============================================================================
|
|
# Home Assistant backup
|
|
# ==============================================================================
|
|
echo "Starting rsync of the most recent Home Assistant backup file to '$HOMEASSISTANT_DESTINATION'..."
|
|
|
|
# Check if the Home Assistant backup source directory exists.
|
|
if [ ! -d "$HOMEASSISTANT_BACKUP_SOURCE_DIR" ]; then
|
|
echo "Error: Home Assistant backup directory '$HOMEASSISTANT_BACKUP_SOURCE_DIR' not found." >&2
|
|
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Home Assistant backup directory not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
|
|
else
|
|
# Find the most recent file in the directory.
|
|
LATEST_BACKUP_FILE=$(ls -t "$HOMEASSISTANT_BACKUP_SOURCE_DIR" | head -n 1)
|
|
|
|
# Check if a file was found.
|
|
if [ -z "$LATEST_BACKUP_FILE" ]; then
|
|
echo "Error: No files found in Home Assistant backup directory '$HOMEASSISTANT_BACKUP_SOURCE_DIR'." >&2
|
|
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: No files found in Home Assistant backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
|
|
else
|
|
FULL_BACKUP_PATH="$HOMEASSISTANT_BACKUP_SOURCE_DIR/$LATEST_BACKUP_FILE"
|
|
rsync -av --progress "$FULL_BACKUP_PATH" "$HOMEASSISTANT_DESTINATION"
|
|
if [ $? -eq 0 ]; then
|
|
echo "Success: Most recent Home Assistant backup '$LATEST_BACKUP_FILE' copied to '$HOMEASSISTANT_DESTINATION'."
|
|
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Home Assistant backup copied to $HOMEASSISTANT_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
|
|
else
|
|
echo "Error: rsync failed to copy Home Assistant backup file '$LATEST_BACKUP_FILE'. Please check permissions." >&2
|
|
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Home Assistant backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
|
|
fi
|
|
fi
|
|
fi
|