Parcourir la source

support cleaning up large files also

Pi il y a 1 an
Parent
commit
3afb487d69
1 fichiers modifiés avec 45 ajouts et 18 suppressions
  1. 45 18
      cleanup-files.sh

+ 45 - 18
cleanup-files.sh

@@ -1,6 +1,9 @@
 #!/bin/bash
 set -euo pipefail
 DIRECTORY=$1
+FORCE=${2-false}
+JUST_DO_IT=${3-x}
+I_MEAN_IT=${4-x}
 LIMIT=10000000000 # 10GB
 ACTION=print #delete
 #LIMIT=13000000 # 13MB
@@ -11,51 +14,75 @@ estimate(){
 }
 
 cleanup(){
-	time="$1"
-	echo "Reclaiming $((RECLAIM/1000000)) MB of data older than $AGE ($time)..."
-	find "$DIRECTORY" -type f -"$time" +"$AGE" -"$ACTION"
+	filter="$1"
+	unit="${2-}"
+	echo "Reclaiming $((RECLAIM/1000000)) MB of data greater than $WEIGHT ($filter)..."
+	find "$DIRECTORY" -type f -"$filter" +"${WEIGHT}${unit}" -"$ACTION"
 }
 
 search(){
 	upper="$3"
 	lower="$2"
-	time="$1"
-	AGE="$upper"
+	filter="$1"
+	unit="${4-}"
+	WEIGHT="$upper"
 	while (( upper - lower > 0)); do
-		echo "Finding files older than $AGE ($time)..."
-		estimate "$DIRECTORY" "$time" "$AGE"
+		echo "Finding files greater than $WEIGHT ($filter)..."
+		estimate "$DIRECTORY" "$filter" "${WEIGHT}${unit}"
 		echo "Could reclaim $((RECLAIM/1000000)) MB..."
 		if (( SIZE - RECLAIM > LIMIT ));
 	       	then
-			upper="$AGE"
-			AGE=$(((AGE + lower)/2))
-			if (( AGE == upper )); then break; fi
+			upper="$WEIGHT"
+			WEIGHT=$(((WEIGHT + lower)/2))
+			if (( WEIGHT == upper )); then break; fi
 		else
-			lower="$AGE"
-			AGE=$(((upper + AGE)/2))
-			if (( AGE == lower )); then break; fi
+			lower="$WEIGHT"
+			WEIGHT=$(((upper + WEIGHT)/2))
+			if (( WEIGHT == lower )); then break; fi
 		fi
-		sleep 0.5
 	done
 }
 
 SIZE=$(find "$DIRECTORY" -type f -print0 | du -cb --files0-from=- | grep total | cut -d'	' -f1)
 RECLAIM=0
-declare AGE
+declare WEIGHT
 echo "Size limit is   : $((LIMIT/1000000)) MB"
 echo "Current size is : $((SIZE/1000000)) MB"
 
+# nothing to do
 if (( SIZE < LIMIT )); then exit 0; fi
 
 echo "Need to reclaim : $(( (SIZE - LIMIT)/1000000 )) MB"
 
 search mtime 30 365
+if (( WEIGHT > 30 )); then cleanup mtime && exit 0; fi
 
-if (( AGE > 30 )); then cleanup mtime && exit 0; fi
+echo "Looking for big fat blobs (any age) bigger than 10% of capacity..."
+search size $((LIMIT/10)) "$LIMIT" c
+if (( WEIGHT > LIMIT/10 )); then cleanup size c && exit 0; fi
 
-search mtime 1 30
+# avoid deleting young files
+if ! "$FORCE"; then echo "Refusing to cleanup (small) files younger than $WEIGHT days";  exit 1; fi
 
-if (( AGE > 1 )); then cleanup mtime && exit 0; fi
+search mtime 7 30
+if (( WEIGHT > 7 )); then cleanup mtime && exit 0; fi
+
+echo "Looking for big blobs (any age) bigger than 1% of capacity..."
+search size $((LIMIT/100)) $((LIMIT/10)) c
+if (( WEIGHT > LIMIT/100 )); then cleanup size c && exit 0; fi
+
+# avoid deleting very young files
+if [ "$JUST_DO_IT" != "JUST_DO_IT" ]; then echo "Refusing to cleanup (small) files younger than 1 week";  exit 1; fi
+
+search mtime 1 7
+if (( WEIGHT > 1 )); then cleanup mtime && exit 0; fi
+
+echo "Looking for blobs (any age) bigger than 0.1% of capacity..."
+search size $((LIMIT/1000)) $((LIMIT/100)) c
+if (( WEIGHT > LIMIT/1000 )); then cleanup size c && exit 0; fi
+
+# avoid deleting ultra young files
+if [ "$I_MEAN_IT" != "I_MEAN_IT" ]; then echo "Refusing to cleanup (small) files younger than 1 day";  exit 1; fi
 
 search cmin 1 $((60*24))