Search code examples
azure-blob-storagerestoresql-server-2017

Not able to restore databases from blob container using T-SQL


I am backing up my databases to an Azure blob storage. I am able to backup and restore from maintenance plan. However I am not able to restore databases with script. Below is the T-SQL I am using:

RESTORE DATABASE database_name
FROM URL = 'https://StorageAccount.blob.core/Container/FileName.bak'
WITH CREDENTIAL   = 'https://StorageAccount.blob.core.windows.net/Container', STATS = 10

I am getting this error:

Msg 3225, Level 16, State 1, Line 1
Use of WITH CREDENTIAL syntax is not valid for credentials containing a Shared Access Signature.

Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Can you please assist?


Solution

  • You credential is wrong, please follow this link to create a credential.

    CREATE CREDENTIAL mycredential   
    WITH IDENTITY= 'msftutorialstorage', -- this is the name of the storage account you specified when creating a storage account   
    SECRET = '<storage account access key>' -- this should be either the Primary or Secondary Access Key for the storage account
    

    Then in your restore code, use this credential like below:

    RESTORE DATABASE AdventureWorks2016 
    FROM URL = 'https://msftutorialstorage.blob.core.windows.net/sql-backup/AdventureWorks2016.bak' 
    WITH CREDENTIAL = 'mycredential',
    STATS = 5 -- use this to see monitor the progress
    GO