autoDeleterBelowSpace.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. #
  3. # prune_dir - prune directory by deleting files if we are low on space
  4. #
  5. DIR=/media/usb/ftp/data/domsod/FI9900P_00626E69B672/record
  6. CAPACITY_LIMIT=95
  7. if [ "$DIR" == "" ]
  8. then
  9. echo "ERROR: directory not specified"
  10. exit 1
  11. fi
  12. if ! cd $DIR
  13. then
  14. echo "ERROR: unable to chdir to directory '$DIR'"
  15. exit 2
  16. fi
  17. if [ "$CAPACITY_LIMIT" == "" ]
  18. then
  19. CAPACITY_LIMIT=95 # default limit
  20. fi
  21. CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  22. if [ $CAPACITY -gt $CAPACITY_LIMIT ]
  23. then
  24. #
  25. # Get list of files, oldest first.
  26. # Delete the oldest files until
  27. # we are below the limit. Just
  28. # delete regular files, ignore directories.
  29. #
  30. ls -rt $DIR | while read FILE
  31. do
  32. FILE="${DIR}/${FILE}"
  33. if [ -f $FILE ]
  34. then
  35. if rm -f $FILE
  36. then
  37. now=$(date)
  38. echo "${now}: Deleted $FILE" >> /var/log/ftpDeleter.log
  39. CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')
  40. if [ $CAPACITY -le $CAPACITY_LIMIT ]
  41. then
  42. # we're below the limit, so stop deleting
  43. now=$(date)
  44. echo "${now}: Below limit, stopped deleting. Capacity: ${CAPACITY}" >> /var/log/ftpDeleter.log
  45. exit
  46. fi
  47. fi
  48. fi
  49. done
  50. else
  51. now=$(date)
  52. echo "${now}: Don't need to delete anything, capacity: ${CAPACITY}"
  53. fi