Skip to content

Instantly share code, notes, and snippets.

@AkdM
Last active April 7, 2024 15:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.
# ignore
custom_components/
zigbee2mqtt/log
zigbee2mqtt/state.json
home-assistant.log
home-assistant.log.1
home-assistant.log.fault
.ssh/ # Don't be stupid and ignore that folder
.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 "ha@home.local". 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.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment