nlbwmon-action 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/sh
  2. case "$1" in
  3. backup)
  4. dbdir=$(uci -q get nlbwmon.@nlbwmon[0].database_directory)
  5. if [ ! -d "${dbdir:-/var/lib/nlbwmon}" ]; then
  6. echo "Unable to locate database directory" >&2
  7. exit 1
  8. fi
  9. exec /bin/tar -C "${dbdir:-/var/lib/nlbwmon}" -c -z . -f -
  10. ;;
  11. commit)
  12. exec /usr/sbin/nlbw -c commit
  13. ;;
  14. download)
  15. shift
  16. type=json
  17. delim=,
  18. period=
  19. group_by=
  20. order_by=
  21. while [ -n "$1" ]; do
  22. case "$1" in
  23. -f)
  24. case "$2" in
  25. csv|json) type=$2 ;;
  26. *) echo "Invalid data format" >&2; exit 1 ;;
  27. esac
  28. shift
  29. ;;
  30. -s)
  31. case "$2" in
  32. ?) delim=$2 ;;
  33. *) echo "Invalid delimitter" >&2; exit 1 ;;
  34. esac
  35. shift
  36. ;;
  37. -t)
  38. case "$2" in
  39. [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) period=$2 ;;
  40. *) echo "Invalid period" >&2; exit 1 ;;
  41. esac
  42. shift
  43. ;;
  44. -g|-o)
  45. case "$1:$2" in
  46. -g:?*) group_by=$2 ;;
  47. -o:?*) order_by=$2 ;;
  48. *) echo "Argument required for $1" >&2; exit 1 ;;
  49. esac
  50. shift
  51. ;;
  52. *)
  53. echo "Unknown option $1" >&2
  54. exit 1
  55. ;;
  56. esac
  57. shift
  58. done
  59. exec /usr/sbin/nlbw -c $type -s$delim \
  60. ${period:+-t $period} \
  61. ${group_by:+-g "$group_by"} \
  62. ${order_by:+-o "$order_by"}
  63. ;;
  64. periods)
  65. for date in $(/usr/sbin/nlbw -c list); do
  66. case "$date" in
  67. [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])
  68. res="${res:+$res, }\"$date\""
  69. ;;
  70. esac
  71. done
  72. printf '{ "periods": [ %s ] }' "$res"
  73. ;;
  74. restore)
  75. if [ ! -f /tmp/nlbw-restore.tar.gz ]; then
  76. echo "Unable to locate archive to restore" >&2
  77. exit 1
  78. fi
  79. dbdir=$(uci -q get nlbwmon.@nlbwmon[0].database_directory)
  80. files=$(/bin/tar -tzf /tmp/nlbw-restore.tar.gz | grep -E '^(\./)?[0-9]{8}\.db(\.gz)?$' | tr '\n' ' ')
  81. if [ -z "$files" ]; then
  82. echo "Invalid or empty backup archive" >&2
  83. exit 1
  84. fi
  85. /etc/init.d/nlbwmon stop
  86. /bin/mkdir -p "${dbdir:-/var/lib/nlbwmon}"
  87. for file in $(/bin/tar -C "${dbdir:-/var/lib/nlbwmon}" -vxzf /tmp/nlbw-restore.tar.gz $files); do
  88. res="${res:+$res, }\"${file#./}\""
  89. done
  90. /bin/rm -f /tmp/nlbw-restore.tar.gz
  91. /etc/init.d/nlbwmon start
  92. printf '{ "restored": [ %s ] }' "$res"
  93. ;;
  94. *)
  95. echo "Usage: $0 {commit|download|periods|restore}" >&2
  96. exit 1
  97. ;;
  98. esac