Backing up home-assistant.log file on restarts

Using hassio 0.103.4. Whenever i restart Home Assistant from Configuration-Server Control-Server Management-Restart button, HA purges the home-assistant.log file and create a new one from scratch. I would like to examine the previous log file for problems; is there a way to back-up/rename home-assistant.log file automatically whenever i restart HA??

Thanks…

Home Assistant fires events on a lot of things, including restarts.

You could trigger on the restart and run a command line service to copy it.


  trigger:
    - platform: event
      event_type: call_service
      event_data:
        service: restart
  action: 
    service: shell_command.backup_logs

Then in config, define the shell command

shell_command:
  # Backup logs and append a version number if one already exists.
  backup_logs: cp -r --backup=t /path/to/logfile /path/to/backup/folder

This might work. I mean, what’s the point of firing off a restart event if you don’t let other things handle it!

Or another documented method (works every time I shut down/restart HA)

- alias: "[homeassistant] on shutdown"
  trigger:
    platform: homeassistant
    event: shutdown
  action:

Well the logic is completely right, however on shell command i am stuck with hassio not letting using folder or file. Both ‘/home-assistant.log’ and ‘/usr/share/hassio/homeassistant//home-assistant.log’ is giving error; maybe from the file priviliges. As i see lots of people are having the same problem. Thanks anyway…

So the problem is shell command. Check this

Think i found the culprit. Tried

sudo docker exec -it homeassistant /bin/bash

and in the shell i tried:

cp -r --backup=t /path/to/logfile /path/to/backup/folder

As i see the shell in Hassio HA uses busybox; so the busybox cp command does not have options like ‘–backup=t’. So i entered:

shell_command:
  backup_logs: cp /config/home-assistant.log /config/external_data/

which works like a charm! Now i need to find a way to rename backup log files which is busybox compliant…

Thanks for all your help.

UPDATE:
Solved also the version renaming issue. For future reference, here they are:

shell_command:
  # Backup logs and append a version number if one already exists.
  backup_logs: cp /config/home-assistant.log /config/external_data/log-`date +"%Y%m%d%H%M"`.log
- alias: "Backup Log file"
  trigger:
    platform: homeassistant
    event: shutdown
  action:
    service: shell_command.backup_logs
10 Likes