From 8af78dc249739fa61d91282f8f25417e8611ea06 Mon Sep 17 00:00:00 2001 From: Phil Date: Wed, 27 Nov 2024 20:36:02 +0000 Subject: [PATCH] Added V2 of the script --- ha-backup.sh => V1/ha-backup.sh | 0 V2/ha-backup.sh | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) rename ha-backup.sh => V1/ha-backup.sh (100%) create mode 100644 V2/ha-backup.sh diff --git a/ha-backup.sh b/V1/ha-backup.sh similarity index 100% rename from ha-backup.sh rename to V1/ha-backup.sh diff --git a/V2/ha-backup.sh b/V2/ha-backup.sh new file mode 100644 index 0000000..6a3ff63 --- /dev/null +++ b/V2/ha-backup.sh @@ -0,0 +1,77 @@ +#!/bin/bash +set -x + +# Set your Home Assistant SSH credentials +HA_SSH_KEY="SSH_PRIVATE_KEY" # Replace with your SSH Private key for HomeAssistant +HA_SSH_USER="SSH_USER" # Replace with your HomeAssistant server username (Default is root) +HA_SSH_HOST="SSH_HOST_IP_OR_HOSTNAME" # Replace with your HomeAssistant server IP address +HA_SSH_PORT="SSH_PORT" # Replace with your HomeAssistant server port + +# Set the backup destination path +BACKUP_DEST="BACKUP_FILE_DESTINATION" # Replace with the location for the backup files + +# Set your ntfy topic and URL +NTFY_TOPIC="NTFY_TOPIC" # Replace with your ntfy topic +NTFY_SERVER="NTFY_SERVER_FQDN_ADDRESS" # Replace with your ntfy server address + +NTFY_URL="$NTFY_SERVER/$NTFY_TOPIC" + +# Timestamp variable for consistent naming +TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S') + +# Send notification that backup has started +curl -X POST -d "Backup process started at $TIMESTAMP" $NTFY_URL + +# Create a backup on the Home Assistant server and extract the slug +BACKUP_OUTPUT=$(ssh -i "$HA_SSH_KEY" -p "$HA_SSH_PORT" "$HA_SSH_USER@$HA_SSH_HOST" "ha backup new --name=auto_backup_$TIMESTAMP") +if [ $? -ne 0 ]; then + # Send notification if backup creation fails + curl -X POST -d "Error: Failed to create backup on Home Assistant server." $NTFY_URL + echo "Error: Failed to create backup on Home Assistant server." + exit 1 +fi + +# Extract the backup slug and strip any carriage return characters +BACKUP_SLUG=$(echo "$BACKUP_OUTPUT" | awk '/slug:/ {print $2}' | tr -d '\r') +if [ -z "$BACKUP_SLUG" ]; then + # Send notification if backup slug extraction fails + curl -X POST -d "Error: Failed to extract backup slug." $NTFY_URL + echo "Error: Failed to extract backup slug." + exit 1 +fi + +# Send notification that backup was created +curl -X POST -d "Backup created successfully with slug: $BACKUP_SLUG" $NTFY_URL + +# Create a filename for the backup +BACKUP_FILE="$BACKUP_DEST/${TIMESTAMP}_${BACKUP_SLUG}.tar" + +# Copy the backup file from Home Assistant to your local machine +scp -i "$HA_SSH_KEY" -P "$HA_SSH_PORT" "$HA_SSH_USER@$HA_SSH_HOST:/usr/share/hassio/backup/${BACKUP_SLUG}.tar" "$BACKUP_FILE" +if [ $? -ne 0 ]; then + # Send notification if backup copy fails + curl -X POST -d "Error: Failed to copy backup to local machine." $NTFY_URL + echo "Error: Failed to copy backup to local machine." + exit 1 +fi + +# Send notification that backup file was copied successfully +curl -X POST -d "Backup file successfully copied to: $BACKUP_FILE" $NTFY_URL + +# Clean up old backups if needed (adjust the number of days to keep) +find "$BACKUP_DEST" -name '*.tar' -mtime +90 -exec rm {} \; +if [ $? -ne 0 ]; then + # Send notification if cleanup fails + curl -X POST -d "Error: Failed to clean up old backups." $NTFY_URL + echo "Error: Failed to clean up old backups." + exit 1 +fi + +# Send notification that the cleanup was successful +curl -X POST -d "Old backups cleaned up successfully." $NTFY_URL + +# Success message +echo "Backup completed successfully: $BACKUP_FILE" +# Send notification that the backup was completed +curl -X POST -d "Backup completed successfully: $BACKUP_FILE" $NTFY_URL +