Skip to content

Instantly share code, notes, and snippets.

@AkdM
Last active November 9, 2024 13:30
Show Gist options
  • Save AkdM/70f3f600356b3b834ae0290bd6f741b3 to your computer and use it in GitHub Desktop.
Save AkdM/70f3f600356b3b834ae0290bd6f741b3 to your computer and use it in GitHub Desktop.
Daily backup Home Assistant configuration into a git repository
# You can whitelist files/folders with !, those will not be ignored.
# Everything that starts with a / are for root elements
# ignore
/custom_components/
/zigbee2mqtt/log
/zigbee2mqtt/state.json
/home-assistant_v2.* # Exclude Home Assistant history-related database. Make sure to enable git LFS if you don't exclude that, since those files can go easily over 100MB
/home-assistant.log*
/.ssh/
.storage
# keep
!.gitignore
!.HA_VERSION

Daily backup Home Assistant with git

As always, please CONSTANTLY read and UNDERSTAND what you copy and run on the Internet. Stay safe!

  • mkdir a .ssh dir to the root of the config folder (should be /root/homeassistant)
  • ssh-keygen to that directory. I've used ssh-keygen -t rsa -b 4096 -C "[email protected]". I will not cover the usage of a passphrase here.
  • Copy /root/homeassistant/.ssh/id_rsa.pub content to your SettingsDeploy keys page of your Github repo
  • Because the shell command won't (obviously) have access to all of the HA instance, cd to your ha configuration directory and run that command to target the newly generated id_rsa file:
git config core.sshCommand "ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' -F /dev/null"
  • Copy backup.sh to the HA configuration folder
  • Create a .gitignore file into the HA configuration folder and add the needed exceptions (I've provided mine)
  • Copy the automation content (automation.yaml), create a new automation and paste the content. You can also go to the automations.yaml and write it here if you're more comfortable. I've set the automation to automatically run at 2:00 am everday. Feel free to change it as you like. Also I like to be notified on the app, using the native notify service.
# I've included the notify service to let you know when the backup is successful or not.
# In the long term I felt like having the successful notification wasn't needed so I removed it.
# Feel free to tweak that, and why not share what you did :)
alias: Daily Backup
description: "Backup Home Assistant configuration folder"
trigger:
- platform: time
at: "02:00:00"
condition: []
action:
- service: shell_command.backup
data: {}
response_variable: backup_response
- if:
- condition: template
value_template: "{{ backup_response['returncode'] == 0 }}"
then:
- service: notify.notify
data:
title: ✅ Backup successful
message: "{{ backup_response['stdout'] }}"
else:
- service: notify.notify
data:
title: ❌ Backup failed
message: "{{ backup_response['stderr'] }}"
mode: single
#!/bin/sh
# To run before
# git config core.sshCommand "ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' -F /dev/null"
HA_VERSION=`cat .HA_VERSION`
COMMIT_CURRENT_DATE=$(date +'%d-%m-%Y %H:%M:%S')
COMMIT_MESSAGE="[$HA_VERSION]: $COMMIT_CURRENT_DATE"
echo "$COMMIT_MESSAGE"
git add .
git commit -m "$COMMIT_MESSAGE"
git push
# add the following into your configuration.yaml file
# …
shell_command:
backup: bash /config/backup.sh
# …
@lrosselld
Copy link

I am trying to add the .storage/lovelace file to save my Dashboard config. However, any way that I add the .storage folder to .gitignore it will not add to github.

Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment