b2-data-retention/local-retention.sh

23 lines
527 B
Bash
Executable file

#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 -print )
old_dirs=$( find "$ARCHIVE_DIR" -maxdepth 1 -mindepth 1 -type d -print )
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"
done
for dir in $last_n_dirs; do
rm -r "$dir"
done