瀏覽代碼

reduce number of iterations

Pi 1 年之前
父節點
當前提交
1568c65d63
共有 1 個文件被更改,包括 48 次插入28 次删除
  1. 48 28
      cleanup-files.sh

+ 48 - 28
cleanup-files.sh

@@ -8,16 +8,18 @@ LIMIT=10000000000 # 10GB
 ACTION=print #delete
 #LIMIT=13000000 # 13MB
 #ACTION=print
+MB1=1000000
 
 estimate(){
-	RECLAIM=$(find "$1" -type f -"$2" +"$3" -print0 | du -cb --files0-from=- | grep total | cut -d'	' -f1) || RECLAIM=0
+	find "$1" -type f -"$2" +"$3" ${4-} -print0 | du -cb --files0-from=- | grep total | cut -d'	' -f1
 }
 
 cleanup(){
 	filter="$1"
 	unit="${2-}"
-	echo "Reclaiming $((RECLAIM/1000000)) MB of data greater than $WEIGHT ($filter)..."
-	find "$DIRECTORY" -type f -"$filter" +"${WEIGHT}${unit}" -"$ACTION"
+	tmin="${3-}"
+	echo "Deleting files greater than ${WEIGHT}${unit} ($filter)$(if [ -n "$tmin" ]; then echo " where $tmin"; fi) ..."
+	find "$DIRECTORY" -type f -"$filter" +"${WEIGHT}${unit}" ${tmin} -"$ACTION"
 }
 
 search(){
@@ -25,10 +27,18 @@ search(){
 	lower="$2"
 	filter="$1"
 	unit="${4-}"
-	WEIGHT="$upper"
-	while (( upper - lower > 0)); do
-		echo "Finding files greater than $WEIGHT ($filter)..."
-		estimate "$DIRECTORY" "$filter" "${WEIGHT}${unit}"
+	tmin="${5-}"
+	if [ "$unit" = "c" ]; then
+		res=500000
+	elif [ "$filter" = "mmin" ]; then
+		res=3
+	else
+		res=0
+	fi
+	WEIGHT="$lower"
+	while (( upper - lower > res)); do
+		echo "Finding files greater than ${WEIGHT}${unit} ($filter)$(if [ -n "$tmin" ]; then echo " where $tmin"; fi) ..."
+		RECLAIM=$(estimate "$DIRECTORY" "$filter" "${WEIGHT}${unit}" "${tmin}")
 		if (( SIZE - RECLAIM > LIMIT ));
 	       	then
 			upper="$WEIGHT"
@@ -43,47 +53,57 @@ search(){
 	done
 }
 
-SIZE=$(find "$DIRECTORY" -type f -print0 | du -cb --files0-from=- | grep total | cut -d'	' -f1)
+summary() {
+	SIZE=$(find "$DIRECTORY" -type f -print0 | du -cb --files0-from=- | grep total | cut -d'	' -f1)
+	echo "Current size is : $((SIZE/MB1)) MB"
+}
+
 RECLAIM=0
-declare WEIGHT
-echo "Size limit is   : $((LIMIT/1000000)) MB"
-echo "Current size is : $((SIZE/1000000)) MB"
+declare WEIGHT SIZE
+
+summary
+echo "Size limit is   : $((LIMIT/MB1)) MB"
 
 # nothing to do
 if (( SIZE < LIMIT )); then exit 0; fi
 
-echo "Need to reclaim : $(( (SIZE - LIMIT)/1000000 )) MB"
+trap 'summary' EXIT
+
+echo "Need to reclaim : $(( (SIZE - LIMIT)/MB1 )) MB"
 
 search mtime 30 365
 if (( WEIGHT > 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
+echo "Looking for big fat blobs older than 7 days and bigger than $((LIMIT/10/MB1))M ..."
+search size $((LIMIT/10/MB1)) $((LIMIT/MB1)) M "-mtime +7"
+if (( WEIGHT > LIMIT/10/MB1 )); then cleanup size M "-mtime +7" && exit 0; fi
 
 # avoid deleting young files
-if ! "$FORCE"; then echo "Refusing to cleanup (small) files younger than $WEIGHT days";  exit 1; fi
+if ! "$FORCE"; then echo "Refusing to cleanup files smaller than $((LIMIT/10/MB1))M and younger than 30 days";  exit 1; 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
+echo "Looking for big blobs older than 1 day and bigger than $((LIMIT/100/MB1))M ..."
+search size $((LIMIT/100/MB1)) $((LIMIT/MB1)) M "-mtime +1"
+if (( WEIGHT > LIMIT/100/MB1 )); then cleanup size M "-mtime +1" && 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
+if [ "$JUST_DO_IT" != "JUST_DO_IT" ]; then echo "Refusing to cleanup files smaller than $((LIMIT/100/MB1))M and younger than 7 days";  exit 1; fi
+
+search mtime 0 7
+if (( WEIGHT > 0 )); then cleanup mtime && exit 0; fi
 
-search mtime 1 7
-if (( WEIGHT > 1 )); then cleanup mtime && exit 0; fi
+search mmin $((60*24)) $((60*24*2))
+if (( WEIGHT > 60*24 )); then cleanup mmin && 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
+echo "Looking for blobs older than 12 hours and bigger than $((LIMIT/1000/MB1))M ..."
+search size $((LIMIT/1000/MB1)) $((LIMIT/MB1)) M "-mmin +$((60*12))"
+if (( WEIGHT > LIMIT/1000/MB1 )); then cleanup size M "-mmin +$((60*12))" && 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
+if [ "$I_MEAN_IT" != "I_MEAN_IT" ]; then echo "Refusing to cleanup files smaller than $((LIMIT/1000/MB1))M and younger than 1 day";  exit 1; fi
 
-search cmin 1 $((60*24))
+search mmin 1 $((60*24))
 
-cleanup cmin
+cleanup mmin