You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/roles/common/templates/backup_autoremove.sh

22 lines
493 B
Bash

#!/usr/bin/env bash
set -euxo pipefail;
backupsToKeep={{ backups_to_keep | quote }};
function onlyDatedFiles() {
grep --perl-regexp '/\d+(-\d+)*(\.[^/]+)*$';
}
function getDirName() {
grep --only-matching --perl-regexp '^.+(?=/[^/]+)';
}
find -H {{ backups_directory | quote }} -type f |
onlyDatedFiles |
getDirName |
sort --unique |
while read -r dir; do
find "$dir" -type f | onlyDatedFiles | sort --reverse | tail --lines=+$((backupsToKeep + 1)) | xargs rm --force;
done