Home Assistant Backup Automation Scripts
This wiki provides detailed documentation for the Home Assistant Backup Automation Scripts project, including configuration, usage instructions, and advanced features. The project contains two versions of the script to automate the backup process of your Home Assistant server. Table of Contents
Overview
The Home Assistant Backup Automation Scripts automate the process of creating, transferring, and managing backups of your Home Assistant server. These scripts enable seamless backup management by:
- Creating backups on your Home Assistant server.
- Transferring backups to your local machine for safekeeping.
- Cleaning up old backups automatically on both the Home Assistant server and the local machine based on retention policies.
Features
- Backup Creation: Automatically create backups on your Home Assistant server.
- Backup Transfer: Copies backups to a local machine or any defined destination.
- Old Backup Cleanup: Automatically removes old backups from both the Home Assistant server and local storage after a configurable retention period.
- Storage Monitoring (V2 Only): The script checks the available storage on the Home Assistant server and removes old backups when storage falls below a defined threshold.
- Optional Notifications: Integrates with ntfy or Discord for notifications about backup status (Version 2 only).
Version 1 - Simple Backup with Cleanup
Features of Version 1:
- Simpler Backup Workflow: This version focuses on creating backups, transferring them to a local machine, and cleaning up old backups without the notification feature.
Version 1 Usage:
- Configure the script for your Home Assistant SSH credentials and backup destination.
- Set retention policies for old backups.
- Run the script to automatically manage backups and storage cleanup.
- Optionally, schedule the script to run periodically (e.g., daily).
Version 2 - Full Backup & Cleanup with Notifications
Features of Version 2:
- Complete Backup Workflow: Version 2 covers the full backup creation process, cleanup of old backups, and optional notifications via ntfy and Discord.
- Backup Cleanup: Automatically removes backups older than the defined retention period on both the Home Assistant server and local machine.
- Storage Monitoring & Cleanup: If the available storage falls below 2GB, the script will remove old backups on the Home Assistant server based on a defined retention period to free up space.
- Notification System: Sends notifications about backup success, failure, and other events via ntfy or Discord.
Version 2 Usage:
- Configure the script to connect to your Home Assistant server.
- Set notification preferences to receive alerts for backup operations.
- Run the script to automate backup creation, transfer, and cleanup.
- Optionally, schedule the script to run at regular intervals (e.g., daily, weekly).
Installation & Configuration
Step 1: Prerequisites
Before you begin, ensure that you have the following prerequisites installed:
-
Bash shell (on both your local machine and the Home Assistant server).
-
SSH access to your Home Assistant server.
-
Home Assistant with ha CLI tool installed.
Step 2: Script Configuration
Open the script in a text editor and configure the following variables:
HA_SSH_KEY: Path to your SSH private key for accessing Home Assistant.
HA_SSH_USER: SSH username (default: root).
HA_SSH_HOST: IP address or hostname of your Home Assistant server.
BACKUP_DEST: Path to your local backup storage.
Retention Policies: Configure how long to keep old backups on both the Home Assistant server and local machine.
Optional: Set up ntfy or Discord notifications (for Version 2).
Running the Script
After configuring the script, make it executable:
chmod +x home_assistant_backup.sh
Run the script using the following command:
./home_assistant_backup.sh
This will:
- Create a backup on the Home Assistant server.
- Transfer the backup file to your local machine.
- Clean up old backups based on the defined retention period.
Scheduling Backups
To automate the backup process, you can schedule the script to run at regular intervals using cron. For example, to run the script daily at midnight:
crontab -e
Add the following line:
0 0 * * * /path/to/home_assistant_backup.sh
Notification Setup
Version 1 (No Notifications):
Version 1 does not include notification functionality, but you can customize it further if needed.
Version 2 (Optional Notifications):
You can configure ntfy or Discord notifications to receive alerts when a backup is created, failed, or completed.
- ntfy Setup: Enable ntfy notifications by setting
NTFY_ENABLED=trueand configuring theNTFY_SERVERandNTFY_TOPICvariables. - Discord Setup: Enable Discord notifications by setting
DISCORD_ENABLED=trueand providing theDISCORD_WEBHOOK_URL.
Retention Policies & Cleanup
The script allows you to define retention policies for both local and remote backups:
- Local Backup Cleanup: Deletes backups older than a configurable number of days (default is 30 days).
- Remote Backup Cleanup: Deletes backups on the Home Assistant server that are older than a specified retention period (default is 30 days).
Storage Monitoring & Cleanup
These cleanup policies help maintain storage space and ensure that outdated backups are removed automatically. Storage Monitoring & Cleanup
In Version 2, we introduced a new feature for storage monitoring. The script checks the available storage on the Home Assistant server and, if it falls below 2GB, it will automatically remove old backups to free up space. You can configure the number of days' worth of backups to remove if storage is running low.
License & Disclaimer
The software is provided "as-is" without any warranties or guarantees of any kind.
By using this script, you acknowledge and agree that:
No Warranty: The script is provided without any warranty, express or implied, including but not limited to warranties of merchantability or fitness for a particular purpose. Data Loss: The author cannot be held responsible for any data loss, damage to your system, or any other kind of loss resulting from the use of this script. Use at Your Own Risk: You are using this script at your own risk. The author shall not be liable for any damages arising from the use or inability to use the script. Modifications and Redistribution: If you modify or redistribute the script, you do so at your own risk. You are solely responsible for ensuring that any modifications or redistributed versions do not cause harm or violate any laws.