Home-Assistant-Backup-Script/V2/home_assistant_backup.sh

134 lines
5.2 KiB
Bash

#!/bin/bash
set -x
### Global Settings ###
# 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
# ntfy settings
NTFY_ENABLED=false # Set to 'true' to enable ntfy notifications
NTFY_SERVER="NTFY_SERVER_FQDN_ADDRESS" # Replace with your ntfy server address
NTFY_TOPIC="NTFY_TOPIC" # Replace with your ntfy topic
# Discord webhook settings
DISCORD_ENABLED=false # Set to 'true' to enable Discord notifications
DISCORD_WEBHOOK_URL="DISCORD_WEBHOOK_URL" # Replace with your Discord webhook URL
# Set clean up old backups (adjust the number of days to keep)
BACKUP_FILE_CLEANUP="90" # Set the number of days that backup files will be kept for (Default 90 days)
# Set the location of the Home Assistant backups (Set to default location /backup)
# If you have a custom install, using the likes of Supervised
# this location should normally be /usr/share/hassio/backup
BACKUP_FILE_LOCATION="/backup"
### Script ###
### Function for sending notifications ###
send_ntfy_notification() {
local message=$1
if [ "$NTFY_ENABLED" = true ]; then
curl -X POST -d "$message" $NTFY_URL
fi
}
send_discord_notification() {
local message=$1
if [ "$DISCORD_ENABLED" = true ]; then
curl -X POST -H "Content-Type: application/json" -d '{"content": "'"$message"'"}' $DISCORD_WEBHOOK_URL
fi
}
# ntfy URL setup
NTFY_URL="$NTFY_SERVER/$NTFY_TOPIC"
# Timestamp variable for consistent naming
TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S')
# Start the timer
START_TIME=$(date +%s)
# Send notification that backup has started
send_ntfy_notification "Backup process started at $TIMESTAMP"
send_discord_notification "Backup process started at $TIMESTAMP"
# 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
send_ntfy_notification "Error: Failed to create backup on Home Assistant server."
send_discord_notification "Error: Failed to create backup on Home Assistant server."
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
send_ntfy_notification "Error: Failed to extract backup slug."
send_discord_notification "Error: Failed to extract backup slug."
echo "Error: Failed to extract backup slug."
exit 1
fi
# Send notification that backup was created
send_ntfy_notification "Backup created successfully with slug: $BACKUP_SLUG"
send_discord_notification "Backup created successfully with slug: $BACKUP_SLUG"
# 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:$BACKUP_FILE_LOCATION/${BACKUP_SLUG}.tar" "$BACKUP_FILE"
if [ $? -ne 0 ]; then
# Send notification if backup copy fails
send_ntfy_notification "Error: Failed to copy backup to local machine."
send_discord_notification "Error: Failed to copy backup to local machine."
echo "Error: Failed to copy backup to local machine."
exit 1
fi
# Get the size of the backup file
BACKUP_FILE_SIZE=$(stat -c %s "$BACKUP_FILE")
if [ $? -ne 0 ]; then
send_ntfy_notification "Error: Failed to get backup file size."
send_discord_notification "Error: Failed to get backup file size."
echo "Error: Failed to get backup file size."
exit 1
fi
# 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
send_ntfy_notification "Error: Failed to clean up old backups."
send_discord_notification "Error: Failed to clean up old backups."
echo "Error: Failed to clean up old backups."
exit 1
fi
# Send notification that the cleanup was successful
send_ntfy_notification "Old backups cleaned up successfully."
send_discord_notification "Old backups cleaned up successfully."
# Success message
echo "Backup completed successfully: $BACKUP_FILE"
send_ntfy_notification "Backup completed successfully: $BACKUP_FILE"
send_discord_notification "Backup completed successfully: $BACKUP_FILE"
# Get the total time taken by the script
END_TIME=$(date +%s)
RUNTIME=$((END_TIME - START_TIME))
# Send notification with backup file size and script runtime
send_ntfy_notification "Backup file size: $BACKUP_FILE_SIZE bytes. Script runtime: $RUNTIME seconds."
send_discord_notification "Backup file size: $BACKUP_FILE_SIZE bytes. Script runtime: $RUNTIME seconds."