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.
- Go to GitHub.
- Create a private repository named
homeassistant-backup
. - Set the default branch to
master
. - Generate a token with no expiration and full permissions for this specific repository.
- 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.
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
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
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
[{"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":[[],[],[]]}]