Skip to content

Instantly share code, notes, and snippets.

@scheffler
Last active January 4, 2025 01:06
Show Gist options
  • Save scheffler/7edd40f430235aab651fadcc7d191a89 to your computer and use it in GitHub Desktop.
Save scheffler/7edd40f430235aab651fadcc7d191a89 to your computer and use it in GitHub Desktop.
How to backup a SQL database to an Azure URL using Ola Hallengren's stored procedures

Description

How to backup a SQL database to an AZURE storage account using the Ola Hallengren scripts.

Requires the stored procedures from: https://ola.hallengren.com/sql-server-backup.html

Create an azure storage account

First, create an azure storage account with block blob support (v2 will suffice) and then create a blob container within that. For this example we'll call it sqlbackup.

Once created, generate an SAS token which allows full read/write access to the container and copy off the url. The url will have two parts and should look like this:

https://<storagename>.blob.core.windows.net/?sv=reallylongtokenstring

Setup credentials in SQL Server

The next step is to store the SAS credentails in SQL Server so that it can be used by the backup command. To do this, open a query window on the master db and run the following sql (with <> substitutions):

IF NOT EXISTS  
(SELECT * FROM sys.credentials   
WHERE name = 'https://<storageaccount>.blob.core.windows.net/<containername>')  
CREATE CREDENTIAL [https://<storageaccount>.blob.core.windows.net/<containername>] 
   WITH IDENTITY = 'SHARED ACCESS SIGNATURE',  
   SECRET = '<token string starting with sv>';  

Create a SQL Agent Backup Job

Create a new SQL Agent job which references the DatabaseBackup stored proc like so:

EXECUTE [dbo].[DatabaseBackup]
@Databases = 'USER_DATABASES',
@BackupType = 'FULL',
@Compress='Y',
@Url='https://<storageaccount>.blob.core.windows.net/<containername>',
@BlockSize=65536,
@MaxTransferSize=4194304

The key to this working is that the Url must match the SQL Server credential name created above.

Also, the BlockSize and MaxTransferSize settings are import for working with large dbs. See here for more guidance: https://blogs.msdn.microsoft.com/sqlcat/2017/03/10/backing-up-a-vldb-to-azure-blob-storage/

With that, the backups should start flowing!

@jchandler1985
Copy link

Thanks @scheffler, very useful! Directed me down the right path.

If you are backing up to Azure Storage Account from a SQL Managed Instance and want to use the Managed Instance's managed identity you need to create the credential like so:

CREATE CREDENTIAL [https://<mystorageaccountname>.blob.core.windows.net/<containername>] 
WITH IDENTITY = 'MANAGED IDENTITY'  

Permission the managed identity on the storage account with RBAC rules.

You will also need to add this additional predicate to the following where clause in the dbo.DatabaseBackup stored procedure:

OR UPPER(credential_identity) = 'MANAGED IDENTITY'

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment