Automating Daily Backups in Home Assistant: A Step-by-Step Guide

Automating Daily Backups in Home Assistant: A Step-by-Step Guide

Hello, fellow Home Assistant enthusiasts! I’ve been exploring ways to streamline my setup, and I’m thrilled to share how I automated daily backups and cleanup using the Supervisor API. This solution has been a game-changer for me, and I hope it can benefit you too!

The Challenge

I wanted to create a system that backs up my configuration every day and deletes backups older than a specified number of days. While there are many solutions out there, none quite met my needs. After some research and experimentation, I decided to script it myself. Here’s how it works!

Prerequisites

Before diving in, ensure you have the following set up:

  • Supervisor API enabled in your configuration.yaml:
    yaml
    api:

  • Shell Command Integration configured to execute scripts:
    yaml
    shell_command:
    manage_backups_api: ‘/config/scripts/manage_backups_api.sh’

  • Directories created in your config folder:

    • backups
    • scripts

The Solution

1. Backup Automation

I set up an automation that triggers daily at 4:00 AM. This automation performs two main tasks:

  • Creates a full backup using the hassio.backup_full service.
  • Executes a shell script to manage and delete old backups.

Here’s the automation configuration:
yaml
alias: Manage Backups Using API
trigger:

  • platform: time
    at: “04:00:00”
    action:
  • service: hassio.backup_full
    metadata: {}
    data:
    compressed: true
  • service: shell_command.manage_backups_api
    data: {}

2. Shell Script: manage_backups_api.sh

This script handles the backup management. It lists all backups, keeps the most recent 7, and deletes the rest. Here’s a breakdown of what it does:

  • Logs every action for easy debugging.
  • Lists all backups using the Supervisor API.
  • Deletes backups beyond the specified number (currently set to 7).

bash
#!/bin/bash

Define logging and backup retention

LOGGING_ENABLED=true
BACKUPS_TO_KEEP=7

Log function

log_message() {
if $LOGGING_ENABLED; then
echo “$(date): $1” >> /config/backups/backup_script_api.log
fi
}

Start logging

log_message “Starting backup management”

API configuration

HASS_URL=“http://supervisor
HASS_TOKEN=“$SUPERVISOR_TOKEN”
HEADERS=(“-H” “Authorization: Bearer $HASS_TOKEN” “-H” “Content-Type: application/json”)

Fetch backups

response=$(curl -s -w “\nHTTP_STATUS:%{http_code}” “${HEADERS[@]}” “$HASS_URL/backups”)
http_body=$(echo “$response” | grep -v “HTTP_STATUS”)
http_status=$(echo “$response” | grep “HTTP_STATUS” | cut -d’:’ -f2)

Save backups to file

if [ -s /config/backups/backups_api.json ]; then
echo “$http_body” > /config/backups/backups_api.json
BACKUPS=$(jq -r --argjson num “$BACKUPS_TO_KEEP” ‘.data.backups | sort_by(.date) | .[:-$num] | ..slug’ /config/backups/backups_api.json)
for SLUG in $BACKUPS; do
delete_response=$(curl -s -w “\nHTTP_STATUS:%{http_code}” -X DELETE “${HEADERS[@]}” “${HASS_URL}/backups/${SLUG}”)
delete_status=$(echo “$delete_response” | grep “HTTP_STATUS” | cut -d’:’ -f2)
if [ “$delete_status” -eq 200 ]; then
log_message “Deleted backup: $SLUG”
else
log_message “Failed to delete backup: $SLUG”
fi
done
else
log_message “Failed to list backups or backups.json is empty”
fi

End logging

log_message “Backup management completed”

Benefits

  • Hands-off Operation: Once set up, this automation runs seamlessly without manual intervention.
  • Space Management: Keeps your backup storage tidy by automatically deleting old backups.
  • Customizable: Easily adjust the number of backups to retain by modifying the BACKUPS_TO_KEEP variable.

Final Thoughts

This solution has been a lifesaver for me, ensuring my Home Assistant setup is always backed up and my storage remains organized. I’d love to hear how you’re automating your backups or if you’ve implemented similar solutions! Let’s keep the community knowledge flowing!

Happy automating! :rocket: