Home-Assistant-Backup-Script/V2
2025-02-01 15:34:17 +00:00
..
home_assistant_backup.sh Forgot to upload the script 2025-02-01 15:34:17 +00:00
README.md Added SCP and SSH requirements into V2 README.md 2025-01-05 18:21:48 +00:00

Home Assistant Backup Script

This script automates the process of creating, copying, and cleaning up backups for a Home Assistant server. It supports the ability to:

  • Create a backup on the Home Assistant server.
  • Copy the backup file to a local machine.
  • Clean up old backups on both the local machine and Home Assistant server.
  • Send notifications to ntfy and Discord.

Requirements

Configuration

Before using the script, you need to set up the following environment variables in the script:

Global Settings

  • HA_SSH_KEY: Path to your SSH private key for Home Assistant.
  • HA_SSH_USER: Username for SSH login (usually root).
  • HA_SSH_HOST: IP address or hostname of your Home Assistant server.
  • HA_SSH_PORT: SSH port (default is 22).
  • BACKUP_DEST: Destination directory for storing local backups.
  • BACKUP_FILE_LOCATION: Location of the backups on the Home Assistant server (default is /backup).
  • LOCAL_BACKUP_CLEANUP: Number of days to keep local backups (default is 90 days).
  • REMOTE_BACKUP_CLEANUP: Number of days to keep backups on the Home Assistant server (default is 30 days).
  • NTFY_ENABLED: Set to true to enable ntfy notifications.
  • NTFY_SERVER: The FQDN of your ntfy server.
  • NTFY_TOPIC: The topic for your ntfy notifications.
  • DISCORD_ENABLED: Set to true to enable Discord notifications.
  • DISCORD_WEBHOOK_URL: Your Discord webhook URL.
  • STORAGE_THRESHOLD_KB: The disk space threshold in KB for triggering cleanup (default is 2GB).

Notifications

  • ntfy: Notifications are sent via HTTP POST to your ntfy server.
  • Discord: Notifications are sent via a Discord webhook.

Set NTFY_ENABLED and DISCORD_ENABLED to true to enable notifications for respective services. Replace the relevant URLs and topics.

Script Flow

  1. Backup Creation: The script first creates a backup on the Home Assistant server.
  2. Disk Space Check: It checks available disk space on the Home Assistant server. If it is less than the specified threshold, it will clean up old backups on the Home Assistant server.
  3. Backup Transfer: The backup file is then copied from the Home Assistant server to the local backup destination.
  4. Local Cleanup: Old backups are removed from the local machine if they exceed the specified retention period.
  5. Notification: Notifications are sent at each major step (e.g., start of backup, successful backup, cleanup actions) via ntfy and Discord if enabled.
  6. Backup File Size and Script Runtime: The script reports the backup file size and runtime after completing the backup process.

Usage

  1. Clone or Download the Script:

    • Clone the repository or download the script file.
  2. Make the Script Executable:

    chmod +x home_assistant_backup.sh
    
    
    

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 of any kind, either express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. The entire risk arising out of the use or performance of the script remains with you.

  • 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. You are solely responsible for backing up your data and ensuring the integrity of your systems before using the script.

  • Use at Your Own Risk: You are using this script at your own risk. The author shall not be liable for any damages, including but not limited to direct, indirect, incidental, special, consequential, or punitive damages, arising out of the use of or inability to use the script.

  • No Support: The script is provided "as-is" with no formal support or maintenance. The author does not provide any guarantees of continued functionality, updates, or support.

  • 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.

By using or modifying this script, you agree to indemnify and hold harmless the author from any and all claims, damages, liabilities, and expenses arising from your use or modification of the script.


Use of this script is subject to the terms above. If you do not agree with this disclaimer, you should not use the script.