Shut down Home Assistant cleanly before shutdown Docker

Hello,

I’ve seen a lot of info on the forums about shutting down host operating system, but what I need is to cleanly shutdown HA itself, which I have running in Docker on a Synology NAS. I’m not sure if simply shutting down the container is going to shut it down without breaking the recorder database.

Basically I have a scheduled task on the Synology NAS shutting down the Docker container every night to back up config files, then restarting the docker container.

Is there a better way to do this? Command line or something to cleanly shutdown HA? Maybe can put that in the backup script or in the Docker container’s shutdown script?

Thanks

I have HA running in a container, and I don’t shut it down when I back up my configuration. Sure, I get the message blank has changed as we read it, but it has seemed to work for me. I have had to use those backups when I had a HDD failure on my machine running HA, and there was no data loss (outside of the information between the backup and the restoration).

I don’t automate my backup, every time I make some major changes to my configuration I make a backup. I have a script that backups the configs of a few docker configs and puts each one in its own tar ball and names it with the Application Name and the current date in the file name. Then I manually copy it off the docker machine to my NAS.

If you are concerned with data loss of sensor data, then I would look into exporting to something like InfluxDB.

I’ve been backing up the entire config directory. When I do that with HA running, it breaks the HA database. Should I just exclude the database file from the backup and run the backup with HA running?

How often are you writing to the database? I think I have mine set to every 10 seconds, so that could make a possible difference.

I have it set to default, I might change to 10sec or something. How would a script look where HA manually flat copies out the config folder?

like this!:

Also I have had no issues shutting down the system without stopping HA. I have also had no issues backing up the config folder while HA is running, there do not appear to be any locks that prevent it, and the contents of the backup look just fine.

Here is my script:
I have this in my home directory “~” and run it with sudo. This script being named “ConfigBackup.sh”

sudo ./ConfigBackup.sh
#!/bin/bash
TIME=`date +%b-%d-%y`                 # Set the date for filename.
DESDIR=/home/glenn                    # Destination of backup files.
#FILENAME=backup-XXXXX-$TIME.tar.gz   # The filename including the date.
#SRCDIR=/home/glenn/XXXXX/            # Source backup folder.


echo Starting Backups
echo ....
#Grocy
echo Starting Grocy Backup
FILENAME=backup-grocy-$TIME.tar.gz
SRCDIR=/home/glenn/grocy_config/
tar -cpzf $DESDIR/$FILENAME $SRCDIR
echo Grocy Backup Complete

echo ...

#Home Assistant Config Folder
echo Starting Home Assistant Backup
FILENAME=backup-hass-$TIME.tar.gz
SRCDIR=/home/glenn/hass_config/
tar -cpzf $DESDIR/$FILENAME $SRCDIR
echo Grocy Home Assistant Complete

echo ...

#LetsEncrypt
echo Starting SWAG Backup
FILENAME=backup-letsencrypt-$TIME.tar.gz
SRCDIR=/home/glenn/letsencrypt_config/
tar -cpzf $DESDIR/$FILENAME $SRCDIR
echo SWAG Backup Complete

echo ...

#Jellyfin
#echo Starting Jellyfin Backup
#FILENAME=backup-jellyfin-$TIME.tar.gz
#SRCDIR=/home/glenn/jellyfin_config/
#tar -cpzf $DESDIR/$FILENAME $SRCDIR
#echo Jellyfin Backup Complete

echo ...
echo All Backups Complete

1 Like

Fantastic, thanks for sharing!

I’ll play around with it and see what sticks.

I appreciate your help.

If you plan on doing this in a cronjob then I would just comment out all the echos.
I have the echos in since I run it on the command line manually; although I think my automated scripts have more info since I send statuses to MQTT (for some, not all)

You could possibly do it a bit easier.

there is a homeassistant.stop service that stops the HA server.

So you could run that service in an automation a minute before your scheduled usual backup script that shuts down the container.

That way the server is already stopped before the container gets stopped so the DB shouldn’t ever get corrupted.

Then in your backup script the container should get restarted which (I think) automatically restart the HA server.

That last bit is the only part I’m not completely sure about. I’ve never tested it like that before. So you can easily check it yourself tho.

1 Like

I’m not sure I like splitting the automation like that, but in the end that might be the way to go. Or just exclude the database from the backup, leave HA container running, and call it a day.