Aracılığıyla paylaş


Azure CLI kullanarak PostgreSQL için Azure Veritabanı esnek sunucu örneğini geri yükleme

Bu örnek CLI betiği, tek bir PostgreSQL için Azure Veritabanı esnek sunucu örneğini önceki bir zamana geri yükler.

Azure hesabınız yoksa, başlamadan önce ücretsiz hesap oluşturun.

Önkoşullar

Örnek betik

Bu betik için Cloud Shell'de çalıştırılması çok uzun sürdüğü için Azure CLI'yi yerel olarak kullanın.

Azure'da oturum açma

Belirli bir abonelik kullanarak oturum açmak için aşağıdaki betiği kullanın.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

Daha fazla bilgi için bkz . Etkin aboneliği ayarlama veya etkileşimli olarak oturum açma

Betiği çalıştırın

# 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

Dağıtımı temizleme

Bu kaynaklara sürekli ihtiyaç duymadığınız sürece az group delete komutunu kullanarak kaynak grubunu ve onunla ilişkili tüm kaynakları kaldırmak için aşağıdaki komutu kullanın. Bu kaynaklardan bazılarının oluşturulması ve silinmesi biraz zaman alabilir.

az group delete --name $resourceGroup

Örnek başvuru

Bu betik, aşağıdaki tabloda ana hatları verilen komutları kullanır:

Komut Notlar
az group create komutu bir grup oluşturmak için kullanılır. Tüm kaynakların depolandığı bir kaynak grubu oluşturur.
az postgresql server create Veritabanlarını barındıran PostgreSQL için Azure Veritabanı esnek bir sunucu örneği oluşturur.
az postgresql server restore Bir sunucuyu yedekten geri yükler.
az grubunu sil Bir kaynak grubunu tüm iç içe geçmiş kaynaklar dahil siler.