此範例 CLI 指令碼會將單一「適用於 PostgreSQL 的 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 CLI,因為在 Cloud Shell 中執行將花費過長時間。
登入 Azure
使用下列指令碼以特定的訂用帳戶登入。
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
如需詳細資訊,請參閱設定使用中的訂用帳戶或以互動方式登入
執行指令碼
# Restore an Azure Database for PostgreSQL server
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="backup-restore-postgresql"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
restoreServer="restore-server$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
echo "Using resource group $resourceGroup with login: $login, password: $password..."
# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku
# Sleeping commands to wait long enough for automatic backup to be created
echo "Sleeping..."
sleep 10m
# Restore a server from backup to a new server
# To specify a specific point-in-time (in UTC) to restore from, use the ISO8601 format:
# restorePoint=“2021-07-09T13:10:00Z”
restorePoint=$(date +%s)
restorePoint=$(expr $restorePoint - 60)
restorePoint=$(date -d @$restorePoint +"%Y-%m-%dT%T")
echo $restorePoint
echo "Restoring $restoreServer"
az postgres server restore --name $restoreServer --resource-group $resourceGroup --restore-point-in-time $restorePoint --source-server $server
清除部署
使用下列命令來移除資源群組及所有與其相關聯的資源,除非您持續需要這些資源,則請使用 az group delete 命令。 這其中某些資源可能需要一些時間才能建立和刪除。
az group delete --name $resourceGroup
範例參考
此指令碼會使用下表中簡述的命令:
| 命令 | 注意事項 |
|---|---|
| 請輸入命令:az group create 來建立群組。 | 建立用來存放所有資源的資源群組。 |
| az postgresql server create | 建立適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體,其中裝載資料庫。 |
| az postgresql 伺服器復原 | 從備份還原伺服器。 |
| az group delete | 刪除資源群組,包括所有的巢狀資源。 |