Automating Home Assistant Configuration to GitHub
Being a newbie to Home Assistant, I wanted to automate the changes to the configuration files/folder by pushing these backups to a private GitHub repository. This approach helps avoid exposing any secrets or API tokens and ensures that I can restore contents when required due to any failures of the Home Assistant instance.
While many articles cover this topic, I want to capture all the steps comprehensively without neglecting any detail. Here is a step-by-step guide to setting up this automation:
Install Advanced SSH & Web Terminal Addon. Home Assistant Community Add-on: Advanced SSH & Web Terminal
Create the .gitignore file in the terminal by using the following command. Include the needed lines of code in the .gitignore for excluding it from committing.
- Initial git commands and changing master to main branch for keeping the GitHub naming convention for branches.
- Create .ssh directory to store SSH key passphrases. More reading
Copy and add the SSH key generated from file “ssh/id_rsa.pub” to the “SSH and GPG Key” section in the github.com. https://github.com/settings/ssh/new
Enter the following command to target and inform where the newly generated id_rsa file.
- Now enter the following commands. You will obtain these lines of code when the creating your free GitHub repository. Remember to use SSH mode, not HTTPS version of the remote URL.
If incase you set remote URL incorrectly, follow this command to update it.
- Create a Shell script “backup2git.sh” with the following git commands to handle adding and committing files to repository.
- Provide executable access to the file and Check by manually running it. This manual execution is happening at the “Advanced SSH & Web Terminal” docker container not to the actual Home Assistant container.
To validate how it runs on the Home Assistant container utilizes the following command. Further reading on how to handle Shell command through SSH. SSH’ing from a command line sensor or shell command
The SSH terminal will display the following message. Type “yes” to add github.com as a known host. This will allow in executing the script through the automation.
- Include the following lines of code to the configuration.yaml file.
- Create an Automation that runs on a specific time internal by calling the above shell script as a service.
The code of the automation viewed by checking in the ‘Edit in YAML’ option.
By following these steps, you can ensure that your Home Assistant configuration is regularly backed up to a private GitHub repository. This setup will provide peace of mind knowing that you can restore your configuration in the event of any failures.
Related Link:
Feel free to reach out with any questions or comments. Happy automating!