tools: add ipcam_cleanup.sh script
This commit is contained in:
parent
72a45b8521
commit
8d4045f6c3
102
tools/ipcam_cleanup.sh
Normal file
102
tools/ipcam_cleanup.sh
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Function to display usage
|
||||||
|
usage() {
|
||||||
|
echo "Usage: $0 --dir <dir1> [<dir2> ...] [--min-free-space <space_in_GiB>] [--debug]" 1>&2
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to output error messages and exit
|
||||||
|
error_msg() {
|
||||||
|
echo "Error: $1" 1>&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to output debug messages
|
||||||
|
debug_msg() {
|
||||||
|
if $DEBUG_MODE; then
|
||||||
|
echo "$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Initialize variables
|
||||||
|
declare -a DIRS
|
||||||
|
MIN_FREE_SPACE=200 # Default minimum free space in GiB
|
||||||
|
DEBUG_MODE=false
|
||||||
|
PARSE_DIRS=false
|
||||||
|
|
||||||
|
# Parse command-line arguments
|
||||||
|
for arg in "$@"; do
|
||||||
|
if $PARSE_DIRS; then
|
||||||
|
if [[ "$arg" =~ ^-- ]]; then
|
||||||
|
PARSE_DIRS=false
|
||||||
|
else
|
||||||
|
DIRS+=("$arg")
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
case $arg in
|
||||||
|
--dir)
|
||||||
|
PARSE_DIRS=true
|
||||||
|
;;
|
||||||
|
--min-free-space)
|
||||||
|
MIN_FREE_SPACE="$2"
|
||||||
|
shift # Remove argument value
|
||||||
|
;;
|
||||||
|
--debug)
|
||||||
|
DEBUG_MODE=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if [ "$arg" != "${DIRS[-1]}" ]; then
|
||||||
|
usage
|
||||||
|
error_msg "Unknown parameter passed: $arg"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check if at least one directory is provided and if it is indeed a directory
|
||||||
|
if [ ${#DIRS[@]} -eq 0 ]; then
|
||||||
|
usage
|
||||||
|
error_msg "No directories specified."
|
||||||
|
fi
|
||||||
|
|
||||||
|
for DIR in "${DIRS[@]}"; do
|
||||||
|
if [ ! -d "$DIR" ]; then
|
||||||
|
error_msg "'$DIR' is not a directory."
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check if all directories are on the same disk
|
||||||
|
PREV_DISK=""
|
||||||
|
for DIR in "${DIRS[@]}"; do
|
||||||
|
CURRENT_DISK=$(df "$DIR" | tail -n 1 | awk '{print $1}')
|
||||||
|
if [ -n "$PREV_DISK" ] && [ "$PREV_DISK" != "$CURRENT_DISK" ]; then
|
||||||
|
usage
|
||||||
|
error_msg "Directories are not on the same disk."
|
||||||
|
fi
|
||||||
|
PREV_DISK=$CURRENT_DISK
|
||||||
|
done
|
||||||
|
|
||||||
|
# Convert GiB to KiB (as df outputs in KiB)
|
||||||
|
MIN_FREE_SPACE_KiB=$((MIN_FREE_SPACE * 1024 * 1024))
|
||||||
|
|
||||||
|
# Get the free space on the disk where the directories are located
|
||||||
|
FREE_SPACE_KiB=$(df "${DIRS[0]}" | tail -n 1 | awk '{print $4}')
|
||||||
|
|
||||||
|
# Find files (not directories) in all specified directories, sort them by modification time (oldest first)
|
||||||
|
if [ $FREE_SPACE_KiB -lt $MIN_FREE_SPACE_KiB ]; then
|
||||||
|
debug_msg "Less than $MIN_FREE_SPACE GiB free, cleaning up..."
|
||||||
|
find "${DIRS[@]}" -type f -printf '%T+ %p\n' | sort | while IFS= read -r line; do
|
||||||
|
if [ $(df "${DIRS[0]}" | tail -n 1 | awk '{print $4}') -ge $MIN_FREE_SPACE_KiB ]; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
FILE=$(echo "$line" | cut -d ' ' -f2-)
|
||||||
|
debug_msg "Deleting $FILE"
|
||||||
|
rm -f -- "$FILE"
|
||||||
|
REMAINING_SPACE_KiB=$(df "${DIRS[0]}" | tail -n 1 | awk '{print $4}')
|
||||||
|
debug_msg "Remaining space: $((REMAINING_SPACE_KiB / 1024 / 1024)) GiB"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
debug_msg "Enough free space available."
|
||||||
|
fi
|
Loading…
x
Reference in New Issue
Block a user