Skip to content

Instantly share code, notes, and snippets.

@deltacodepl
Forked from scheffler/ola-sql-backup-to-url.md
Created August 25, 2024 17:19
Show Gist options
  • Save deltacodepl/42ed68c3df798f1ba68180ee44f3da84 to your computer and use it in GitHub Desktop.
Save deltacodepl/42ed68c3df798f1ba68180ee44f3da84 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!

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