cleanup-files.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. set -euo pipefail
  3. DIRECTORY=$1
  4. FORCE=${2-false}
  5. JUST_DO_IT=${3-x}
  6. I_MEAN_IT=${4-x}
  7. LIMIT=10000000000 # 10GB
  8. ACTION=print #delete
  9. #LIMIT=13000000 # 13MB
  10. #ACTION=print
  11. MB1=1000000
  12. estimate(){
  13. find "$1" -type f -"$2" +"$3" ${4-} -print0 | du -cb --files0-from=- | grep total | cut -d' ' -f1
  14. }
  15. cleanup(){
  16. filter="$1"
  17. unit="${2-}"
  18. tmin="${3-}"
  19. echo "Deleting files greater than ${WEIGHT}${unit} ($filter)$(if [ -n "$tmin" ]; then echo " where $tmin"; fi) ..."
  20. find "$DIRECTORY" -type f -"$filter" +"${WEIGHT}${unit}" ${tmin} -"$ACTION"
  21. }
  22. search(){
  23. upper="$3"
  24. lower="$2"
  25. filter="$1"
  26. unit="${4-}"
  27. tmin="${5-}"
  28. if [ "$unit" = "c" ]; then
  29. res=500000
  30. elif [ "$filter" = "mmin" ]; then
  31. res=3
  32. else
  33. res=0
  34. fi
  35. WEIGHT="$lower"
  36. while (( upper - lower > res)); do
  37. echo "Finding files greater than ${WEIGHT}${unit} ($filter)$(if [ -n "$tmin" ]; then echo " where $tmin"; fi) ..."
  38. RECLAIM=$(estimate "$DIRECTORY" "$filter" "${WEIGHT}${unit}" "${tmin}")
  39. if (( SIZE - RECLAIM > LIMIT ));
  40. then
  41. upper="$WEIGHT"
  42. WEIGHT=$(((WEIGHT + lower)/2))
  43. if (( WEIGHT == upper )); then break; fi
  44. if (( WEIGHT == lower )) && [ "$lower" != "$2" ]; then break; fi
  45. else
  46. lower="$WEIGHT"
  47. WEIGHT=$(((upper + WEIGHT)/2))
  48. if (( WEIGHT == lower )) || (( WEIGHT == upper )); then break; fi
  49. fi
  50. done
  51. }
  52. summary() {
  53. SIZE=$(find "$DIRECTORY" -type f -print0 | du -cb --files0-from=- | grep total | cut -d' ' -f1)
  54. echo "Current size is : $((SIZE/MB1)) MB"
  55. }
  56. RECLAIM=0
  57. declare WEIGHT SIZE
  58. summary
  59. echo "Size limit is : $((LIMIT/MB1)) MB"
  60. # nothing to do
  61. if (( SIZE < LIMIT )); then exit 0; fi
  62. trap 'summary' EXIT
  63. echo "Need to reclaim : $(( (SIZE - LIMIT)/MB1 )) MB"
  64. search mtime 30 365
  65. if (( WEIGHT > 30 )); then cleanup mtime && exit 0; fi
  66. echo "Looking for big fat blobs older than 7 days and bigger than $((LIMIT/10/MB1))M ..."
  67. search size $((LIMIT/10/MB1)) $((LIMIT/MB1)) M "-mtime +7"
  68. if (( WEIGHT > LIMIT/10/MB1 )); then cleanup size M "-mtime +7" && exit 0; fi
  69. # avoid deleting young files
  70. if ! "$FORCE"; then echo "Refusing to cleanup files smaller than $((LIMIT/10/MB1))M and younger than 30 days"; exit 1; fi
  71. search mtime 7 30
  72. if (( WEIGHT > 7 )); then cleanup mtime && exit 0; fi
  73. echo "Looking for big blobs older than 1 day and bigger than $((LIMIT/100/MB1))M ..."
  74. search size $((LIMIT/100/MB1)) $((LIMIT/MB1)) M "-mtime +1"
  75. if (( WEIGHT > LIMIT/100/MB1 )); then cleanup size M "-mtime +1" && exit 0; fi
  76. # avoid deleting very young files
  77. 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
  78. search mtime 0 7
  79. if (( WEIGHT > 0 )); then cleanup mtime && exit 0; fi
  80. search mmin $((60*24)) $((60*24*2))
  81. if (( WEIGHT > 60*24 )); then cleanup mmin && exit 0; fi
  82. echo "Looking for blobs older than 12 hours and bigger than $((LIMIT/1000/MB1))M ..."
  83. search size $((LIMIT/1000/MB1)) $((LIMIT/MB1)) M "-mmin +$((60*12))"
  84. if (( WEIGHT > LIMIT/1000/MB1 )); then cleanup size M "-mmin +$((60*12))" && exit 0; fi
  85. # avoid deleting ultra young files
  86. 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
  87. search mmin 1 $((60*24))
  88. cleanup mmin