diff options
author | Evgeny Sorokin <me@ch1p.io> | 2024-01-19 17:35:44 +0300 |
---|---|---|
committer | Evgeny Sorokin <me@ch1p.io> | 2024-01-19 17:35:44 +0300 |
commit | 8d4045f6c3a36517f70ddd522065365b414e9a02 (patch) | |
tree | b87c30b6662fc2195fb14dcea00957f39a76df8b | |
parent | 72a45b852105a9e62d700dffba62f436c4e0ca55 (diff) |
tools: add ipcam_cleanup.sh script
-rw-r--r-- | tools/ipcam_cleanup.sh | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tools/ipcam_cleanup.sh b/tools/ipcam_cleanup.sh new file mode 100644 index 0000000..47bdc59 --- /dev/null +++ b/tools/ipcam_cleanup.sh @@ -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 |