Reading Time: < 1 minute

In this post we will see how we can easily restore database in Azure SQL ManagedInstance. There are two ways to do this, using T-SQL and Powershell.

 

Using Powershel

Before executing the following Powershell Script to restore the database we must be sure for the following prerequisites:

  • Powershell command line Shell
  • SqlServer Powershell module
  • SQL Managed Instance
  • Azure Storage Account


Install Or Update SqlServer PS module

To install the SqlServer module execute the following command:

#Install The SqlServer module if not installed
Install-Module -Name SqlServer

#Alternatively Use The "-AllowClobber" parameter if previous version of the SqlServer module is already installed
Install-Module -Name SqlServer -AllowClobber 

#Update The SqlServer module if already installed
Update-Module -Name SqlServer
#Type the Managed instance admin login
$username = "#########"

#Type the Managed instance admin password
$password = '################'

#Type the Full Managed instance name
$managedInstance = "############.9ab5d2b08bb9.database.windows.net"

#Leave this parameter as is
$database = "master" 

#Before execute the Invoke-Sqlcmd, type the address with the full database backup path

Invoke-Sqlcmd -ServerInstance $managedInstance -Database `
$database -Username `
$username -Password $password `
-Query "RESTORE DATABASE [mydb] FROM URL = 'https://#########.blob.core.windows.net/files/databases/mydb.bak'"

 

Using T-SQL

Before executing the following T-SQL Script to restore the database we must be sure for the following prerequisites:

 

  • SSMS (SQL Server Management Studio)
  • SQL Managed Instance
  • Azure Storage Account
CREATE CREDENTIAL [https://#########.blob.core.windows.net/datbases] 
WITH IDENTITY = 'SHARED ACCESS SIGNATURE'
, SECRET = 'sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-01-31T20:41:49Z&st=2019-01-01T12:41:49Z&spr=https&sig=###################################################'

RESTORE DATABASE [Chinook] FROM URL =
  'https://#########.blob.core.windows.net/databases/mydb.bak'
Share This