I am using crontab (on Ubuntu Server 20.04) to automatically create daily backups of a text file and storing them on an external hard drive. I am using the following crontab to create my backups:
@daily cp ~/myfile.txt /mnt/usb/backups/$(date +\%Y\-\%m\-\%d)_myfile.txt
This effectively creates a daily backup file on my external hard drive with the date and time (year/month/day) of creation prefaced in the filename.
i.e. /mnt/usb/backups/2023-02-01_myfile.txt
and /mnt/usb/backups/2023-02-02_myfile.txt
etc.
The problem is that the hard drive is eventually going to fill up. So what I want to do is automatically delete the oldest version of the backup image file (but only delete when external HD space is running out) every time the cp
backup command runs in crontab. This would prevent the external drive from filling up by deleting the oldest version of the backed up file when the disk space is near capacity.
Does anyone have any idea how I might go about doing this?