Skip to content

Instantly share code, notes, and snippets.

@AkdM
Last active November 20, 2024 22:39
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
#
@AkdM
Copy link
Author

AkdM commented Jun 21, 2024

@vasilvestre I recommend you to commit the backups into a private repository, as even if you ignore things, some updates or new integrations may lead to exposing secrets, and you obviously don't want that. I often change my configuration, try new integrations and stuff, and I don't want to worry about that. It will be a peace of mind if you put everything into a private repository.

About the configurations that is outside the /config folder I'm not really sure how to handle that, since I don't have anything interesting located outside that folder. I may have other ideas to make the backup easier to configure and to save (everything, this time), which would involve Github release instead of committing, with the same idea of hass-auto-backup. In the meantime, feel free to improve my researches :)

I've ignored the .db file since it's only historical data, mine is 404MB currently because of the recent devices I've added I guess, thus resulting into a failing commit because I didn't enabled LFS.
Actually here's my current .gitignore file:

/custom_components/
/zigbee2mqtt/log
/zigbee2mqtt/state.json
/home-assistant_v2.*
/home-assistant.log*
.ssh/
.storage

@vasilvestre
Copy link

For some reason /home-assistant_v2.* isn't matching my files. I had to change content to this :

# ignore
/custom_components/
/zigbee2mqtt/log
/zigbee2mqtt/state.json
/home-assistant_v2.db
/home-assistant_v2.db-shm
/home-assistant_v2.db-wal
/home-assistant.log*
/.ssh/
.storage

@vasilvestre
Copy link

I have this issue with the automation only, "Identify file /config/.ssh/id_rsa not accessible : no such file or directory."
Is this due to the permissions ?
image

@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