Azure CLI 簡單入門

Azure CLI 是什麼

Azure 命令行接口 (CLI) 是用於管理 Azure 資源的 Microsoft 跨平臺命令行體驗。 Azure CLI 易於學習,是構建適用於 Azure 資源的自定義自動化功能的完美工具。web

通俗的說就是:能夠讓咱們經過一系列的命令行接口來管理咱們的Azure 資源,如部署應用,設置防火牆,數據庫導出備份等等。sql

如何使用

首先咱們下載一個 Azure CLI客戶端,點這裏下載,下載完成後,咱們能夠經過 Windows PowerShell 查看azure cli 的版本,輸入  az --version可查看版本號。
P.S:最新版Azure CLI 的命令都是以 「az」開頭,能夠經過Windows 命令提示符、Windows PowerShell、**瀏覽器 + Azure Cloud Shell 等三種方式運行。這裏咱們使用 Windows PowerShell 這個工具來實現咱們的命令。shell

Part1-登陸

登陸命令以下:az login  ,若是用到的是由世紀互聯運營的中國版Azure ,請先執行 az cloud set -n AzureChinaCloud  以切換到中國區登陸。
鏈接你的帳戶的命令是:Connect-AzAccount  ,中國區是:Connect-AzAccount -Environment AzureChinaCloud數據庫

Part2-部署

這裏部署的是一個應用服務api

az webapp deployment source config-zip --resource-group GroupName --name AppName --src D:\MyProject\Publish\Publish.zip
複製代碼
  • GroupName 是你的資源組名稱
  • AppName 是你的應用名稱
  • src 後面的是你的部署文件路徑(文件只能是zip)

Part3-數據庫

防火牆

這裏利用了 這個網址2019.ip138.com/ic.asp 的獲取本機IP的接口而後修改對應防火牆規則的IP地址。瀏覽器

param($Server)
$Ip = Invoke-WebRequest -Uri "http://2019.ip138.com/ic.asp"
$str=$null
if ($Ip.StatusCode -eq 200)
{
  [string]$str = $Ip.ParsedHtml.body.innerHTML
  $StartIndex = $str.IndexOf("[")
  $EndIndex = $str.IndexOf("]")
  $length = $EndIndex - $StartIndex - 1
  $ip = $str.Substring($StartIndex + 1, $length)
  $ip
}
else
{
  Write-Warning "Bad Request"
}
az sql server firewall-rule update -g groupname -s $Server -n firefulename --start-ip-address $Ip --end-ip-address $Ip
複製代碼
  • groupname  是你的資源組名稱
  • firefulename 是你的防火牆規則名稱

備份

若提示登陸憑據已失效,中國區需執行:Connect-AzAccount -Environment AzureChinaCloud
再執行如下命令(文中導出路徑已固定本地文件夾,可自行調整):app

$today=Get-Date
$dbName="yourdbname"
$bacName=$dbName+"-"+$today.ToString('yyyy-M-d-H-m')+".bacpac"
$Secure_String_Pwd=ConvertTo-SecureString "yourpassword" -AsPlainText -Force
$exportRequest = New-AzSqlDatabaseExport -ResourceGroupName YourGroupName -ServerName YourServer -DatabaseName $dbName -StorageKeytype StorageAccessKey -StorageKey storagekeythisVwZJQg4go430testww5S+L3r32OPHxSuzRABCWWCv4N/YWEX6rln8JWUQhckA== -StorageUri https://yourstorage.blob.core.chinacloudapi.cn/database-container/$bacName -AdministratorLogin youraccount -AdministratorLoginPassword $Secure_String_Pwd
$exportRequest
Start-Sleep -s 90
$ctx = New-AzStorageContext -ConnectionString "yourconnectionstringstr"
$ContainerName='yourcontainer'
Get-AzStorageblobcontent -Blob $bacName `
  -Container $containerName `
  -Destination 'D:\yourlocal\backup' `
  -Context $ctx
複製代碼

Part4-存儲

導出Blob

$ctx = New-AzStorageContext -ConnectionString "yourconnectionstring"
$bacName="yourblobname"
$ContainerName='yourcontainer'
Get-AzStorageblobcontent -Blob $bacName `
  -Container $containerName `
  -Destination 'D:\yourlocal\backup' `
  -Context $ctx
複製代碼

參考資料

相關文章
相關標籤/搜索