forked from grafana-tools/grafana-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-backup.go
217 lines (193 loc) · 5.5 KB
/
do-backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Backup tool for Grafana.
// Copyright (C) 2016-2017 Alexander I.Grafov <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ॐ तारे तुत्तारे तुरे स्व
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/gosimple/slug"
"github.com/grafana-tools/sdk"
)
func doBackup(opts ...option) {
var (
cmd = initCommand(opts...)
)
if cmd.applyHierarchically {
backupDashboards(cmd)
return
}
if cmd.applyForBoards {
backupDashboards(cmd)
}
if cmd.applyForDs {
backupDatasources(cmd, nil)
}
if cmd.applyForUsers {
backupUsers(cmd)
}
}
func backupDashboards(cmd *command) {
var (
boardLinks []sdk.FoundBoard
rawBoard []byte
meta sdk.BoardProperties
board sdk.Board
datasources = make(map[string]bool)
err error
)
ctx := context.Background()
if boardLinks, err = cmd.grafana.SearchDashboards(ctx, cmd.boardTitle, cmd.starred, cmd.tags...); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err))
os.Exit(1)
}
if cmd.verbose {
fmt.Printf("Found %d dashboards that matched the conditions.\n", len(boardLinks))
}
// TODO: If this directory already exists prompt to overwrite (unless --force)
VerifyOrCreateDir(*flagDir)
for _, link := range boardLinks {
select {
case <-cancel:
exitBySignal()
default:
if rawBoard, meta, err = cmd.grafana.GetRawDashboardBySlug(ctx,link.URI); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s for %s\n", err, link.URI))
continue
}
if cmd.applyHierarchically {
if err = json.Unmarshal(rawBoard, &board); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("error %s parsing %s\n", err, meta.Slug))
} else {
extractDatasources(cmd, datasources, board)
}
}
var fname = fmt.Sprintf(path.Join(*flagDir, "%s.db.json"), meta.Slug)
if err = ioutil.WriteFile(fname, rawBoard, os.FileMode(int(0666))); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s for %s\n", err, meta.Slug))
continue
}
if cmd.verbose {
fmt.Printf("%s writen into %s.\n", meta.Slug, fname)
}
}
}
if cmd.applyHierarchically {
backupDatasources(cmd, datasources)
}
}
func backupUsers(cmd *command) {
var (
allUsers []sdk.User
rawUser []byte
err error
)
ctx := context.Background()
if allUsers, err = cmd.grafana.GetAllUsers(ctx); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err))
return
}
VerifyOrCreateDir(*flagDir)
for _, user := range allUsers {
select {
case <-cancel:
exitBySignal()
default:
rawUser, _ = json.Marshal(user)
var fname = fmt.Sprintf(path.Join(*flagDir, "%s.user.%d.json"), slug.Make(user.Login), user.OrgID)
if err = ioutil.WriteFile(fname, rawUser, os.FileMode(int(0666))); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("error %s on writing %s\n", err, fname))
continue
}
if cmd.verbose {
fmt.Printf("%s written into %s\n", user.Name, fname)
}
}
}
}
func backupDatasources(cmd *command, datasources map[string]bool) {
var (
allDatasources []sdk.Datasource
rawDs []byte
err error
)
ctx := context.Background()
if allDatasources, err = cmd.grafana.GetAllDatasources(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
if cmd.verbose {
fmt.Printf("Found %d datasources.\n", len(allDatasources))
}
VerifyOrCreateDir(*flagDir)
for _, ds := range allDatasources {
select {
case <-cancel:
exitBySignal()
default:
if datasources != nil {
if _, ok := datasources[ds.Name]; !ok {
continue
}
}
if rawDs, err = json.Marshal(ds); err != nil {
fmt.Fprintf(os.Stderr, "datasource marshal error %s\n", err)
continue
}
var fname = fmt.Sprintf(path.Join(*flagDir, "%s.ds.%d.json"), slug.Make(ds.Name), ds.OrgID)
if err = ioutil.WriteFile(fname, rawDs, os.FileMode(int(0666))); err != nil {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s for %s\n", err, ds.Name))
continue
}
if cmd.verbose {
fmt.Printf("%s written into %s", ds.Name, fname)
}
}
}
}
func extractDatasources(cmd *command, datasources map[string]bool, board sdk.Board) {
for _, row := range board.Rows {
for _, panel := range row.Panels {
if panel.Datasource != nil {
datasources[*panel.Datasource] = true
if cmd.verbose {
fmt.Printf("Found Datasource [%s] in dashboard [%s]: Adding to backup list.\n", slug.Make(*panel.Datasource), board.Title)
}
}
}
}
}
// Checks to see if a directory exists. If not creates it along with any parent directories. Returns an error if the
// file exists but is not a directory or if it is unable to create the directory.
func VerifyOrCreateDir(directory string) (error) {
stat, err := os.Stat(directory)
if err == nil {
if !stat.IsDir() {
return errors.New("Specified path is not a directory!")
}
return nil
}
if os.IsNotExist(err) {
err = os.MkdirAll(directory, 0755)
return err
}
return err
}