Rotate backups with Bash script

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.