Skip to content

Commit 5025582

Browse files
author
r.khavronenko
committed
init
1 parent 1fc3dda commit 5025582

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Created by .ignore support plugin (hsz.mobi)

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
# grafana-import-export
2-
shell scripts for importing and exporting Grafana's dashboards
2+
3+
Simple scripts to export from, and import dashboards to [Grafana](http://grafana.org/)
4+
Example was taken from https://gist.github.com/crisidev/bd52bdcc7f029be2f295
5+
6+
## Dependencies
7+
**[JQ](https://stedolan.github.io/jq/)** - to process .json
8+
9+
## dashboard-exporter
10+
To make it work, you need to replace **KEY**, **HOST** and **FILE_DIR** variables with your own
11+
12+
Do not forget to set permissions before run
13+
```
14+
chmod 755 dashboard-exporter.sh
15+
```
16+
17+
Then run:
18+
```
19+
./dashboard-exporter.sh
20+
```
21+
22+
Expected output:
23+
```
24+
./dashboard-exporter.sh
25+
% Total % Received % Xferd Average Speed Time Time Time Current
26+
Dload Upload Total Spent Left Speed
27+
100 21102 0 21102 0 0 53000 0 --:--:-- --:--:-- --:--:-- 53020
28+
29+
```
30+
31+
Look for exported .json dashboards at **FILE_DIR** path
32+
33+
## dashboard-importer
34+
To make it work, you need to replace **KEY**, **HOST** and **FILE_DIR** variables with your own
35+
36+
Do not forget to set permissions before run
37+
```
38+
chmod 755 dashboard-importer.sh
39+
```
40+
41+
To import all .json files from **FILE_DIR** to your Grafana:
42+
```
43+
./dashboard-importer.sh
44+
```
45+
46+
To import only some of them:
47+
```
48+
./dashboard-importer.sh dashboard1.json dashboard2.json
49+
```

dashboard-exporter.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
KEY=xxxxxxxxxx
3+
HOST="http://your.grafana.host.com"
4+
FILE_DIR=path/to/dashboards
5+
6+
if [ ! -d "$FILE_DIR" ] ; then
7+
mkdir -p "$FILE_DIR"
8+
fi
9+
10+
for dash in $(curl -sSL -k -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq '.' |grep -i uri|awk -F '"uri": "' '{ print $2 }'|awk -F '"' '{print $1 }'); do
11+
DB=$(echo ${dash}|sed 's,db/,,g').json
12+
curl -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/dashboards/${dash}" | jq '.dashboard.id = null' > "$FILE_DIR/$DB"
13+
done

dashboard-importer.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
KEY=xxxxxxxxxx
3+
HOST="http://your.grafana.host.com"
4+
FILE_DIR=path/to/dashboards
5+
6+
import_dashboard(){
7+
printf "Processing $1 file...\n"
8+
curl -XPOST "${HOST}/api/dashboards/db" --data-binary @./$1 \
9+
-H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer ${KEY}"
10+
printf "\n"
11+
}
12+
13+
if [[ -n "$1" ]]
14+
then
15+
for file in "$@"; do
16+
file="$FILE_DIR/$file"
17+
if [ -f "$file" ]
18+
then
19+
import_dashboard "$file"
20+
else
21+
echo "$file not found."
22+
fi
23+
done
24+
else
25+
echo "Importing all"
26+
for file in $FILE_DIR/*.json; do
27+
import_dashboard "$file"
28+
done
29+
fi

0 commit comments

Comments
 (0)