-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_cleaner.sh
45 lines (33 loc) · 1.37 KB
/
storage_cleaner.sh
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
#!/bin/bash
### USER CONSTANTS ###
######################
threshold=90 # threshold for max disk usage
drive_path='/motioneyes_nvr_ssd' # drive where recordings are stored
# camera names (subdirectories within the target drive)
cameras=(
camera_1
camera_2
camera_3
)
######################
######################
# percentage of disk usage on the target drive
disk_usage=$(df -h | awk -v path="$drive_path" '$0 ~ path {print $5}' | tr -d '%')
# if the disk usage is greater than or equal to the threshold, then do the following
if [ $disk_usage -ge $threshold ]; then
# log record of deletion process
echo "" >> removed.log
echo "On $(date -Iseconds), disk_usage is $disk_usage%. Removing the following dirs:" >> removed.log
# find unique dirs in drive with a YYYY-MM-DD format and then sort them chronologically
oldest_date_dirs=$(find "$drive_path/" -type d -regextype egrep -regex '.*/[0-9]{4}-[0-9]{2}-[0-9]{2}$' | sed 's/.*\///' | sort -u | sort -t - -k1,1n -k2,2n -k3,3n)
# get the oldest directory
oldest_dir=$(echo $oldest_date_dirs | awk '{print $1}')
# loop through each camera subdirectory and delete the target date subdirectory
for camera in "${cameras[@]}"; do
# Delete the directory and its contents and update log
delete_path="$drive_path/$camera/$oldest_dir"
rm -rf $delete_path
echo $delete_path >> removed.log
done
fi
exit 0