Now is the best time (the only better time was yesterday) to setup a reliable Home Assistant backup to GitHub and start experimenting and enhancing without worries.

Automatic Home Assistant Backup to GitHub

And it doesn’t matter if you have brand new Home Assistant installation or there are years of experience and fine tuning in your config files.

What will you get from this Article?

If you’re reading this then you probably already have working Home Assistant.

If not then you better check these two articles:

  • HOW TO Install HOME ASSISTANT – Container & Supervised methods – LINK
  • Home Assistant on Windows using VirtualBox GUIDE LINK

Install your Home Assistant and then come back here to learn how to Home Assistant Backup to GitHub. 

As you may know you can use Home Assistant snapshots for backup, but there is one major drawback doing this – The snapshots are stored locally.

I will show you another Home Assistant Backup method where your configuration files will be stored securely on GitHub! A set and forget method.

Home Assistant runs on a series of YAML files. So if you manage to automatically backup and version control these files on a remote host – that means you are one step closer to the best DevOps practices.

Where you treat your server not as pet, but as cattle that can be easily replaced if needed.

Now let’s start this.

Installing Terminal Add-on in Home Assistant

Go to Home Assistant > Supervisor > Add-on store and search for “SSH & Web Terminal“.

Activating SSH & Web Terminal in Home Assistant

Disable the “Protection mode” (must) and enable “Show in sidebar” (optional).

Then go to the Configuration tab of the SSH & Web Terminal add-on and enter a password of your choice there. It is recommended to use SSH key instead of the password here. If you wish to do that you can find more info here.

Set the password or SSH key and hit the SAVE button.

Adding a password for the SSH & Web Terminal Home Assistant add-on
Adding a password for the SSH & Web Terminal Home Assistant add-on

Finally to start it go back to the Info tab of the SSH & Terminal add-on and click on the START button.

If there are no errors in the Log tab you can try to open the add-on either form the sidebar or from the “OPEN WEB UI” button.

We need this add-on because we are going to use some linux commands in a Terminal to enable Automatic Home Assistant Backup to GitHub. Don’t be worry they are not complicated and I will try to explain everything as best as I can.

What is Git and GitHub?

Git de-facto is the standard for version control system.

GitHub is a free online repository for backup, contributing and sharing source code. 

And guess what? We are going to use GitHub to backup our Home Assistant configuration.

Creating a GitHub repository

You will need a GitHub account. Go to the GitHub website and create one it is easy & free (best combination). If you already have an account – just log in it.

Next we have to create a repository which again is easy & free.

Creating a new GitHub repository.

Click on the plus button in the upper right corner of the GitHub web page and select New repository.

Type a repository name, select Private and click on Create repository button.

Automatic Home Assistant Backup to GitHub 1

Public vs Private repo?

You can have public or private repos in your GitHub account. I will use private repository as I want to store my Home Assistant passwords there.

If you wish you can make you Home Assistant config files public by selecting Public repository. Just don’t forget to take care about your passwords by not adding your secrets.yaml file to the repo for example. 

If you decide to go All-in with private repo and put everything inside, just like me – it is highly recommended to enable 2 Factor Authentication (2FA) for your GitHub account for better protection. You can find how exactly – here.

Creating .gitignore file

Now when we have our repository let’s make a good use of our newly installed SSH & Terminal add-on.

Open the Terminal add-on and go to the Home Assistant configuration folder. For the Home Assistant OS (that is Home Assistant running from an image on virtual machine or on Raspberry Pi) the correct folder is /config”.

For Home Assistant Supervised (that is Home Assistant running from Docker containers with Supervisor menu enabled) – I forgot where it is exactly at the moment – sorry (I will update this at later stage with the exact path).

The folder you are looking for is where you configuration.yaml file is. Find it and enter inside it by using for example

cd /config

Then when you double check that you are in the correct configuration folder (remember all yaml files are there, including configuration.yaml) execute the following:

nano .gitignore 

This will open the nano text editor and it will create a .gitignore file where you can tell which files/folders to be added in the Home Assistant backup to GitHub.

Now you can copy and paste the following lines inside the text editor:

# 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

Use Ctrl +X and press Y and hit Enter to save the file.

Only use the exact .gitignore content if you are using Private repository otherwise you can expose your Home Assistant passwords to the public.

In other words if you use the same .gitignore file, you will include the following files that are in the /config, /config/.ssh/ and /config/node-red folders:

  • All – yaml, md, sh, json, js files
  • id_rsa, id_rsa.pub, .gitignore files.

And you will ignore/exclude these files and folders from very soon automatic Home Assistant backup to GitHub:

  • .storage, .cloud, .google.token, home-assistant.log

Now let’s continue forward.

Git repository initialisation

The following steps are easy and straightforward as it will soon become the Automatic Home Assistant Backup to GitHub.

Just copy and paste the commands in your terminal:

To initialise the repository:

git init

To add all files to the repository with respect to the .gitignore rules

git add .

To commit the changes with the commit message “first commit”

git commit -m "first commit"  

Now you have to add your GitHub repository as a remote repository where the changes can be pushed and to stay only locally.

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

Creating SSH keys for GitHub

Before we push our commit to GitHub or in other words before we create our first Manual Home Assistant Backup to GitHub we have to authenticate to GitHub somehow.

And the best way is to use SSH keys. This is how you can do it:

Create a .ssh folder inside your Home Assistant configuration folder.

mkdir .ssh 

Generate a private and public SSH keys

ssh-keygen -t rsa -b 4096 -C "your@mail.com"

The script above will ask you where to store the keys. It is important here not add passphrase to allow Automatic Home Assistant Backup to GitHub at later stage and to change the default path with this one – .ssh/id_rsa

To allow Automatic Home Assistant Backup to GitHub change the default path and enter no passphrase
To allow Automatic Home Assistant Backup to GitHub change the default path and enter no passphrase.

When everything finish you should have your private (id_rsa) and public (id_rsa.pub) keys inside .ssh/ folder, which folder from the other hand should be in Home Assistant configuration folder.

Now we have to copy and paste the content of id_rsa.pub inside the GitHub. So copy the content of the file and go to Github.com > Your Profile > Settings > SSH and GPG keys.

Adding new SSH key in GitHub.com
Adding new SSH key in GitHub.com

Click on New SSH key, type a Title of your choice and paste the content of id_rsa.pub file inside it. Then click on Add SSH key button.

Tell Git where the SSH Keys are

Because we have stored the SSH key in a non-standard location now we need to tell Git where to find it with the following command.

git config core.sshCommand "ssh -i /config/.ssh/id_rsa -F /dev/null"

Pushing all changes to GitHub manually

Now you can push everything you did locally to GitHub with the following command.

git push -u origin master
First manual Home Assistant Backup to GitHub
First manual Home Assistant Backup to GitHub

If everything is working without errors and you have your config files in GitHub you can continue to the automation part.

Automatic Home Assistant Backup to GitHub

We can now manually do a Home Assistant GitHub backup, but let’s make this process automatic.

The first step is to smash the like button for the YouTube algorithm.

Just kidding let’s continue.

We will need a shell script for the automation part. Later we will execute this script from Home Assistant automation, but for now just open Terminal and execute the following

nano ha_gitpush.sh 

and paste the following lines inside the nano text edior.

# 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

Use Ctrl +X and press Y and hit Enter to save the file.

Now we have to make this file executable with this command:

chmod +x ha_gitpush.sh

Test if the script is working properly by executing it from the Terminal:

./ha_gitpush.sh

You should have another commit pushed to Github with commit message equal to the current date and time.

Question Of The Day?

Do you make regular backups of your Home Assistant until now?

Type simple Yes or No in the comments below and I will know for what are you talking about.

Creating Home Assistant Backup to GitHub automation

To create an automation that will regularly do Home Assistant backup to GitHub you can either edit automations.yaml file or you can use the Home Assistant web interface.

I will show you the the first method and if you want to see how to use the web interface – watch my video I’m explaining the GUI method there in details.

Now open automations.yaml file in your favourite text editor or Home Assistant add-on (like File editor) and paste the following text inside it:

#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

This automation will be triggered everyday at 23:23:23h and it will execute the script that we created in the previous step.

If you want you can try to use the time_pattern platform by changing the trigger part to for example:

# Another trigger for automation just replace in the above if you want this one:
  trigger:
    platform: time_pattern
    # You can also match on interval. This will match every 5 minutes
    minutes: "/5"

To test if the fully Automatic Home Assistant Backup to GitHub work – go to Home Assistant > Configuration > Automations. Find your automation and click on the “EXECUTE” button.

Executing Home Assistant Backup to GitHub automation
Executing Home Assistant Backup to GitHub automation

Of course you can wait to the specified time in the automation and see if it will work.

Next Wednesday?

Hey If you like this article, join me again next Wednesday when I will post my new article.

Hit the subscribe button to get notified via e-mail:

Now let’s go back.

Restore Home Assistant

From now you can freely break your Home Assistant installation whenever you wish and this is how you can restore it.

Open the terminal and enter inside your Home Assistant configuration folder:

To go back to the commit before HEAD (current commit and state)

git reset --hard HEAD^

To go back two commits before HEAD (current commit and state)

git reset --hard HEAD~2

To undo the commands above (reset hard commands) and to return back to the the state before hard resets type:

git reset --hard HEAD@{1}

Support my Work!

If you like what you see so far and you want more content like this you may want to become one of my supporters (actually the first one) check my support page.

Any other sort of engagement on this site and my YouTube channel does really help out a lot with the Google & YouTube algorithms, so make sure you hit the subscribe, as well as the Like and Bell buttons If you enjoy this Automatic Home Assistant Backup to GitHub video article.

Also feel free to add me on Twitter by searching for @KPeyanski.  You can find me on my Discord server as well.

I really hope that you find this information useful and you now know how to properly do Automatic Home Assistant backup to GitHub.

Stay safe and don’t forget Home Smart, But Not Hard!

Thank you for reading, and see you next time.


41 Comments

Pablo · 23/07/2020 at 12:44 am

Excellent article, very interesting!

Thanks for doing it, Kiril.

However, have you tried to do the back up with the Google Drive Add-on ?

It’s very easy to do it!

    Kiril Peyanski · 23/07/2020 at 7:54 am

    No, but I will try it. I just realised that I’m missing the cases where integration is done through Home Assistant GUI. So I guess HA Snapshots sent to some cloud service like Google Drive will be perfect. Are you doing this already? Thanks for your comment.

Mark · 07/08/2020 at 6:56 pm

Nice article. You’ve got a number of formatted quotes which prevent copying and pasting commands directly into terminal.

for Example:
git commit -m “config files on `date +’%d-%m-%Y %H:%M:%S’`”

and
git config core.sshCommand “ssh -i /config/.ssh/id_rsa -F /dev/null”

    KIril Peyanski · 07/08/2020 at 7:04 pm

    Thank you very much for that comment The Apple Notes is the tool to blame for that. I will fix them right away.

    matt · 13/09/2020 at 1:21 pm

    Hey thanks for never fixing this -.-

      Kiril Peyanski · 14/09/2020 at 10:22 am

      What do you mean matt? Is it still not copy/pasteable? I’m pretty sure that I changed that right after Mark comment.

        andrew · 07/02/2021 at 5:36 am

        When I pasted in the the automation it changed the single quotes around 23:23:23 over to 3 dots. Not sure if that’s what Matt was referring to. The other commands that Mark mentioned worked fine for me.

        – at: ’23:23:23’

        – at: ..23:23:23…

          Kiril · 07/02/2021 at 10:40 am

          Thank you Andrew! It should be fixed already. My copy/pasting from notes app in macOS make this strange characters which result in YAML errors.

Ioannis Kapoglou · 16/08/2020 at 5:09 pm

Hi Kiril,
thx for the great article.

i did everything as described and although everything is working flawless, i cannot access my nodered webui after setting everything up.

I cant figure out how these 2 can be related, but i restored a previous backup (node red was working perfectly) , redid your method and nodered fails to load again.

In the nodered log no big errors are seen. Any ideas?

    KIril Peyanski · 24/08/2020 at 3:43 pm

    OMG that is very strange. Sorry to reply late. Do you have any progress on that?

Kyle · 17/09/2020 at 9:16 pm

Thanks so much KIril – I can run the shell script no problem from a terminal to update git. Unfortunately, though, I cannot initiate the script from an automation – any pointers for how to fix?

    Jonathan · 01/02/2022 at 6:54 am

    I have the same error. Running from terminal no problem. the automation does not work though

Kyle · 17/09/2020 at 9:45 pm

seeing this .. not updating github repository w/ automation.. but does work with running shell script manually

https://hastebin.com/namutocigi.coffeescript

Ramon Bell · 22/11/2020 at 4:27 am

I have followed this process everything has gone well expect up to the point when I generate the keys. The folder is created and the file is made for the keys but the keys aren’t present. I know the keys are made because they are present in the command line. What would be the reason the keys wouldn’t be there

bung · 18/12/2020 at 5:40 pm

would be nice to have an updated version of this guide with some kind of watchdog / notification system to notify when git push has failed

bung · 18/12/2020 at 6:03 pm

Quotes are Still Broken……..

would be nice to have an updated version of the guide with some kind of watchdog / notification if the push fails

    Kiril · 21/12/2020 at 10:29 pm

    Sorry for that, I thought I fixed the quotes before. Now I re-enter them again and it should be fine. Can you please test again? If they are not working can you point me which one are exactly?

      Paul · 10/01/2021 at 7:01 pm

      Worked perfectly for me so I’m assuming it’s all fixed!

Michael · 18/01/2021 at 4:03 am

Great tutorial! Thank you! One question. In the automations.yaml file, where did the add-on: a0d7b954_ssh come from?

– data:
addon: a0d7b954_ssh
input: /config/ha_gitpush.sh

    Kiril · 20/01/2021 at 8:35 am

    It’s kind of internal Home Assistant naming which corresponds to the SSH & Web Terminal add-on. Nothing to worry about, just use the same line in your automation.

Michael · 25/01/2021 at 2:38 am

One last question — I’m running basic Home Assistant OS on an odroid n2+ (the Home Assistant Blue). Does that create a problem for the Automation of running the shell script? I can run manually ha_gitpush.sh from Terminal, but can’t get the Automation to work. I’ve tried searching all the forums and I see the issue, but not a solution. Any insight would be appreciated!

    Kiril · 25/01/2021 at 10:17 pm

    HA blue shouldn’t be a problem. Can you please double check that you installed exactly the SSH & Web Terminal add-on? As I remember that there is similar Add-on with similar name that it is not working with the stuff described in the video/article.

      Michael · 27/01/2021 at 3:15 am

      You are the best! Problem solved. I was using the wrong add-on. The “official” one is “Web Terminal & SSH” and the one that works is a community add-on “SSH & Web Terminal”. Thanks for the guidance.

        Kiril · 27/01/2021 at 6:21 pm

        Really glad that I was able to help and that you sort out the problem. Thanks for sharing that info about the add-ons.

        Kenny · 06/03/2021 at 5:01 pm

        I’ve the same problem, here also Web Terminal & SSH. But what name off the addon did you fill in? core_ssh is also not working, Web Terminal & SSH neither

          Kenny · 06/03/2021 at 5:23 pm

          I was to soon, it is solved.

          Thanks Kiril, i like your videos.

          Kiril · 06/03/2021 at 11:04 pm

          I’m very glad that you like them 🙂

Erik · 11/02/2021 at 11:56 pm

Hi

Great instructions.

Just tried to push to git for the first time and got complaints about a large file: home-assistant_v2.db

Should I add that file to .gitignore or is that an important file to backup?

Cheers / Erik

    Kiril · 15/02/2021 at 9:53 pm

    Hi Erik,
    you can .gitignore home-assistant_v2.db file without any issues. If you restore from git backup HA will create a new home-assistant_v2.db. The only thing that will be missing is the history for the last 7 days (for example when exactly your lights were turned on/off) – it will start fresh.

James · 28/02/2021 at 12:06 am

Great post, thanks for writing it up! Does the .gitignore that you posted catch config set for integrations and extensions installed via the Supervisor add-on store or HACS?

    Kiril · 06/03/2021 at 8:00 am

    No, buy you can catch that with the Home Assistant snapshots.

TBoneCO · 26/05/2021 at 5:08 pm

Good article but ran into issues with Docker setup. Script would run if logged into host but not container. Tripped over full path names and SSH key. Used bash session to container to figure out issues one by one. Then had to switch to shell service in HA.

Roy · 17/01/2022 at 2:14 am

seeing this .. not updating github repository w/ automation.. but does work with running shell script manually – any suggestions?

    Nick · 10/02/2022 at 4:17 pm

    Same thing, did you ever figure it out and get it working via the automations?

Nick · 10/02/2022 at 4:16 pm

Everything setup and am able to trigger the script manually, but the automation does not run. Couldn’t find anything in the logs either. Any ideas? I love this solution but without the automation it’s not useful for me.

    Krede · 18/03/2022 at 6:45 pm

    The creator of the add-on removed the stdin command, that you’ll use to run the script from automations.yaml..

Travis · 17/05/2022 at 2:21 am

Like many others I have the script working manually but the automation is not.

looking at the url when I am in terminal it appears the addons is now called core_ssh. I changed this but it still doesn’t work. I am now looking into the above comment about stdin.

Perhaps we can just run it as a shell command. I will check this too.

Matt · 06/09/2022 at 1:29 am

Instead of using the stdin command, add this to your configuration.yaml:
shell_command:
push_to_github: bash /config/ha_gitpush.sh

Then after rebooting, you can call your new service ‘Shell Command: push_to_github’ in your automation. No input data required.

I.e automation should be:
alias: push HA configuration to GitHub repo
trigger:
– at: “23:23:23”
platform: time
action:
– service: shell_command.push_to_github
data: {}

    KIril Peyanski · 06/09/2022 at 7:53 am

    Thanks for the tip Matt. It is very useful!

    j2a · 09/07/2023 at 7:55 pm

    I’m getting this when using @matt’s workaround (which is further than I’d gotten before)

    “`
    2023-07-09 12:37:39.390 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /config/ha_gitpush.sh`, return code: 128
    NoneType: None
    “`

    My config:

    “`
    # Shell command instead of using the stdin
    shell_command:
    push_to_github: bash /config/ha_gitpush.sh
    “`

    My automation:

    “`
    – id: ‘1688917544672’
    alias: nightly backup to Github
    description: Github repo “hass”
    trigger:
    – platform: time
    at: ’23:23:00′
    condition: []
    action:
    – service: shell_command.push_to_github
    data: {}
    “`

John Link · 04/11/2023 at 6:14 pm

When I run the backup script it tells me that the home-assistant.log file is larger than the GitHub’s recommended file size event though I have it in the .gitignore file.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *