Backup your configs as often as possible

I’ve just lost months worth of setup and tweaks of home assistant config. Not sure of the root cause at the moment, but I just wanted to remind everyone of the time they spend setting all this up and the seconds it takes to copy it to a different machine (at the minimum). There are guides to backup to github, Dropbox, and USB already on the home assistant site. Follow them! I will do on my fresh install.

Below is a rant/pondering that nobody has to read, but if you have any ideas how to prevent the same thing again then feel free to chime in.

The USB that was booting my Pi for some reason stopped booting (stuck at either the rainbow screen, or the 4 raspberry screen). Dont know why. The files all seemed present. I tried replacing all the boot partition with a newer version, but still it wouldn’t boot. I then booted an ubuntu live USB and copied all my home directory from the raspbian partition to what I thought was the live ubuntu USB storage. Installed fresh raspbian on the USB stick and now it works. I went to restore all my configs and lo-and-behold the live USB contains nothing that I put there. I guess I didn’t think about persistence and just assumed I could copy files across. There was no read-only warning. Lesson learned I suppose. So I don’t know when I’m going to get round to redoing all my config and automations, fortunately some of them were posted on here. I think firstly I’ll set up a backup cron to backup to my other server.

MUCH LATER EDIT: I just got a like on this post, 8 months after I originally posted it so in case anyone else comes by here and is interested, there is another forum post where I include a much better version of the .cmd file.*

Look here → [I give up, just is not worth the time and effort - #122 by sparkydave]


EDIT:
If anyone is interested I decided to add the following to my .cmd file so that only files newer than 14 days are kept. (PLEASE CHECK YOUR PATH BEFORE USING!!!)

forfiles /P "D:\Users\[user_account_name]\OneDrive\Home Assistant Snapshots" /D -14 /C "cmd /c del @file"

Is there a way to automatically delete old snapshots on my Pi from within HA?


If anyone else like me doesn’t use Dropbox or GitHub but does use OneDrive (i.e. every Windows user who signs on to their PC with a Microsoft account) then another solution is as follows. It’s not as slick as the other solutions but once setup it is automatic. And I’m not claiming to have been clever here, what I use isn’t rocket science but it might hep anyone who hasn’t considered it.

I have an automation that does a nightly snapshot:

  #==================================
  #== Create a daily snapshot backup 
  #==================================
  - alias: Daily snapshot
    trigger:
      platform: time
      at: '3:15:00'
    # No need for backup if on holiday as nothing will have changed
    condition:
      condition: state
      entity_id: input_boolean.holiday_mode
      state: 'off'
    action:
      service: hassio.snapshot_full
      data_template:
        name: automated backup {{ now().strftime('%Y-%m-%d') }}

On my PC which I log in to every day I have a .cmd file that does a Robocopy to OneDrive

@echo off

REM
REM --- Copy Home Assistant Snapshots to OneDrive 
REM

echo ====================================================================
echo COPYING - Home Assistant Snapshots
echo ====================================================================

robocopy "\\[ip address of HA Pi]\backup" "D:\Users\[user_account_name]\OneDrive\Home Assistant Snapshots"

pause

Robocopy is included in all versions of Windows since (I think 8). The pause is unnecessary but gives a chance to see that it has run correctly.

I then set up a scheduled job to run the .cmd file every time I log on to my Windows PC. (Search for Task Scheduler on your PC).

I have also set the folder to be available on and off line.

This way I have a snapshot every day on my Pi, on my PC and in the cloud.

I hope this is useful.

2 Likes

Backing up is a good idea, but worthless if you don’t try a restore. So, check that after backup

  • the data is complete
  • the data is usable

You may make use of a temporary Home Assistant Docker container to do the checks.

Totally agree about making the backups, also experienced a crash of my RPi 1 some months ago. Now i’m making a daily backup with an SH script running as Cronjob on the new RPi 3.

  1. SH script making tar.gz file with all the files except the db file
  2. After tar.gz is ready the SH script uploads the tar.gz file to a FTP server on my network

Script runs daily at 11PM as this is the moment i’m normally not working on my setup anymore. I can also invoke the script with the command-line sensor to manually make a backup.

Would you recommend docker to run home assistant (not just the backup test). I’ll be reinstalling fresh now and I used a python virtual env last time.

A shell script on cron sounds like a good idea. I already had this for PlexPy, so not sure why I didn’t include home assistant!

I run HA in Docker on Raspbian for about a year now and it has become much easier since then. Previously I used venv to, but updates and dependency management were giving me hell.

I also used Docker Compose to bundle up the HA container with a Homebridge container, but dropped the dedicated Homebridge instance in favour of the embedded Homebridge feature of Home Assistant.

In general

  • I run natively what is available as OS distribution package (e.g. nginx, Homegear)
  • I put everything else into Docker containers (e.g. NodeJS / Python Apps)

Absolutely! The number one thing I do when testing various backup solutions for things. Try the restore! So many people forget this very important step and have no idea if it works until that real test comes.

If you have another server (e.g. a NAS) set it up as a git server, then you convert your HA config directory to be a git repository.
Any changes in the config once tested can be pushed to the remote server (plus you’ll have version control of your HA config).
You may also have an automation to check daily to see if your local config is up to date (in case you do some work from other hosts too)

I don’t have one of those clean configs to share. Mine is usually a mess of testing stuff as I am really not a GUI guy as I go for automations mixed with a little voice control. I version control things enough with my backups plus it backs everything up that I wouldn’t backup on any git.