c# - 如何使用 C# 代码使用命令 azure cli 导出数据库
问题描述
我使用Azure CLI将资源组中的数据库备份导出到blobstorage,所以我想在Visual Studio 代码上使用相同的命令和 c#。
例如,我在 Azure CLI 中使用以下命令导出资源组中的数据库:
az sql db export -s (sql server) -n (database) -g (group) -p (password)
-u login --storage-key " key "
--storage-key-type
--storage-uri (url blob)
我怎样才能使用 C# 代码来实现这一点?
解决方案
你可以使用 Azure 管理 REST API。这是开始使用 C# 的入门指南。
就 SQL 导出而言,这是一个可以移植到 C# 的脚本示例。所有功劳归功于撰写博客的 Microsoft 员工!
# Sign in to Azure.
Login-AzureRmAccount
# If your Azure account is on a non-public cloud, make sure to specify the proper environment
# example for the German cloud:
# Login-AzureRmAccount -EnvironmentName AzureGermanCloud
# Fill in your subscription and SQL Database details
$subscriptionId = "11111111-aaaa-bbbb-cccc-222222222222"
$resourceGroup = "yourresourcegroup"
$server = "yourserver"
$database = "yourdatabase"
$sqlAdminLogin = "sqladmin"
# $sqlPassword = "yourpassword"
# may break if your password contains characters used by PowerShell, e.g. the $ sign
# instead setting it directly in request body further below
# Generate a unique filename for the BACPAC
$bacpacFilename = $database + (Get-Date).ToString("yyyy-MM-dd-HH-mm") + ".bacpac"
# Fill in your storage account and container name
$baseStorageUri = "https://yourstorageaccount.blob.core.windows.net/yourcontainer/"
# Compose the storage URI
$storageUri = $baseStorageUri + $bacpacFilename
# Fill in your storage access key
$storageKey= "mDyvvJ...yourstoragekey...tnlXIosA=="
# If you have multiple subscriptions, uncomment and set to the subscription you want to work with:
# Set-AzureRmContext -SubscriptionId $subscriptionId
# This is the Tenant ID from the account that created your AAD app:
$tenantId = "99999999-zzzz-yyyy-xxxx-888888888888"
# This is the Application ID from your AAD app:
$clientId = "54c45a1a-5c1a-40ad-88b6-a37e82223eda"
# This is the Secret from your AAD app:
$key = "yoursecret"
# Acquire the authentication context
$authUrl = "https://login.windows.net/${tenantId}"
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl
$cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential $clientId,$key
$authresult = $authContext.AcquireToken("https://management.core.windows.net/",$cred)
# Fill in the request header with authentication and content type
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$authresult.CreateAuthorizationHeader()
}
# Fill in the request body with storage details and database login
$body = @{storageKeyType = 'StorageAccessKey'; `
storageKey=$storageKey; `
storageUri=$storageUri;`
administratorLogin=$sqlAdminLogin; `
administratorLoginPassword='yourpassword';`
authenticationType='SQL'`
} | ConvertTo-Json
# Compile the details for the REST URI
$restURI = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/servers/$server/databases/$database/export?api-version=2014-04-01"
# Execute the REST API command
$result = Invoke-RestMethod -Uri $restURI -Method POST -Headers $authHeader -Body $body
Write-Output $result
推荐阅读
- javascript - 如何将反应登录组件重构为现有的材料 ui 登录组件?
- testing - Laravel 8 - 使用“Date Mutators”和 Faker 的 PHPUnit 测试问题
- qt - 如何使用 qml 打开相机闪光灯或手电筒?
- java - 使用正则表达式获取子字符串
- reactjs - 图像从带有阴影的容器中溢出
- android - 如何在flutter中写入json文件?下面的代码有什么问题?
- php - 用 PHP 从今天和昨天读取文件
- android - 为什么我的 DialogFragment 在 Android 11 中没有全屏显示?
- python - 如何输入字符串或句子并从字典中输出相应的值
- django-tables2 - 当我在一个视图中设置表标题时,我有三个表视图都是 django-table2,每个视图都会更改它