Auto delete files from storage

I use a pendrive on my openwrt device. On this pendrive a ip cam save video (only on detects movement).

The problem is: when the pen drive is full ip cam stop to save video.

Is there a way to auto delete the old files? Per example when the disk space exceeds 95% of sizeopenwrt delete OLD files until 85% of size(from 95% to 85%). In this way the ip cam have always size to save data and I lost only old video.

Is it possible?

Sure. A cronjob (standard UNIX tool) - you could e.g. periodically probe df output and start deleting things once your treshold has been reached.

Keep in mind USB sticks aren't meant for this type of use so they'll wear out fast unless you use a wear-leveling file system.

1 Like

For example, I use this cron job to delete all directories older than 90 days.

timeout -k 20m 15m /usr/bin/find /mnt/sda1/ftp/cam/uploads/ -type d -maxdepth 1 -mtime +90 -exec rm -rf {} \;

I use -type d (directory) because my ip cam creates a folder for each new day.
So basically this cron job deletes all directories older than 90 days with their contents.
timeout -k 20m 15m will try to end the task after 15 minutes (SIGTERM).
If it fails, timeout tries to kill the task after 20min. (SIGKILL)
timeout needs to be separately installed.
opkg install coreutils-timeout

To delete old files only after a certain usage threshold is reached on the filesystem, you could create a script that checks the file system usage.

For example, you could use this to get the usage threshold:
df -k /mnt/sda1 | tail -1 | awk '{print $5}'
Output:
9%

To only get the number (for easier comparison in scripts)
df -k /mnt/sda1 | tail -1 | awk '{print $5} | grep -Eo '[0-9]+'
Output:
9

3 Likes

logrotate with maxsize option, though intended for log files, should do the trick for this use case too

1 Like

Thanks to all fr reply.

Just 3 considerations:

  1. I cannot use "time" as delete criteria. The ip cam save video only on motion detection and not continuously. It save short video (and files) every time there is a motion detection. If I use "file time" are delete criteriathe device can delete all files (because the ip cam hasn't registered recently) also if there is free space on storage. I should check storage status and delete old files. Is this possible witha cron script?
  2. I'm not an expert, I have configuared my device some months ago to works with openwrt (samba, external storage, wifi connection, ...) and now it works. Is there a GUI package for cron? If possible I'like avoid the shell command. I have use it on device configuration but I have spent many time to do it :slight_smile:
  3. USB is temporary. I will replace it with a small hard disk after all works. Is a mechanical hard disk or an SSD better fot this use?
  1. What criterion do you want to use?
  2. Cron is console package principally, you should just add job and enable it.
  3. I think, HDD is better, because it has much more rewriting cycles. SSD is faster, so if speed of HDD suffices, use it.

My ip cam does the same.
Why can't you use the file creation timestamp as a criterion?

I think the correct criteria must be the storage use quote. The cron script must check the % of storage use and then delete the files from the older. For example every 15 minutes the script must control if storage quote is more that 85% and if this is TRUE delete the files (from the more older) until have a storage quote of 65%. In this way there is always free space for new videos and only old videos will be delete. This is possible with a cron script?

Because if the cam rarely records you would risk deleting all the videos for no reason (just because they are old not because need more space for new videos)

Yes, it could be implemented.
Check space with df command, delete files in while cycle.

Since you are not writing continuously or at high rates, either SSD or spinning-platter is fine. Except for enterprise-grade (expensive) hard drives, SSDs are probably a better choice between lower power draw, no vibration/drop problems, and even just availability.