Is is possible to use the Az.sql module to Restore a SQL Server .bak File to an Azure Managed Instance Database?

Amie Barkes 41 Reputation points
2022-09-09T15:35:27.72+00:00

I am tasked with restoring a .bak file in order to copy the data to Azure data Lake. I do not have the option to restore the .bak file to local database and I am hoping it may be possible to restore the .bak file using the AzSql module or via an ADF pipeline using the RESTORE DATABASE from URL command where the .bak file is in Azure Storage account which may be accessed by the SQL managed instance.

Azure SQL Database
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Morillo 33,611 Reputation points MVP
    2022-09-09T15:59:59.04+00:00

    I don't know how to do it using CLI, but below you will find how to do it using T-SQL on SSMS while been connected to an Azure SQL Managed Instance.

    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'  
    

    You can also use PowerShell. You may need to install SQL Server PS Module.

    #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'"  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.