Delete old backups shell command error

if you use -ve number it takes the newest files :frowning:

core-ssh:/backup# find /backup/* -mtime -5
/backup/55f83d3e.tar
/backup/93282f30.tar
/backup/cd708564.tar
/backup/f510d046.tar
core-ssh:/backup# find /backup/* -mtime +5
/backup/0d00a7d8.tar
/backup/31238b08.tar
/backup/3c14e9d6.tar
/backup/3ea6fbd3.tar
/backup/a6f2844a.tar
/backup/b2f5c512.tar
/backup/b9336de2.tar
core-ssh:/backup# ls --full-time -t
total 742532
-rw-r--r--    1 root     root          8192 2018-09-24 23:29:17 +0100 f510d046.tar
-rw-r--r--    1 root     root         69632 2018-09-23 23:27:18 +0100 93282f30.tar
-rw-r--r--    1 root     root      96716800 2018-09-23 01:04:29 +0100 cd708564.tar
-rw-r--r--    1 root     root         24576 2018-09-20 23:26:59 +0100 55f83d3e.tar
-rw-r--r--    1 root     root      86650880 2018-09-19 23:06:03 +0100 ec7bfd29.tar
-rw-r--r--    1 root     root      64020480 2018-09-15 23:04:11 +0100 a6f2844a.tar
-rw-r--r--    1 root     root      77598720 2018-09-14 23:05:33 +0100 3c14e9d6.tar
-rw-r--r--    1 root     root      78172160 2018-09-13 23:05:08 +0100 b2f5c512.tar
-rw-r--r--    1 root     root     123033600 2018-09-12 23:07:15 +0100 0d00a7d8.tar
-rw-r--r--    1 root     root     101079040 2018-09-11 23:05:58 +0100 3ea6fbd3.tar
-rw-r--r--    1 root     root      75755520 2018-09-10 23:04:23 +0100 31238b08.tar
-rw-r--r--    1 root     root      57210880 2018-09-09 23:03:38 +0100 b9336de2.tar

Yeah ok… perhaps it’s different with the windows command I used… was just a thought.

In Windows I have this command:

ForFiles /p “C:\Users\david\Dropbox\Apps\Docker Backups NUC” /s /d -2 /c “cmd /c del @file

Where the /d -2 denotes files older than 2 days. It does exactly that and stops my dropbox filling up…

Still not deleting old files.

This works fine via ssh: find /backup/* -mtime +30 -exec rm {} +

But trying to run it as a shell command:

delete_old_snapshots: find /backup/* -mtime +30 -exec rm {} +

fails with

2018-09-26 01:15:11 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: find /backup/* -mtime +30 -exec rm {} +, return code: 1
NoneType: None

I can only assume it is some sort of permissions thing.

My hassio build is down atm as I’m moving over to a small desktop ubuntu machine so I can’t test it. There must be a way to add the permissions needed by rm, but I don’t know them :frowning:

I think I worked it out…
See this thread:

Thanks David. I’ll give this a go.

delete_old_snapshots: find /usr/share/hassio/backup/* -mtime +30 -exec rm {} +

Might have to wait a couple of days for confirmation. I deleted the old files via ssh.

No luck unfortunately.

Error running command: `find /usr/share/hassio/backup/* -mtime +30 -exec rm {} +`, return code: 1

NoneType: None

run
which find
and give the full path for find. in my case it would be
/usr/bin/find /backup/* -mtime +30 -execdir rm {} +

The trouble with that is SSH might work in a different container to HA, as evidenced by the previous commands working via SSH but not as shell_commands.

but the “find” command should be installed in the same directory regardless. There shouldn’t be two of them on the same server. What I’m thinking is that “find” isn’t being found in whatever environment you have when your shell command is run. You may have said this earlier, is this being run as a cron job or some other scheduler.

A time triggered automation calls the shell command.

core-ssh:~# which find
/usr/bin/find

so try putting /usr/bin in front of your find command and see if specifying the path to find helps.

Sigh.

Error running command: /usr/bin/find /usr/share/hassio/backup/* -mtime +30 -exec rm {} +, return code: 1

One last thing to try:

/usr/bin/find /backup/* -mtime +30 -exec rm {} +

Error running command: `/usr/bin/find /backup/* -mtime +30 -exec rm {} +`, return code: 1

NoneType: None

That’s it. I’m out of ideas. Going to delete them from the samba share using task scheduler on another always on PC on the network.

Which is the approach I’m using. I think the issue as I said is the docker container running the command_line in yaml is different to the ssh container and those files just aren’t available in the command_line container.

@DavidFW1960 do you just have a batch file with the command you posted above called by windows task scheduler?

Not a batch file. Using Task Scheduler defining an action like this:

1 Like

Just as well I tested this first. Going to have to map the samba share to a drive.

C:\Users\Tom>ForFiles /p "\\hassio\backup" /s /d -30 /c "cmd /c del @file"
ERROR: UNC paths (\\machine\share) are not supported.
1 Like