Cannot automate cleaning up of /backup on Home Assistant OS

I have seen this question asked a few times with no answer.

I am using the hassio.backup_full service daily to make a backup. I am running the Home Assistant OS on Raspberry Pi and running an rsync over ssh command to copy my backups to my Mac. (The command is initiated through my Mac’s crontab).

I only want to keep backups for the last 7 days on my Raspberry Pi. So I want to run this command nightly in Home Assistant OS:

find /backup -ctime +7 -exec rm {} \;

I tried creating a shell_command like this:

shell_command:
  clean_up_old_backups: "find /backup -ctime +7 -exec rm {} \\;"

But that command fails because it cannot find /backup.

The documentation says that shell commands run in the “homeassistant” docker context, so I ran the following command to find out what my directory structure looks like:

docker exec -it homeassistant bash

and sure enough /backup is not there.

There might be a way to mount the /backup directory somehow, but I can’t figure out how to do this.

When setting up the Docker for Homeassistant, you create the directory

Example:

I have it in a directory with Samba Share
So I can use this as a network folder on my laptop/tablet.

Thanks @apreick . I’m actually using Home Assistant OS (I don’t build anything, I just installed an image on my Raspberry Pi). But I believe that as part of the normal operation of Home Assistant OS, it uses the “homeassistant container context” (see snapshot of Shell Command documentation). I assumed they are referring to a docker context, but maybe it’s something else. It’s a bit nebulous.

Remove the leading /. That should get you to the backups directory (it’s named backups, not backup).

No. The default location of the backup directory in Home Assistant OS is /backup.

➜  / ls /          
addon_configs  data           lib            proc           ssl
addons         dev            lib64          root           sys
backup         etc            media          run            tmp
bin            home           mnt            sbin           usr
command        homeassistant  opt            share          var
config         init           package        srv
➜  / 

From Backup documentation page:

Interesting… according to the docs, it’s backups. Backup - Home Assistant

What version of HA are you running?

lol of course HAOS does it differently.

Yup. I’m happy to move my backup directory somewhere else (in /share ?) but that would create a recursion since /share is apparently included in the backup. Ugh… there HAS to be a proper way to do this.

There are a few FRs to allow the backup service to do it itself or at least expose a service call to do it. (Service to remove backup).

Removing the leading slash didn’t work? The only other thing I can think of would be to SAMBA share the /backup directory and use another system to do it perhaps. I don’t know as I don’t use HAOS (I run Container).

I just voted for that feature :slight_smile:

Yes, I could use Samba… Maintaining Samba for me is more work than periodically (every 6 months?) running the purge command manually. I just sucks that the computer can’t do that for me. So simple :pensive:

1 Like

I mean, fundamentally, it should.

Ok, one more thing… try this: find ./backup -ctime +7 -exec rm {} \; and see if that works. I tried it in my docker container and it seems to work.

Oh, of course that command runs fine. That’s what I meant by “manually” :grinning: (not n number of “rm” commands)

1 Like

So yay? It works for you? :slight_smile:

When in doubt make a bash script and run THAT in your command line instead of the command. If it works in shell and works in bash then your solution is at hand. That way you don’t have to navigate any tricky syntax issues in YAML and you can test easily until it’s dialed in.

1 Like

Yeah, there’s a way to do it manually.

If anybody reading this thread knows the answer to my original question, I’m all ears!