Skip to content

Instantly share code, notes, and snippets.

@MathiasReker
Last active October 27, 2024 16:31
Show Gist options
  • Save MathiasReker/f1c8cadfe0d01add5e4613f1d43b3cb2 to your computer and use it in GitHub Desktop.
Save MathiasReker/f1c8cadfe0d01add5e4613f1d43b3cb2 to your computer and use it in GitHub Desktop.
Home Assistant Backup to GitHub

Home Assistant Backup to GitHub

This guide explain how to automates the process of backing up your Home Assistant backup files (/backup directory) to a private GitHub repository. It includes steps for setting up GitHub, configuring Git, and running backups from a NodeRed flow.

This guide is for advanced users. This guide is for Alpine Linux.

Setup Instructions

1. GitHub Repository

  1. Go to GitHub.
  2. Create a private repository named homeassistant-backup.
  3. Set the default branch to master.
  4. Generate a token with no expiration and full permissions for this specific repository.

2. Install Required Add-ons

  • Install the Advanced SSH & Web Terminal add-on in Home Assistant.
  • Configure the addon with a password. You need this password later on in the Node Red flow.

3. Initialize Git Repository

SSH into the server and run the following commands in your terminal:

cd /
sudo git init
sudo echo "/*\n!/backup/\n!.gitattributes" >> .gitignore
sudo git remote add origin https://REPLACE_WITH_USERNAME:[email protected]/REPLACE_WITH_USERNAME/homeassistant-backup.git

Install Git LFS to uplodad files > 100 MB

sudo apk add git-lfs
sudo git lfs install
sudo git lfs track "*.tar"

sudo git add .gitattributes .gitignore
sudo git commit -m "Track .tar files with Git LFS"
sudo git push -u origin master -f

Create the backup script

cd /
sudo nano backup.sh

Add:

#!/usr/bin/env bash
set -e
HA_VERSION_FILE="/homeassistant/.HA_VERSION"
if [[ ! -f $HA_VERSION_FILE ]]; then
    echo "Error: Home Assistant version file not found at $HA_VERSION_FILE"
    exit 1
fi
HA_VERSION=$(cat "$HA_VERSION_FILE")
COMMIT_CURRENT_DATE=$(date +'%d-%m-%Y %H:%M:%S')
COMMIT_MESSAGE=":safety_vest: [$HA_VERSION]: $COMMIT_CURRENT_DATE"
sudo git add /backup
sudo git commit -m "$COMMIT_MESSAGE"
sudo git push -u origin master -f

Create a NodeRed flow that creates a full backup and run /backup.sh afterwards

[{"id":"fd49290bbbf4cafa","type":"exec","z":"a6541044a83813f3","command":"sshpass -pREPLACE_THIS_WITH_PASSWORD ssh -o StrictHostKeyChecking=no [email protected] 'bash /backup.sh'","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":490,"y":300,"wires":[[],[],[]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment