適用於:Azure SQL 資料庫
此 Azure CLI 範例會將 SQL Database 中的資料庫備份至 Azure 儲存體容器。
如果您沒有 Azure 訂閱,請在開始之前,先建立一個 Azure 免費帳戶。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
範例指令碼
啟動 Azure Cloud Shell
Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。
若要開啟 Cloud Shell,請選取程式碼區塊右上角的 [試試看]。 您也可以在另一個獨立的瀏覽器標籤中開啟 Cloud Shell,方法是前往 https://shell.azure.com。
當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI。 選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。
登入 Azure
Cloud Shell 會在首次登入的帳戶下自動完成驗證。 使用下列指令碼使用不同的訂閱登入,將 <Subscription ID> 替換為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂閱,請在開始之前,先建立一個 Azure 免費帳戶。
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
執行指令碼
# Backup an Azure SQL single database to an Azure storage container
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-azuresql-rg-$randomIdentifier"
tag="backup-database"
server="msdocs-azuresql-server-$randomIdentifier"
database="msdocsazuresqldb$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
storage="msdocsazuresql$randomIdentifier"
container="msdocs-azuresql-container-$randomIdentifier"
bacpac="backup.bacpac"
echo "Using resource group $resourceGroup with login: $login, password: $password..."
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
echo "Creating $storage..."
az storage account create --name $storage --resource-group $resourceGroup --location "$location" --sku Standard_LRS
echo "Creating $container on $storage..."
key=$(az storage account keys list --account-name $storage --resource-group $resourceGroup -o json --query [0].value | tr -d '"')
az storage container create --name $container --account-key $key --account-name $storage
echo "Creating $server in $location..."
az sql server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password
az sql server firewall-rule create --resource-group $resourceGroup --server $server --name AllowAzureServices --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
echo "Creating $database..."
az sql db create --name $database --resource-group $resourceGroup --server $server --edition GeneralPurpose --sample-name AdventureWorksLT
echo "Backing up $database..."
az sql db export --admin-password $password --admin-user $login --storage-key $key --storage-key-type StorageAccessKey --storage-uri "https://$storage.blob.core.windows.net/$container/$bacpac" --name $database --resource-group $resourceGroup --server $server
清除資源
您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。
az group delete --name $resourceGroup
範例參考
此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。
| 指令 | 備註 |
|---|---|
| az sql server | 伺服器命令。 |
| az sql db | 資料庫命令。 |