I’ve got an automation within HA creating a backup of it’s config every night at 3am. It’s working fine, however I’ve noticed that there’s no way to remove old backups, so the list just grows and grows.
Does anyone have a solution for removing old backup tar files after (say) 7 days?
Did you ever find a solution to this? I’ve been looking at the same and the only solutions I can find seem to be for HASS OS and not running in a docker
# delete old backups
# flow: open backup folder & list files sorted by age -> delete first 3 entries (jungest files) from output -> run remove on each left
sensors
- platform: command_line
name: delete old backups
command: 'cd /config/backups/ && ls -A1t | sed -e "1,4d" | xargs rm'
I use this in my HA docker container. So your path might be different. By default command line sensors are called every minute. Setting scan_interval to some more useful value might be advised.
Hi and thanks for sharing the solution. I’ve used it to create a command_line switch so it can be used by the backup-Automation:
- platform: command_line
switches:
purge_backups:
friendly_name: Purge HA-config Backups
unique_id: switch.purge_backups
icon_template: mdi:trash-can
# go to backup direcotory, list files by date, filter result list to hide the first 7 results (show from nr.8) and remove remaining
command_on: cd /config/backups/ && ls -A1t | tail -n +8 | xargs rm -v
The switch will stay turned on if you use it in lovelace, but that isn’t my scope. I just use it in the automation to trigger it and then turn it of again:
Adds a service which runs your command. No unnecessary toggling. Or
Basically the same thing except stdin is set from message before the command is run, allowing you to pass some value as input from your automation/script.
I just tried to set up a backup purge command using the Shell Command integration.
Correct me if I’m wrong, but the /backup dir is not mounted in the homeassistant container, so we can’t use that integration for this specific case?
From the docs:
If you are using Home Assistant Operating System, the commands are executed in the homeassistant container context. So if you test or debug your script, it might make sense to do this in the context of this container to get the same runtime environment.
I tried to set it up and I managed to remove the files. The issue I have now is that HomeAssistant still keeps the references to the files in Settings->System->Backups list. The files don’t exist anymore so when trying to download a file it returns 0B file (so it’s expected) but I’d like also to clean up those references. I don’t see any service (only backup.create). Anyone solved it?
I’ve just started using this integration hass auto backup and it adds a lot more capability around backups. I’ll report back once I have more experience with it but it seems to work well so far.
Just adding an update - this looks good but Home Assistant configuration.yaml now separates command_line stuff…
command_line:
- switch:
name: Purge Backups
# go to backup direcotory, list files by date, filter result list to hide the first 7 results (show from nr.8) and remove remaining
command_on: cd /config/backups/ && ls -A1t | tail -n +8 | xargs rm -v
for those who prefer to do it on Bash I wrote myself a little script which does the handling and is started daily by cron.
it creates a lockfile in case the job run more often than only a single process
removes all backups older than 7 days (properly for HA, not only removing the files)
creates a new backup
That’s it. Match the files and directories according to your need.
#!/bin/bash
#set -x
LOCKFILE=/var/lock/backup.run
# Einen lock mit FileDescriptor 958 erstellen
exec 958<>$LOCKFILE ||exit 1
flock -n 958 || exit 1 # Non-Blocking. Wenn das nicht geht, steigt er mit Fehlercode aus
# Löscht die Lock-Datei bei Erhalt von "EXIT" Signal
trap 'rm -f $LOCKFILE' EXIT
# Und los geht's
NAME="CRON_Vollbackup_am_`date +%d.%m.%y`"
pushd /usr/share/hassio/backup
find . -ctime "+7" > /tmp/filelist.$$
if [ ! -z /tmp/filelist.$$ ]; then
for i in $(cat /tmp/filelist.$$) ; do
FILE = $(basename -s .tar $i)
ha backups rm $FILE
done
fi
rm -f /tmp/filelist.$$
popd
ha backup new --name \"$NAME\"
rm -f $LOCKFILE
exit
I can’t get this to work with Home Assistant OS either. The command gets executed in the ‘homeassistant’ docker’s environment. There is no backups there. I have not yet figured it out.
If you have the advanced ssh and web terminal addon installed, you could create a shell_command to run the code inside the ssh addon container, which does have the backup volume mapped: