Home
%3CLINGO-SUB%20id%3D%22lingo-sub-464176%22%20slang%3D%22en-US%22%3ERe%3A%20Recreate%20dropped%20database%20on%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-464176%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F296874%22%20target%3D%22_blank%22%3E%40JovanPop%3C%2FA%3E%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20script.%20I%20tried%20and%20it%20ran%20for%20about%20an%20hour%20before%20returning%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ENew-AzResource%20%3A%20InternalServerError%20%3A%20An%20unexpected%20error%20occured%20while%20processing%20the%20request.%20Tracking%20ID%3A%0A'857ea045-6dde-41ce-a6a0-59e7c34763f8'%0AAt%20line%3A1%20char%3A1%0A%2B%20New-AzResource%20-ResourceId%20%22subscriptions%2F%24subscriptionId%2FresourceGro%20...%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20CloseError%3A%20(%3A)%20%5BNew-AzResource%5D%2C%20ErrorResponseMessageException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceCmdlet%3C%2FPRE%3E%3CP%3EAt%20the%20end%20the%20database%20was%20not%20restored%2C%20is%20there%20any%20other%20way%20to%20restore%20a%20dropped%20MI%20database%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EJose%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-650810%22%20slang%3D%22en-US%22%3ERe%3A%20Restore%20dropped%20database%20on%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-650810%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20updated%20the%20article%20-%20if%20the%20restore%20fails%20try%20again%20with%20the%20date%205%20min%20in%20the%20past.%20It%20is%20possible%20that%20deleted%20time%20is%20not%20accurate.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-386285%22%20slang%3D%22en-US%22%3ERestore%20dropped%20database%20on%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-386285%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlserverstorageengine%2F2019%2F01%2F21%2Frecreate-dropped-database-on-azure-sql-managed-instance%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EMSDN%20on%20Jan%2021%2C%202019%3C%2FA%3E.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%20This%20version%20of%20article%20is%20adjusted%20for%20the%20latest%20Az%20Powershell%20module%3C%2FSTRONG%3E%3CBR%20%2F%3EAzure%20SQL%20Database%20-%20Managed%20Instance%20is%20fully-managed%20PaaS%20service%20that%20provides%20advanced%20disaster-recovery%20capabilities.%20Even%20if%20you%20accidentally%20drop%20a%20database%20or%20someone%20drops%20your%20database%20as%20part%20of%20security%20attack%2C%20Managed%20Instance%20will%20enable%20you%20to%20easily%20recover%20the%20dropped%20database.%20This%20feature%20is%20in%20preview%20at%20the%20time%20of%20writing%20this%20article.%3CBR%20%2F%3E%3CBR%20%2F%3EAzure%20SQL%20Managed%20Instance%20performs%20automatic%20backups%20of%20you%20database%20every%205-10%20minutes.%20If%20anything%20happens%20with%20your%20database%20and%20even%20if%20someone%20drops%20it%2C%20your%20%3CSTRONG%3E%20data%20is%20not%20lost%20%3C%2FSTRONG%3E%20.%20Managed%20Instance%20enables%20you%20to%20easily%20re-create%20the%20dropped%20database%20from%20the%20automatic%20backups.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20let's%20imagine%20that%20someone%20dropped%20your%20database.%20Below%20you%20can%20find%20the%20PowerShell%20script%20that%20can%20restore%20it.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1505388667%22%20id%3D%22toc-hId-1505388667%22%3ERecreate%20your%20dropped%20database%3C%2FH3%3E%0A%3CP%3E%3CBR%20%2F%3EBefore%20you%20run%20this%20script%20you%20need%20to%20login%20to%20your%20Azure%20account%20and%20select%20the%20subscription%20where%20your%20database%20is%20dropped%20using%20the%20script%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ELogin-AzAccount%0A%0A%24subscriptionId%20%3D%20%22cd82737-0791-418f-......%22%0ASelect-AzSubscription%20-SubscriptionId%20%24subscriptionId%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20also%20need%20to%20install%20necessary%20Az%20modules%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EInstall-Module%20-Name%20%22Az.Accounts%22%0AInstall-Module%20-Name%20%22Az.Resources%22%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20you%20are%20ready%20to%20restore%20your%20database.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EFirst%2C%20populate%20information%20about%20the%20instance%20and%20database%20(in%20this%20case%20AdventureWorksDW)%20that%20you%20want%20to%20recover%3A%3C%2FP%3E%0A%3CPRE%3E%24subscriptionId%20%3D%20%22cd827379-9270-0791-.....%22%20%0A%24resourceGroup%20%3D%20%22rg_recovery%22%20%0A%24location%20%3D%20%22West%20Central%20US%22%20%0A%24managedInstanceName%20%3D%20%22my-managed-instance%22%20%0A%24deletedDatabaseName%20%3D%20%22AdventureWorksDW%22%20%0A%24targetDatabaseName%20%3D%20%22NewAdventureWorksDW%22%20%3C%2FPRE%3E%0A%3CP%3EIn%20this%20example%2C%20dropped%20AdventureWorksDW%20on%20the%20instance%20jovanpop-try-re-create-db%20in%20West%20Central%20US%20region%20will%20be%20re-created%20as%20NewAdventureWorksDW%20database.%20Then%2C%20you%20can%20run%20the%20following%20script%20that%20uses%20these%20variables%20to%20recreate%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24db%20%3D%20Get-AzResource%20-ResourceId%20%22%2Fsubscriptions%2F%24subscriptionId%2FresourceGroups%2F%24resourceGroup%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2F%24managedInstanceName%2FrestorableDroppedDatabases%22%20-ApiVersion%20%222017-03-01-preview%22%20%7C%0A%20Where-Object%20%7B%20%24_.Properties.databaseName%20-eq%20%24deletedDatabaseName%20%7D%20%7C%0A%20%20Sort-Object%20-Property%20%40%7BExpression%3D%7B%24_.Properties.deletionDate%7D%3B%20Ascending%20%3D%20%24False%7D%20%7C%20%0A%20%20%20Select-Object%20-First%201%0A%20%20%0AWrite-Host%20%22Database%20%24(%24db%5B0%5D.Properties.databaseName)%20created%20on%20%24(%24db%5B0%5D.Properties.creationDate)%20dropped%20on%20%24(%24db%5B0%5D.Properties.deletionDate)%22%20%3C%2FPRE%3E%0A%3CP%3EThis%20script%20with%20fetch%20the%20information%20about%20the%20deleted%20database%20and%20display%20it.%20Make%20sure%20that%20you%20get%20some%20info%20by%20examining%20%24db%20object%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EDOWS%5Csystem32%26gt%3B%20%24db%0A%0AName%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20AdventureWorksDW%2C132034386123300000%0AResourceGroupName%20%3A%20cl_recovery%0AResourceType%20%20%20%20%20%20%3A%20Microsoft.Sql%2FmanagedInstances%2FrestorableDroppedDatabases%0ALocation%20%20%20%20%20%20%20%20%20%20%3A%20westcentralus%0AResourceId%20%20%20%20%20%20%20%20%3A%20%2Fsubscriptions%2F.......%2FresourceGroups%2Fcl_pilot%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fmy-managed-instance%2Fre%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20storableDroppedDatabases%2FAdventureWorksDW%2C132034386123300000%0A%3C%2FPRE%3E%0A%3CP%3EOnce%20you%20double%20check%20that%20this%20database%20is%20the%20one%20that%20should%20be%20restored%2C%20you%20can%20execute%20the%20following%20script%3A%3C%2FP%3E%0A%3CPRE%3E%24properties%20%3D%20New-Object%20System.Object%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20CreateMode%20-Value%20%22PointInTimeRestore%22%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20RestorePointInTime%20-Value%20%24db.Properties.deletionDate%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20RestorableDroppedDatabaseId%20-Value%20%24db.ResourceId%0ANew-AzResource%20-Location%20%24location%20-Properties%20%24properties%20-ResourceId%20%22subscriptions%2F%24subscriptionId%2FresourceGroups%2F%24resourceGroup%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2F%24managedInstanceName%2Fdatabases%2F%24targetDatabaseName%22%20-ApiVersion%20%222017-03-01-preview%22%20-Force%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20result%2C%20new%20database%20called%20NewAdventureWorksDW%20will%20be%20created%20as%20a%20copy%20of%20the%20database%20AdventureWorksDW%20that%20is%20dropped.%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3E%20In%20some%20cases%2C%20the%20dropped%20date%20returned%20by%20this%20script%20might%20not%20be%20the%20exact%20date%20when%20the%20database%20is%20dropped.%20Currently%2C%20deleted%20date%20is%20the%20date%20when%20the%20tail%20log%20backup%20is%20finished%3B%20however%2C%20this%20might%20be%20up%20to%205%20minutes%20after%20the%20date%20when%20you%20dropped%20the%20database.%20If%20you%20know%20exact%20date%20when%20you%20dropped%20the%20database%20use%20that%20date.%20Otherwise%20try%20to%20subtract%205min%20from%20this%20date%20if%20your%20restore%20fails.%20This%20is%20temporary%20limitation%20that%20will%20be%20fixed%20before%20GA%20of%20the%20feature.%26nbsp%3B%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EFull%20script%20is%20shown%20in%20the%20following%20listing%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ELogin-AzureRmAccount%0A%24subscriptionId%20%3D%20%22%26lt%3Bsubscription%20id%26gt%3B%22%0A%0ASelect-AzSubscription%20-SubscriptionId%20%24subscriptionId%0A%0A%24resourceGroup%20%3D%20%22%26lt%3Bresource%20group%26gt%3B%22%0A%24location%20%3D%20%22%26lt%3Bregion%26gt%3B%22%0A%24managedInstanceName%20%3D%20%22%26lt%3Binstance%20name%26gt%3B%22%0A%24deletedDatabaseName%20%3D%20%22%26lt%3Bdatabase%20name%26gt%3B%22%20%0A%24targetDatabaseName%20%3D%20%22%26lt%3Bnew%20name%26gt%3B%22%20%0A%0A%24db%20%3D%20Get-AzResource%20-ResourceId%20%22%2Fsubscriptions%2F%24subscriptionId%2FresourceGroups%2F%24resourceGroup%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2F%24managedInstanceName%2FrestorableDroppedDatabases%22%20-ApiVersion%20%222017-03-01-preview%22%20%7C%0A%20Where-Object%20%7B%20%24_.Properties.databaseName%20-eq%20%24deletedDatabaseName%20%7D%20%7C%0A%20%20Sort-Object%20-Property%20%40%7BExpression%3D%7B%24_.Properties.deletionDate%7D%3B%20Ascending%20%3D%20%24False%7D%20%7C%20%0A%20%20%20Select-Object%20-First%201%0A%20%20%0AWrite-Host%20%22Database%20%24(%24db%5B0%5D.Properties.databaseName)%20created%20on%20%24(%24db%5B0%5D.Properties.creationDate)%20dropped%20on%20%24(%24db%5B0%5D.Properties.deletionDate)%22%20%0A%24properties%20%3D%20New-Object%20System.Object%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20CreateMode%20-Value%20%22PointInTimeRestore%22%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20RestorePointInTime%20-Value%20%24db.Properties.deletionDate%20%0A%24properties%20%7C%20Add-Member%20-type%20NoteProperty%20-name%20RestorableDroppedDatabaseId%20-Value%20%24db.ResourceId%0ANew-AzResource%20-Location%20%24location%20-Properties%20%24properties%20-ResourceId%20%22subscriptions%2F%24subscriptionId%2FresourceGroups%2F%24resourceGroup%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2F%24managedInstanceName%2Fdatabases%2F%24targetDatabaseName%22%20-ApiVersion%20%222017-03-01-preview%22%20-Force%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-386285%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Jan%2021%2C%202019%20Azure%20SQL%20Database%20-%20Managed%20Instance%20is%20fully-managed%20PaaS%20service%20that%20provides%20advanced%20disaster-recovery%20capabilities.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-386285%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EManaged%20Instance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ERestore%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on MSDN on Jan 21, 2019.

Note: This version of article is adjusted for the latest Az Powershell module
Azure SQL Database - Managed Instance is fully-managed PaaS service that provides advanced disaster-recovery capabilities. Even if you accidentally drop a database or someone drops your database as part of security attack, Managed Instance will enable you to easily recover the dropped database. This feature is in preview at the time of writing this article.

Azure SQL Managed Instance performs automatic backups of you database every 5-10 minutes. If anything happens with your database and even if someone drops it, your data is not lost . Managed Instance enables you to easily re-create the dropped database from the automatic backups.

 

Now, let's imagine that someone dropped your database. Below you can find the PowerShell script that can restore it.

Recreate your dropped database


Before you run this script you need to login to your Azure account and select the subscription where your database is dropped using the script like:

 

 

Login-AzAccount

$subscriptionId = "cd82737-0791-418f-......"
Select-AzSubscription -SubscriptionId $subscriptionId

 

 

You also need to install necessary Az modules:

 

Install-Module -Name "Az.Accounts"
Install-Module -Name "Az.Resources"

 

 

Now you are ready to restore your database.


First, populate information about the instance and database (in this case AdventureWorksDW) that you want to recover:

$subscriptionId = "cd827379-9270-0791-....." 
$resourceGroup = "rg_recovery" 
$location = "West Central US" 
$managedInstanceName = "my-managed-instance" 
$deletedDatabaseName = "AdventureWorksDW" 
$targetDatabaseName = "NewAdventureWorksDW" 

In this example, dropped AdventureWorksDW on the instance jovanpop-try-re-create-db in West Central US region will be re-created as NewAdventureWorksDW database. Then, you can run the following script that uses these variables to recreate it:

 

 

$db = Get-AzResource -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/managedInstances/$managedInstanceName/restorableDroppedDatabases" -ApiVersion "2017-03-01-preview" |
 Where-Object { $_.Properties.databaseName -eq $deletedDatabaseName } |
  Sort-Object -Property @{Expression={$_.Properties.deletionDate}; Ascending = $False} | 
   Select-Object -First 1
  
Write-Host "Database $($db[0].Properties.databaseName) created on $($db[0].Properties.creationDate) dropped on $($db[0].Properties.deletionDate)" 

This script with fetch the information about the deleted database and display it. Make sure that you get some info by examining $db object:

 

DOWS\system32> $db

Name              : AdventureWorksDW,132034386123300000
ResourceGroupName : cl_recovery
ResourceType      : Microsoft.Sql/managedInstances/restorableDroppedDatabases
Location          : westcentralus
ResourceId        : /subscriptions/......./resourceGroups/cl_pilot/providers/Microsoft.Sql/managedInstances/my-managed-instance/re
                    storableDroppedDatabases/AdventureWorksDW,132034386123300000

Once you double check that this database is the one that should be restored, you can execute the following script:

$properties = New-Object System.Object 
$properties | Add-Member -type NoteProperty -name CreateMode -Value "PointInTimeRestore" 
$properties | Add-Member -type NoteProperty -name RestorePointInTime -Value $db.Properties.deletionDate 
$properties | Add-Member -type NoteProperty -name RestorableDroppedDatabaseId -Value $db.ResourceId
New-AzResource -Location $location -Properties $properties -ResourceId "subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/managedInstances/$managedInstanceName/databases/$targetDatabaseName" -ApiVersion "2017-03-01-preview" -Force

 

As a result, new database called NewAdventureWorksDW will be created as a copy of the database AdventureWorksDW that is dropped.

 

NOTE: In some cases, the dropped date returned by this script might not be the exact date when the database is dropped. Currently, deleted date is the date when the tail log backup is finished; however, this might be up to 5 minutes after the date when you dropped the database. If you know exact date when you dropped the database use that date. Otherwise try to subtract 5min from this date if your restore fails. This is temporary limitation that will be fixed before GA of the feature. 

Full script is shown in the following listing:

 

Login-AzureRmAccount
$subscriptionId = "<subscription id>"

Select-AzSubscription -SubscriptionId $subscriptionId

$resourceGroup = "<resource group>"
$location = "<region>"
$managedInstanceName = "<instance name>"
$deletedDatabaseName = "<database name>" 
$targetDatabaseName = "<new name>" 

$db = Get-AzResource -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/managedInstances/$managedInstanceName/restorableDroppedDatabases" -ApiVersion "2017-03-01-preview" |
 Where-Object { $_.Properties.databaseName -eq $deletedDatabaseName } |
  Sort-Object -Property @{Expression={$_.Properties.deletionDate}; Ascending = $False} | 
   Select-Object -First 1
  
Write-Host "Database $($db[0].Properties.databaseName) created on $($db[0].Properties.creationDate) dropped on $($db[0].Properties.deletionDate)" 
$properties = New-Object System.Object 
$properties | Add-Member -type NoteProperty -name CreateMode -Value "PointInTimeRestore" 
$properties | Add-Member -type NoteProperty -name RestorePointInTime -Value $db.Properties.deletionDate 
$properties | Add-Member -type NoteProperty -name RestorableDroppedDatabaseId -Value $db.ResourceId
New-AzResource -Location $location -Properties $properties -ResourceId "subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/managedInstances/$managedInstanceName/databases/$targetDatabaseName" -ApiVersion "2017-03-01-preview" -Force
2 Comments
Regular Visitor

Hello @JovanPop

 

Thanks for the script. I tried and it ran for about an hour before returning the following error:

 

New-AzResource : InternalServerError : An unexpected error occured while processing the request. Tracking ID:
'857ea045-6dde-41ce-a6a0-59e7c34763f8'
At line:1 char:1
+ New-AzResource -ResourceId "subscriptions/$subscriptionId/resourceGro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzResource], ErrorResponseMessageException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceCmdlet

At the end the database was not restored, is there any other way to restore a dropped MI database?

 

Thanks,

Jose

Microsoft

I have updated the article - if the restore fails try again with the date 5 min in the past. It is possible that deleted time is not accurate.