Setting up Github backup for Home Assistant

Home Assistant Configuration

Also see https://diy-smarthome.com/how-to-set-up-git-to-version-control-your-home-assistant-configuration/

To create the .gitignore execute the following
nano .gitignore

.gitignore file contents

# WARNING: Make your GitHub repo Private if you are using this as it is
# Example .gitignore file for your config dir.
# An * ensures that everything will be ignored.
*
# You can whitelist files/folders with !, these will not be ignored.
!*.yaml
!.gitignore
!*.md
!*.sh
!*.js*

# Comment these 2 lines if you don't want to include your SSH keys
!.ssh/
!id_rsa*

# Comment this if you don't want to include your Node-RED flows.
# This is only valid if you install Node-RED as Home Assistant Add-on
!node-red/

# Uncomment this if you don' want to include secrets.yaml 
# secrets.yaml

# Ignore these files/folders
.storage
.cloud
.google.token 
home-assistant.log

Git repository initialisation
git init

Add all files to the repository
git add .

Commit changes with message “first commit”
git commit -m "first commit"

Add git remote
git remote add origin git@github.com:YOUR_GIT_HUB_ACC/YOUR_REPO.git

AUTOMATED HOME ASSISTANT GITHUB BACKUP

Create a .ssh folder inside your HA config folder
mkdir .ssh

Generate a private and public SSH keys
ssh-keygen -t rsa -b 4096 -C "your@mail.com

CHOOSE as path – .ssh/id_rsa

Get the content of this file
cat .ssh/id_rsa.pub

Paste content in GitHub SSH and GPG keys menu

Tell Git where to find the newly created SSH keys
git config core.sshCommand "ssh -i /config/.ssh/id_rsa -F /dev/null"

When you do the first push you need to enter Yes to the Fingerprint Question.

git add .

git push -u origin master

To create the script execute the following
nano ha_gitpush.sh

# Go to /config folder or 
# Change this to your Home Assistant config folder if it is different
cd /config
# Add all files to the repository with respect to .gitignore rules
git add .
# Commit changes with message with current date stamp
git commit -m "config files on `date +'%d-%m-%Y %H:%M:%S'`"
# Push changes towards GitHub
git push -u origin master

Save file and the make it executable
chmod +x ha_gitpush.sh

Automation

Put this in automations.yaml
- id: l1k3
  alias: push HA configuration to GitHub repo
  trigger:
  # Everyday at 23:23:23 time
  - at: ’23:23:23’
    platform: time
  action:
  - data:
      addon: a0d7b954_ssh
      input: /config/ha_gitpush.sh
    service: hassio.addon_stdin
Reset to HEAD / RESTORE

# Going back to the commit before HEAD
git reset --hard HEAD^      

# Going back two commits before HEAD
git reset --hard HEAD~2     

# To undo a hard reset on Git
git reset --hard HEAD@{1}