Last active
October 27, 2024 16:31
-
-
Save MathiasReker/f1c8cadfe0d01add5e4613f1d43b3cb2 to your computer and use it in GitHub Desktop.
Home Assistant Backup to GitHub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Home Assistant Backup | |
| This project automates the process of backing up your Home Assistant configuration to a private GitHub repository. It includes steps for setting up GitHub, configuring Git, and running backups via a script. | |
| > This guide is for advanced users. This guide is for Alpine Linux. | |
| ## Setup Instructions | |
| ### 1. GitHub Repository | |
| 1. Go to [GitHub](https://github.com). | |
| 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 in to the server and run the following commands in your terminal: | |
| ```bash | |
| 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 | |
| bash``` | |
| 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: | |
| bash``` | |
| cd / | |
| sudo nano backup.sh | |
| ``` | |
| Add: | |
| bash``` | |
| #!/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: | |
| json``` | |
| [{"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