unbound-cache.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. # Installation base dir
  3. CONF="/opt/unbound"
  4. FNAME="unbound.cache"
  5. usage ()
  6. {
  7. # shellcheck disable=SC2086
  8. echo "Usage: $(basename $0) [-s | -l | -r | -h] [filename]"
  9. echo ""
  10. echo "l - Load: Load Unbound DNS cache from file"
  11. echo "s - Save: Save Unbound DNS cache contents to plain file with domain names"
  12. echo "r - Reload: Saves the Unbound DNS cache, reloads the server, then loads the cache"
  13. echo "h - This screen"
  14. echo "filename - File to save/load dumped cache. If not specified, ${CONF}/${FNAME} will be used instead."
  15. echo "Note: Loads cache if no arguments are specified."
  16. echo " Also, unbound-control must be configured."
  17. exit 0
  18. }
  19. root_check ()
  20. {
  21. # shellcheck disable=SC2046
  22. if [ ! $(id | cut -f2 -d"(" | cut -f1 -d")") = "root" ] && [ ! $(id | cut -f2 -d"(" | cut -f1 -d")") = "unbound" ]; then
  23. echo "ERROR: You must be root (or unbound) to run this script"
  24. exit 1
  25. fi
  26. }
  27. check_saved_file ()
  28. {
  29. filename=$1
  30. if [ -n "$filename" ] && [ ! -f "$filename" ]; then
  31. echo ""
  32. echo "ERROR: File $filename does not exist. Save it first."
  33. exit 3
  34. elif [ ! -f "${CONF}/${FNAME}" ]; then
  35. echo ""
  36. echo "ERROR: File ${CONF}/${FNAME} does not exist. Save it first."
  37. exit 4
  38. fi
  39. }
  40. save_cache ()
  41. {
  42. # Save unbound cache
  43. filename=$1
  44. if [ -z "$filename" ]; then
  45. echo "Saving cache to ${CONF}/${FNAME}"
  46. unbound-control dump_cache > ${CONF}/${FNAME}
  47. else
  48. echo "Saving cache to $filename"
  49. unbound-control dump_cache > "$filename"
  50. fi
  51. echo "ok"
  52. }
  53. load_cache ()
  54. {
  55. # Load saved cache contents and warmup cache
  56. filename=$1
  57. if [ -z "$filename" ]; then
  58. echo "Loading cache from ${CONF}/${FNAME}"
  59. check_saved_file "$filename"
  60. unbound-control load_cache < "${CONF}/${FNAME}"
  61. else
  62. echo "Loading cache from $filename"
  63. check_saved_file "$filename"
  64. unbound-control load_cache < "$filename"
  65. fi
  66. }
  67. reload_cache ()
  68. {
  69. # Reload and refresh existing cache and saved dump
  70. filename=$1
  71. save_cache "$filename"
  72. echo "Reloading unbound server"
  73. unbound-control reload
  74. load_cache "$filename"
  75. }
  76. # Root check
  77. root_check
  78. # Check command-line arguments
  79. arg_list=$*
  80. if [ "$*" = "" ]; then
  81. # Load cache if there are no arguments
  82. load_cache
  83. else
  84. # Parse command line
  85. # shellcheck disable=SC2046
  86. set -- $(getopt --options sSlLrRhH --longoptions=save,load,reload,help -- "$arg_list") || exit 5
  87. # Read arguments
  88. for i in $(getopt --options :sSlLrRhH --longoptions=save,load,reload,help -- "$arg_list")
  89. do
  90. case $i in
  91. -s | -S | --save) choice="save";;
  92. -l | -L | --load) choice="load";;
  93. -r | -R | --reload) choice="reload";;
  94. -h | -H | --help | \?) usage;;
  95. -- ) ;;
  96. *)
  97. if [ "$choice" = "" ]; then
  98. echo "ERROR: An argument must be preceded by a flag. See -h."
  99. exit 8
  100. else
  101. file=$(echo "$i" | xargs)
  102. break
  103. fi;;
  104. esac
  105. done
  106. fi
  107. if [ "$choice" = "save" ]; then
  108. save_cache "$file"
  109. elif [ "$choice" = "load" ]; then
  110. load_cache "$file"
  111. elif [ "$choice" = "reload" ]; then
  112. reload_cache "$file"
  113. fi
  114. exit 0