Azure CLI 是什么
Azure 命令行接口 (CLI) 是用於管理 Azure 資源的 Microsoft 跨平台命令行體驗。 Azure CLI 易於學習,是構建適用於 Azure 資源的自定義自動化功能的完美工具。
通俗的說就是:可以讓我們通過一系列的命令行接口來管理我們的Azure 資源,如部署應用,設置防火牆,數據庫導出備份等等。
如何使用
首先我們下載一個 Azure CLI客戶端,點這里下載,下載完成后,我們可以通過 Windows PowerShell 查看azure cli 的版本,輸入 az --version
可查看版本號。
P.S:最新版Azure CLI 的命令都是以 “az”開頭,可以通過Windows 命令提示符、Windows PowerShell、瀏覽器 + Azure Cloud Shell 等三種方式運行。這里我們使用**Windows PowerShell **這個工具來實現我們的命令。
Part1-登錄
登錄命令如下:az login
,如果用到的是由世紀互聯運營的中國版Azure ,請先執行 az cloud set -n AzureChinaCloud
以切換到中國區登錄。
連接你的賬戶的命令是:Connect-AzAccount
,中國區是:Connect-AzAccount -Environment AzureChinaCloud
Part2-部署
這里部署的是一個應用服務
az webapp deployment source config-zip --resource-group GroupName --name AppName --src D:\MyProject\Publish\Publish.zip
- GroupName 是你的資源組名稱
- AppName 是你的應用名稱
- src 后面的是你的部署文件路徑(文件只能是zip)
Part3-數據庫
防火牆
這里利用了 這個網址http://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
再執行以下命令(文中導出路徑已固定本地文件夾,可自行調整):
$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