Delete old backups shell command error

I have a shell command to delete old backups that runs every night, right after I make my nightly backup.

The backup works but the deletion fails.

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

The error received is:

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

NoneType: None

I’m running hassio on a raspi 3. I can see backups older than 30 days in the SAMBA /backup/ share.

Any idea why this fails?

Also just FIY these backups are automaticly copied off the pi to a NAS in case of catastrophic sd card failure.

Try

find /backup/* -mtime +30 -delete

Thanks I’ll give it a go.

Same problem unfortunately:

2018-09-21 01:00:11 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `find /backup/* -mtime +30 -delete`, return code: 1

And files older than 30 days are not deleted.

It’s not a secure as you might want but you could try

find /mnt/usbbackup/* -mtime +30 -execdir rm {} +

Why is it not secure?

If I understand correctly, if someone were to go the directory where the files are and create a “rm” shell script, and “.” was in your path, they could have it run whatever they wanted. It’s the typical linux caveat, if someone hacks into your system plants a malicious command somewhere, it could do damage. :slight_smile:

Thanks for the explanation. I’ll try the shell command tonight and see how it goes. Should it have a semi-colon (;) at the end?

I do not believe so, but it may depend on what linux shell you are using. Try it without it and see if it throws an error.

1 Like

Hmm. Still no go.

Error running command: `find /mnt/usbbackup/* -mtime +30 -execdir rm {} +`, return code: 1

NoneType: None

No wonder. This is not where my backups are: mnt/usbbackup

Still cant get this to work.

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

NoneType: None

What does this return?

I don’t have SSH installed to try it. Will install it tomorrow and try. Very late here. Thanks for your help.

It finds one file.

core-ssh:/backup# find /backup/* -mtime +30 
/backup/ca7bab03.tar

This is the only file older than 30 days in /backup (I had a clean out yesterday).

I’ve been trying this also and what I’ve found is that -execdir doesn’t work however -exec does work :slight_smile:

So mine looks like …

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

Very similar to my first post:

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

NoneType: None

I have noo idea what “+” does instead of “\;” but I’ll give it a go. Thanks.

From the help file…

-exec CMD ARG + Run CMD with {} replaced by list of file names

similar to what you were doing…

-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero

Is it not -30? I set this up on a Windows schedule yesterday…

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…