check_dir_size.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. # v.0.0.0.1
  3. # 21.4.2024
  4. # scripts check directory size and if exceeds maximal size, notify via email to recipient
  5. # syntax:
  6. # check_dir_size <path_to_dir> <maximum_size> <recipient> [<custom_subject>]
  7. # <path_to_dir> - path to check direcory. Directory must exists and be acessible
  8. # <maximum_size> - whole number size in bytes
  9. # <recipient> - email address to notify
  10. # <custom_subject> - optional. Custom subject text. If not defined uses default.
  11. . ./qbashlib.sh
  12. default_subject="Warning notification - directory exceed maximal size."
  13. default_body="Directory '$1' exceed maximal size $2 bytes"
  14. body=$default_body
  15. subject=$default_subject
  16. # Zkontroluj, zda je zadán první argument
  17. argument_exists "$1" "<path_to_dir>"
  18. # Zkontroluj, zda existuje adresář s instalací Wordpressu
  19. if [ ! -d "$1" ]; then
  20. echo "<path_to_dir> directory '$1' not found or not accssible."
  21. exit 1
  22. fi
  23. # Zkontroluj, zda je zadán druhý argument
  24. argument_exists "$2" "<maximum_size>"
  25. # Zkontroluj, zda je zadán druhý argument jako číslo
  26. is_number $2
  27. if [ $? -eq 0 ]; then
  28. echo "<maximum_size> argument must whole number value."
  29. exit 1
  30. fi
  31. # Zkontroluj, zda je zadán třetí argument
  32. is_email $3
  33. if [ $? -eq 0 ]; then
  34. echo "<recipient> argument '$3' is not valid email address."
  35. exit 1
  36. fi
  37. # Zkontroluj, zda je zadán třetí argument
  38. if [ ! -z "$4" ]; then
  39. subject=$4
  40. fi
  41. # porovnání velikosti adresáře proti maximální hodnotě
  42. dir_size=$(dir_get_size $1)
  43. if [ $dir_size -gt $2 ]; then
  44. # velikost přesáhls maximum -> notifikuj
  45. mail_send "$3" "$subject" "$body"
  46. fi