Here is a simple script to rotate your backups:
#!/usr/bin/env bash backup_location=/mnt/data/backups max_number_of_backups=5 function number_of_backups() { echo `ls -1 $backup_location | wc -l` } function oldest_backup() { echo -n `ls -1 $backup_location | head -1` } while [ $(number_of_backups) -gt $max_number_of_backups ] do rm -rf "$backup_location/$(oldest_backup)" done
You should of course change backup_location and max_number_of_backups to fit you environment and needs.
Your variable “$backup_location” in the “function oldest_backup” is not consistent with it being in all caps: $BACKUP_LOCATION
If any one uses this script, please update line 11 with the case sensitive variable $backup_location.
Replace $BACKUP_LOCATION with $backup_location
Bye
Thanks for pointing this out! I have updated the post.