From 012b572eb928651bd4031f9adee60eb105d68cd1 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 17 Aug 2020 02:39:35 +0200 Subject: [PATCH] Fix remove amount calculation in backup.sh --- .scripts/backup.sh | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/.scripts/backup.sh b/.scripts/backup.sh index 326ca69..ef6b856 100755 --- a/.scripts/backup.sh +++ b/.scripts/backup.sh @@ -37,21 +37,25 @@ if [ "$(dirname "$0")" != "." ]; then exit 1 fi +# Enable mathematics in POSIX shell +calc() { awk "BEGIN{print $*}"; } + # All directories -BACKUP_FILES="$(find . -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r)" +BACKUP_FOLDERS="$(find . -mindepth 1 -maxdepth 1 -type d)" # Current amount of backups -BACKUP_COUNT=$(echo "$BACKUP_FILES" | wc -l) +BACKUP_COUNT=$(echo "$BACKUP_FOLDERS" | wc -l) + +# Calculate the amount needed to remove +AMOUNT_REMOVE=$(calc "$BACKUP_COUNT" - $BACKUP_COUNT_MAX + 1) # Remove the oldest backup folders -if [ "$BACKUP_COUNT" -ge $BACKUP_COUNT_MAX ]; then - BACKUPS_OLDEST="$(echo "$BACKUP_FILES" | sed -nE " - 1,${BACKUP_COUNT_MAX}d; - s/^([0-9]{2}|-|_|:){12}$/\0/p - ")" - - printf "%s\n%s\n" "Removing directories:" "$BACKUPS_OLDEST" - rm -rf $BACKUPS_OLDEST +if [ "$AMOUNT_REMOVE" -ge 1 ]; then + REMOVE="$(echo "$BACKUP_FOLDERS" | sort | head -n "$AMOUNT_REMOVE")" + + echo "Removing directories:" + echo "$REMOVE" + rm -rf $REMOVE fi # Execution location