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 +