In the comments here I was asked about using Restore-DbaDatabase when all you have is blobs in an Azure Storage account. This can be done, involves a couple of non dbatools steps.
Restore-DbaDatabase doesn’t natively ‘talk’ Azure, nor do any of the other dbatools commands. The reason for this is that we didn’t want to force dbatools users to have to install the AzureRM/Az powershell modules just to use our module. We’ve gone to a lot of effort to make sure that dbatools is acceptable to Security Admins and that it has a small(ish) footprint, and adding those large modules as prerequisites would have broken that.
In this example I’m going to be using the Az module as that’s the current recommend Azure module, and the one I’ve got to hand.
To follow through this you’re going to need to have already set up the restore target instance to talk to the Azure Blob Storage. Full instructions for that are here:
- Creating Azure Blob storage account for SQL Server backup and restore with PowerShell
- Creating Azure Blob storage account for SQL Server backup and restore via the portal
depending on your preference. For the examples here I’m assuming that you’re using the SAS authentication method (as it’s the current recommendation)
Once that’s setup we move into PowerShell to get the blob information we’re going to need.
Import-Module Az
Import-Module dbatools
Login-AzAccount
Set-AzContext -Context 'MyStorage Subscription'
Start off by importing the modules we’re going to be using. Then login to Azure and set the current subscription/context to the one holding the storage account we’re going to be querying
$account = Get-AzStorageAccount -ResourceGroupName sqlbackups -Name sqlbackups
$context = $acccount.Context
$blobs = Get-AzStorageBlob -Context $context -Container sqlbackup
$filteredblobs = ($blobs | Where-Object{$_.LastModified -gt (Get-Date).AddDays(-1)}).ICloudBlob.Uri.AbsoluteUri
We start by getting the storage account details, here they’re in a storage account called sqlbackups
which lives in a resource group named sqlbackups
.
Grab all the blobs in the specified container. We do the filtering in the next step as Get-AzStorageBlob
doesn’t have any filtering other than -Prefix
which is of almost zero use in this case
Here we’re just filtering for all the blobs touched in the last 24 hours. You can filter for other things, but remember that there are no ‘folders’ below the containers, just /’s in the name. So if you’re trying to filter for something like Ola’s paths for a known database full backup you’ll need to use something like this (using master as an example)
$filteredblobs = ($blobs | Where-Object{$_.name -like '*master*FULL*).ICloudBlob.Uri.AbsoluteUri
All we need for the restore is the full URI (yes, it’s a URL, but technically it’s a URI
Now we can pipe the filtered blobs into Restore-DbaDatabase and things will work as normal
$filteredblobs | Restore-DbaDatabase -SqlInstance sql1 .............
So it’s a little more work that a traditional disk based restore, but not too much.
1 Pingback