다음을 통해 공유


Azure CLI를 사용하여 Azure SQL Database의 단일 데이터베이스를 이전 시점으로 복원

적용 대상:Azure SQL Database

이 Azure CLI 스크립트 예제는 Azure SQL Database의 단일 데이터베이스를 특정 시점으로 복원합니다.

Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다.

필수 조건

샘플 스크립트

이 스크립트의 경우 Cloud Shell에서 실행하기에 너무 오래 걸리므로 Azure CLI를 로컬로 사용하세요.

Azure에 로그인

다음 스크립트를 사용하여 특정 구독을 사용하여 로그인합니다.

subscription="<subscriptionId>" # add subscription here

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

자세한 내용은 활성 구독 설정 또는 대화형으로 로그인을 참조하세요.

스크립트 실행

# Restore a single database in Azure SQL Database to an earlier point in time

# Use Bash rather than Cloud Shell due to its timeout at 20 minutes when no interactive activity 
# In Windows, run Bash in a Docker container to sync time zones between Azure and Bash.

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
___location="East US"
resourceGroup="msdocs-sql-rg-$randomIdentifier"
tag="restore-database"
server="msdocs-azuresql-server-$randomIdentifier"
database="msdocsazuresqldb$randomIdentifier"
restoreServer="restoreServer-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"

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 $server in $___location..."
az sql server create --name $server --resource-group $resourceGroup --___location "$___location" --admin-user $login --admin-password $password

echo "Creating $database on $server..."
az sql db create --resource-group $resourceGroup --server $server --name $database --service-objective S0

# Sleeping commands to wait long enough for automatic backup to be created
echo "Sleeping..."
sleep 30m

# 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 to $restoreServer"
az sql db restore --dest-name $restoreServer --edition Standard --name $database --resource-group $resourceGroup --server $server --service-objective S0 --time $restorePoint

리소스 정리

다음 명령을 사용하여 이러한 리소스가 계속해서 필요한 경우가 아니면 az group delete 명령을 사용하여 리소스 그룹 및 연결된 모든 리소스를 제거합니다. 이러한 리소스 중 일부는 만들고 삭제하는 데 시간이 걸릴 수 있습니다.

az group delete --name $resourceGroup

샘플 참조

이 스크립트는 다음 명령을 사용합니다. 테이블에 있는 각 명령은 명령에 해당하는 문서에 연결됩니다.

명령 설명
az sql db restore 데이터베이스 복원 명령입니다.