From e549409a183dc8ca0cb37dd36e8f71037740a9bb Mon Sep 17 00:00:00 2001 From: rowan Date: Thu, 2 Jan 2025 21:13:53 -0600 Subject: [PATCH] add local retention script --- local-retention.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 local-retention.sh diff --git a/local-retention.sh b/local-retention.sh new file mode 100755 index 0000000..7453bfb --- /dev/null +++ b/local-retention.sh @@ -0,0 +1,23 @@ +#1/usr/bin/env sh + +list_after_n() { + echo "$1" | sort -r | tail -n +"$2" | tr '\n' ' ' +} + +ARCHIVE_DIR=${ARCHIVE_DIR:-"/archive"} +FILE_QUOTA=${FILE_QUOTA:-7} + +old_archives=$( find "$ARCHIVE_DIR" -maxdepth 1 -type f -printf "%f\n" ) +old_dirs=$( find "$ARCHIVE_DIR" -maxdepth 1 -mindepth 1 -type d -printf "%f\n" ) + +last_n_archives=$( list_after_n "$old_archives" "$FILE_QUOTA" ) +last_n_dirs=$( list_after_n "$old_dirs" "$FILE_QUOTA" ) + +for archive in $last_n_archives; do + rm "$ARCHIVE_DIR/$archive" +done + +for dir in $last_n_dirs; do + rm -r "$ARCHIVE_DIR/$dir" +done +