Help with backup script

So i have been doing my backup script, but i have stumbled with this obstacle and would enjoy some help:

The system must have the option to set the amount of saved backups

from the same directory. For example, if the quantity is four and already
there are four saved backups of the same directory, at any time.
Once the new backup is done, the oldest one must be automatically removed.

This is the logic you would use AFTER the the backup operation completed successfully.

#!/bin/bash
# List files from newest first to oldest last
declare -a array=(`ls -t /path/tobackup/filename*` )  
# if we have more than 4 remove fifth (the oldest file) 
# NOTE:  arrays count starting from zero
[ ${#array[*] -gt 4 ]  && rm ${array[4]} 

edit: remove -r from ls command

1 Like

What if i want the user to define the limit of backups?