You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008-2011 OpenWrt.org
  3. START=99
  4. vnstat_option() {
  5. sed -ne "s/^[[:space:]]*$1[[:space:]]*['\"]\([^'\"]*\)['\"].*/\1/p" \
  6. /etc/vnstat.conf
  7. }
  8. start() {
  9. local lib="$(vnstat_option DatabaseDir)"
  10. local pid="$(vnstat_option PidFile)"
  11. [ -n "$lib" ] || {
  12. echo "Error: No DatabaseDir set in vnstat.conf" >&2
  13. exit 1
  14. }
  15. [ -n "$pid" ] || {
  16. echo "Error: No PidFile set in vnstat.conf" >&2
  17. exit 1
  18. }
  19. mkdir -p "$lib"
  20. init_ifaces() {
  21. local cfg="$1"
  22. local url lnk
  23. init_iface() {
  24. local ifn="$1"
  25. if [ -n "$url" ]; then
  26. local try=0
  27. local max=3
  28. local hostname="$(cat /proc/sys/kernel/hostname)"
  29. while [ $((++try)) -le $max ]; do
  30. if wget -q -O "$lib/$ifn" "$url/${hostname}_$ifn" 2>/dev/null && [ -e "$lib/$ifn" ]; then
  31. logger -t "vnstat" "Downloaded backup for database $ifn"
  32. break
  33. else
  34. logger -t "vnstat" "Download try $try/$max for database $ifn failed"
  35. sleep 30
  36. fi
  37. done
  38. elif [ -n "$backup_dir" ]; then
  39. if cp -f "$backup_dir/$ifn" "$lib/" &>/dev/null; then
  40. logger -t "vnstat" "Restored backup for database $ifn"
  41. else
  42. logger -t "vnstat" "Restore of backup for database $ifn failed"
  43. fi
  44. fi
  45. /usr/bin/vnstat -u -i "$ifn" >/dev/null
  46. [ -n "$lnk" ] && {
  47. mkdir -p "$lnk"
  48. [ -L "$lnk/$ifn" ] || ln -s "$lib/$ifn" "$lnk/$ifn"
  49. }
  50. }
  51. config_get url "$cfg" remote
  52. config_get lnk "$cfg" symlink
  53. config_get backup_dir "$cfg" backup_dir
  54. config_list_foreach "$cfg" interface init_iface
  55. return 1
  56. }
  57. config_load vnstat
  58. config_foreach init_ifaces vnstat
  59. SERVICE_PID_FILE="${pid}"
  60. service_start /usr/sbin/vnstatd -d
  61. }
  62. stop() {
  63. local pid="$(vnstat_option PidFile)"
  64. [ -n "$pid" ] || {
  65. echo "Error: No PidFile set in vnstat.conf" >&2
  66. exit 1
  67. }
  68. SERVICE_PID_FILE="${pid}"
  69. service_stop /usr/sbin/vnstatd
  70. }