Advertisement
devinteske

Backup Grafana Dashboards

Oct 13th, 2019
1,570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 2.28 KB | None | 0 0
  1. ############################################################ IDENT(1)
  2. #
  3. # $Title: Script to make backup of Grafana dashboards $
  4. # $Copyright: 2019 Devin Teske. All rights reserved. $
  5. # $FrauBSD$
  6. #
  7. ############################################################ CONFIGURATION
  8.  
  9. GRAFANA_DB= /var/lib/grafana/grafana.db
  10. TABLE=      dashboard
  11. KEYS=       slug, data
  12.  
  13. ############################################################ TARGETS
  14.  
  15. all:
  16.     @sqlite3 $(GRAFANA_DB) 'select $(KEYS) from $(TABLE)' | awk ' \
  17.         function have(name)                                   \
  18.         {                                                     \
  19.             return system(sprintf("type %s > %s 2>&1",    \
  20.                 name, "/dev/null")) == 0;             \
  21.         }                                                     \
  22.         function exists(path)                                 \
  23.         {                                                     \
  24.             return system("[ -e " path " ]") == 0;        \
  25.         }                                                     \
  26.         function cat(file,        line, buf)                  \
  27.         {                                                     \
  28.             if (!exists(file)) return "";                 \
  29.             while ((getline line < file) != 0)            \
  30.                 buf = buf "\n" line;                  \
  31.             close(file);                                  \
  32.             return substr(buf, 2);                        \
  33.         }                                                     \
  34.         BEGIN {                                               \
  35.             jq = have("jq") ? "jq ." : "cat";             \
  36.         }                                                     \
  37.         match($$0, /\|/) {                                    \
  38.             slug = substr($$0, 1, RSTART - 1);            \
  39.             data = substr($$0, RSTART + 1);               \
  40.             prev = cat(file = slug ".json");              \
  41.             jcmd = sprintf("%s > %s", jq, file);          \
  42.             print data | jcmd;                            \
  43.             close(jcmd);                                  \
  44.             data = cat(file);                             \
  45.             if (data != prev) printf "U %s\n", file;      \
  46.         }                                                     \
  47.     ' # END-QUOTE
  48.  
  49. ################################################################################
  50. # END
  51. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement