Defjam121
8/31/2017 - 12:15 AM

bash single file backup rotation script

bash single file backup rotation script

backup_location=/opt/backup
file_location=/opt/test
file_name=file.txt
max_number_of_backups=5

cp $file_location/$file_name "$backup_location/`date +%Y-%m-%d_%Hh%Mm%Ss_`$file_name"

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