summaryrefslogtreecommitdiff
path: root/tools/ipcam_cleanup.sh
blob: 47bdc594057be81bcf42599eb85546268653dcad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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