Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backup script for Standard #1319

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions deploy/standard/scripts/New-Backup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env pwsh

Param(
[Parameter(Mandatory = $true)][string]$storageResourceGroup,
[Parameter(Mandatory = $true)][string]$vecResourceGroup,
[Parameter(Mandatory = $true)][string]$destinationStorageAccount
)

Set-PSDebug -Trace 0 # Echo every command (0 to disable, 1 to enable)
Set-StrictMode -Version 3.0
$ErrorActionPreference = "Stop"

function Get-SearchServiceIndexes {
param(
[Parameter(Mandatory = $true)][string]$searchServiceName,
[Parameter(Mandatory = $true)][string]$pathPrefix
)

$serviceUri = "https://$searchServiceName.search.windows.net"
$uri = $serviceUri + "/indexes?api-version=2020-06-30&`$select=name"
$token = $(az account get-access-token --query accessToken --output tsv --scope "https://search.azure.com/.default")
$headers = @{"Authorization" = "Bearer $token"; "Accept" = "application/json"; "ContentType" = "application/json; charset=utf-8"}

$indexes = $(Invoke-RestMethod -Uri $uri -Method GET -Headers $headers).value.name
foreach ($index in $indexes) {
$uri = $serviceUri `
+ "/indexes/$index/docs/`$count?api-version=2020-06-30"
$req = [System.Net.WebRequest]::Create($uri)

$req.ContentType = "application/json; charset=utf-8"
$req.Accept = "application/json"
$req.Headers["Authorization"] = "Bearer $token"

$resp = $req.GetResponse()
$reader = new-object System.IO.StreamReader($resp.GetResponseStream())
$result = $reader.ReadToEnd()
$documentCount = [int]$result

$pageCount = [math]::ceiling($documentCount / 500)

$job = 1..$pageCount | ForEach-Object -Parallel {
$skip = ($_ - 1) * 500
$uri = $using:serviceUri + "/indexes/$($using:index)/docs?api-version=2020-06-30&search=*&`$skip=$($skip)&`$top=500&searchMode=all"
$outputPath = "$($using:index)_$($_).json"
Invoke-RestMethod -Uri $uri -Method GET -Headers $using:headers -ContentType "application/json" |
ConvertTo-Json -Depth 9 |
Set-Content $outputPath
"Output: $uri"
azcopy copy $outputPath "$($using:pathPrefix)/$($using:index)/"
} -ThrottleLimit 5 -AsJob
$job | Receive-Job -Wait
}
}

# Navigate to the script directory so that we can use relative paths.
Push-Location $($MyInvocation.InvocationName | Split-Path)
try {
# Create backups container
az storage container create -n backups --account-name $destinationStorageAccount

# Get main storage account
$sourceStorageAccountName = ""
foreach ($resourceId in (az storage account list -g $storageResourceGroup --query "@[].id" --output tsv)) {
if ((az tag list --resource-id $resourceId --query "contains(keys(@.properties.tags), 'azd-env-name')") -eq $true) {
$sourceStorageAccountName = $(az resource show --ids $resourceId --query "@.name" --output tsv)
Write-Host "Selecting $sourceStorageAccountName as the storage account"
break;
}
}

if ([string]::IsNullOrEmpty($sourceStorageAccountName)) {
throw "Could not find any storage accounts with the azd-env-name tag in $storageResourceGroup."
}

# Recursively copy storage account contents
$env:AZCOPY_AUTO_LOGIN_TYPE="AZCLI"
foreach ($container in (az storage container list --account-name $sourceStorageAccountName --query "@[].name" --auth-mode login -o tsv)) {
azcopy copy "https://$($sourceStorageAccountName).blob.core.windows.net/$container/" "https://$destinationStorageAccount.blob.core.windows.net/backups/$storageResourceGroup/" --recursive
}

$sourceSearchServiceName = ""
foreach ($resourceId in (az search service list -g $vecResourceGroup --query "@[].id" --output tsv)) {
if ((az tag list --resource-id $resourceId --query "contains(keys(@.properties.tags), 'azd-env-name')") -eq $true) {
$sourceSearchServiceName = $(az resource show --ids $resourceId --query "@.name" --output tsv)
Write-Host "Selecting $sourceSearchServiceName as the search service"
break;
}
}

if ([string]::IsNullOrEmpty($sourceSearchServiceName)) {
throw "Could not find any search services with the azd-env-name tag in $vecResourceGroup."
}

# Save Search Indexes
Get-SearchServiceIndexes -searchServiceName $sourceSearchServiceName -pathPrefix "https://$destinationStorageAccount.blob.core.windows.net/backups/$vecResourceGroup/data-sources/indexes"
}
catch {
if (Test-Path -Path "$env:HOME/.azcopy") {
$logFile = Get-ChildItem -Path "$env:HOME/.azcopy" -Filter "*.log" | `
Where-Object { $_.Name -notlike "*-scanning*" } | `
Sort-Object LastWriteTime -Descending | `
Select-Object -First 1
$logFileContent = Get-Content -Raw -Path $logFile.FullName
Write-Host $logFileContent
}
Write-Host $_.Exception.Message
}
finally {
Pop-Location
Set-PSDebug -Trace 0 # Echo every command (0 to disable, 1 to enable)
}
Loading