-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_datasources.py
55 lines (49 loc) · 1.72 KB
/
backup_datasources.py
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
import subprocess
import json
token = "<your-grafana-token>"
host = "<ip-address>:<port>"
# Execute the initial curl command to retrieve the list of datasources
list_command = [
"curl",
"-s",
"-H", f"Authorization: Bearer {token}",
"-X", "GET",
f"http://{host}/api/datasources"
]
try:
list_result = subprocess.run(
list_command, capture_output=True, text=True, check=True)
datasources = json.loads(list_result.stdout)
except subprocess.CalledProcessError as e:
print(f"Initial request failed with error: {e}")
print("Error output:")
print(e.stderr)
exit(1)
except json.JSONDecodeError as e:
print(f"Error decoding JSON response: {e}")
exit(1)
# Iterate over each datasource ID and execute the detailed curl command
for datasource in datasources:
datasource_id = datasource.get("id")
if datasource_id:
detail_command = [
"curl",
"-s",
"-H", f"Authorization: Bearer {token}",
"-X", "GET",
f"http://{host}/api/datasources/{datasource_id}"
]
try:
detail_result = subprocess.run(
detail_command, capture_output=True, text=True, check=True)
detail_json = json.loads(detail_result.stdout)
filename = f"{datasource_id}.json"
with open(filename, "w") as f:
json.dump(detail_json, f, indent=4)
print(
f"Details for datasource with ID {datasource_id} saved as {filename}")
except subprocess.CalledProcessError as e:
print(
f"Detail request failed for datasource with ID {datasource_id} with error: {e}")
print("Error output:")
print(e.stderr)