This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-grafana.ps1
247 lines (204 loc) · 8.81 KB
/
restore-grafana.ps1
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
param(
[Parameter(Mandatory=$true)] # Full URL of the Grafana instance
[string]$FullUrl = "",
[Parameter(Mandatory=$true)] # Grafana API token
[string]$Token = "",
[Parameter(Mandatory=$true)] # Path to the backup folder
[string]$BackupPath = ""
)
if (Get-Command -ErrorAction Ignore -Type Cmdlet Start-ThreadJob) {
Write-Host "Module 'ThreadJob' is already installed."
}else{
Write-Verbose "Installing module 'ThreadJob' on demand..."
Install-Module -ErrorAction Stop -Scope CurrentUser ThreadJob
}
# Check if required cmdlets are available
$requiredCmdlets = @("Invoke-RestMethod", "ConvertFrom-Json", "Compress-Archive")
$missingCmdlets = $requiredCmdlets | Where-Object { -not (Get-Command -Name $_ -ErrorAction SilentlyContinue) }
if ($missingCmdlets) {
$psVersion = $PSVersionTable.PSVersion.ToString()
Write-Host "The following required cmdlets are not available in your PowerShell version ($psVersion):" -ForegroundColor Red
Write-Host ($missingCmdlets -join ", ") -ForegroundColor Red
Write-Host "Please use PowerShell Core (version 6 or higher) to run this script." -ForegroundColor Red
exit 1
}
# Check if the backup folder exists and contains helm_backup.7z
if (!(Test-Path $BackupPath)) {
Write-Host "The backup folder $BackupPath does not exist."
exit 1
}
$IsEncrypted = $false
# Check if grafana_backup.7z.gpg exists and if so, decrypt it
if (Test-Path "$BackupPath\grafana_backup.7z.gpg") {
$IsEncrypted = $true
Write-Host "Decrypting grafana_backup.7z.gpg..."
gpg --decrypt --output "$BackupPath\grafana_backup.7z" "$BackupPath\grafana_backup.7z.gpg"
if ($LASTEXITCODE -ne 0) {
Write-Host "GPG decryption failed. Aborting the process."
exit 1
}
}
if (!(Test-Path "$BackupPath\grafana_backup.7z")) {
Write-Host "The backup folder $BackupPath does not contain a grafana_backup.7z file."
exit 1
}
# Verify GPG signature
$SignedFile = Join-Path $BackupPath "file_hashes.json"
$SignatureFile = Join-Path $BackupPath "file_hashes.json.sig"
$CheckGPG = $true
if (!(Test-Path $SignedFile) -or !(Test-Path $SignatureFile)) {
Write-Host "The signed file or its signature is missing in the backup folder."
$CheckGPG = $false
Write-Host "Do you want to continue without GPG signature verification? (y/n)"
$answer = Read-Host
if ($answer -ne "y") {
exit 1
}
}
if ($CheckGPG)
{
gpg --verify $SignatureFile $SignedFile
if ($LASTEXITCODE -ne 0)
{
Write-Host "GPG signature verification failed. Aborting the process."
exit 1
}
# Load the JSON file with the file hashes
$FileHashes = (Get-Content -Path $SignedFile | ConvertFrom-Json).Files
function Verify-FileHash($FilePath, $ExpectedHash)
{
$ActualHash = (Get-FileHash -Path $FilePath -Algorithm SHA512).Hash
return $ActualHash -eq $ExpectedHash
}
# Verify the hash of the grafana_backup.7z file
$HelmBackupFile = Join-Path $BackupPath "grafana_backup.7z"
$HelmBackupHash = ($FileHashes | Where-Object { $_.Path -eq "grafana_backup.7z" }).Hash
if (!(Verify-FileHash -FilePath $HelmBackupFile -ExpectedHash $HelmBackupHash))
{
Write-Host "Hash verification failed for grafana_backup.7z. Aborting the process."
exit 1
}
}
$UnpackagedgrafanaPath = ".\grafana"
$SevenZipPath = ".\_tools\7z.exe"
# Decompress the grafana folder
& $SevenZipPath x -y -o"$UnpackagedgrafanaPath" "$BackupPath\grafana_backup.7z" | Out-Null
# Set headers for the API request
$headers = @{
'Authorization' = "Bearer $Token"
'Content-Type' = 'application/json'
}
# Make the API request
$apiUrl = "$FullUrl/api/datasources"
$datasourcesJson = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers
# Extract the 'type' and 'uid' from the JSON response and create a map
$typeUidMap = @{}
foreach ($datasource in $datasourcesJson) {
$typeUidMap[$datasource.type] = $datasource.uid
}
Write-Host "Type-UID map:" -ForegroundColor Yellow
Write-Host ($typeUidMap | ConvertTo-Json)
# Recursively find all JSON files in the 'grafana' folder
$jsonFiles = Get-ChildItem -Path "$UnpackagedgrafanaPath" -Recurse -Filter "*.json"
# Process each JSON file
foreach ($jsonFile in $jsonFiles) {
# Read the JSON file and convert it to a PowerShell object
$jsonContent = Get-Content -Path $jsonFile.FullName -Raw | ConvertFrom-Json
# Function to process JSON objects recursively
function Process-JsonObject {
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
# If the input object is a collection (e.g., array), process each element
foreach ($item in $InputObject) {
Process-JsonObject $item
}
}
elseif ($InputObject -is [psobject]) {
# If the input object is a PSObject, check for "datasource" key and process properties
$properties = $InputObject | Get-Member -MemberType NoteProperty
foreach ($property in $properties) {
$value = $InputObject.$($property.Name)
if ($property.Name -eq "datasource" -and $value.type -and $value.uid) {
# If the "datasource" key is found, update the "uid" using the map
Write-Host "Updating uid for $($value.type) to $($typeUidMap[$value.type])"
$value.uid = $typeUidMap[$value.type]
}
# Recursively process the value of the property
Process-JsonObject $value
}
}
}
# Process the JSON content and update "uid" values
Process-JsonObject $jsonContent
# Save the updated JSON content back to the original file
$jsonContent | ConvertTo-Json | Set-Content -Path $jsonFile.FullName
}
# Get the unique folder names from the JSON file paths
$folderNames = $jsonFiles | ForEach-Object { Split-Path -Path $_.DirectoryName -Leaf } | Sort-Object -Unique
# Initialize a new map to store the folder name and the returned uid
$folderUidMap = @{}
Write-Host "Folder-UID map:" -ForegroundColor Yellow
# Get the existing folders from the API
$getFoldersApiUrl = "$FullUrl/api/folders?limit=1000"
$existingFolders = Invoke-RestMethod -Uri $getFoldersApiUrl -Method Get -Headers $headers
foreach ($folderName in $folderNames) {
# Skip the folder named "General"
if ($folderName -eq "General") {
continue
}
# Check if the folder already exists
$existingFolder = $existingFolders | Where-Object { $_.title -eq $folderName }
if ($existingFolder) {
# Update the folderUidMap with the existing folder's uid
$folderUidMap[$folderName] = $existingFolder.uid
} else {
# Create the folder using the API request
$folderCreationApiUrl = "$FullUrl/api/folders"
$folderCreationBody = @{
"title" = $folderName
} | ConvertTo-Json
$folderCreationResponse = Invoke-RestMethod -Uri $folderCreationApiUrl -Method Post -Headers $headers -Body $folderCreationBody
# Add the folder name and the returned uid to the map
$folderUidMap[$folderName] = $folderCreationResponse.uid
Write-Host "Created folder '$folderName' with uid '$($folderCreationResponse.uid)'"
}
}
foreach ($jsonFile in $jsonFiles) {
# Get the folder name for the current JSON file
$folderName = Split-Path -Path $jsonFile.DirectoryName -Leaf
# Get the folder UID from the folderUidMap, if the folder name exists in the map
$folderUid = $null
if ($folderUidMap.ContainsKey($folderName)) {
$folderUid = $folderUidMap[$folderName]
}
# Read the JSON file content
$jsonFileContent = Get-Content -Path $jsonFile.FullName -Raw | ConvertFrom-Json
# Set the id field to null
$jsonFileContent.id = $null
# Create the request body
$dashboardPostBody = @{
"dashboard" = $jsonFileContent
"message" = "Imported"
"overwrite" = $true
}
# Set the folderUid only if it's not null
if ($folderUid -ne $null) {
$dashboardPostBody["folderUid"] = $folderUid
Write-Host "Setting folderUid to '$folderUid' for '$($jsonFile.Name)'"
}
$dashboardPostBody = $dashboardPostBody | ConvertTo-Json
# Post the JSON file to the /api/dashboards/db endpoint
$dashboardPostApiUrl = "$FullUrl/api/dashboards/db"
$resp = Invoke-RestMethod -Uri $dashboardPostApiUrl -Method Post -Headers $headers -Body $dashboardPostBody
Write-Host "Imported '$($jsonFile.Name)' with uid '$($resp.uid)'"
Write-Host "Response: $($resp | ConvertTo-Json)"
}
# Delete the unpackaged grafana folder
Remove-Item -Path $UnpackagedgrafanaPath -Recurse -Force
if ($IsEncrypted){
# Remove the decrypted 7z file
Remove-Item -Path "${BackupPath}\grafana_backup.7z" -Recurse -Force | Out-Nulls
}