add local retention script

This commit is contained in:
Rowan 2025-01-02 21:13:53 -06:00
parent 891dc82a1b
commit e549409a18

23
local-retention.sh Executable file
View file

@ -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