Which scenario are you in?
Searches for "backup SQL database to Azure Blob Storage" mix up two different products, and the method depends entirely on which one you run.
- SQL Server (on-premises, or SQL Server on an Azure VM). You control the engine, so you can use native
BACKUP DATABASE ... TO URLand write.bakfiles straight into a blob container. This is the main subject of this guide. - Azure SQL Database (the PaaS service). It does not support
BACKUP TO URL. Microsoft already takes automated backups for you, you can keep them for years with long-term retention (LTR), and if you need a portable copy in your own storage account you export a.bacpac. That is covered in the second half.
Azure SQL Managed Instance sits in between: it supports full
COPY_ONLY BACKUP TO URL (T-SQL differences), but that is not the focus here.Part A: SQL Server backup to Azure Blob Storage
The overall flow is: create a container, give SQL Server a credential that can write to it, run
BACKUP ... TO URL for every user database, schedule it with SQL Server Agent, test a restore, and let a lifecycle policy delete old files. Microsoft's reference is SQL Server backup to URL for Azure Blob Storage.Step 1: Create the storage account and container
- In the Azure portal, create or open a general purpose v2 storage account.
- Go to Data storage > Containers and create a private container, for example
sql-backup. Container names must be lowercase, soSQL-backupwill be rejected. - Leave hierarchical namespace (Data Lake Storage) disabled: SQL Server backup to URL does not support Azure Data Lake Storage. You can still use "virtual folders" by putting slashes in the blob name, such as
sql-backup/server01/2026/10/.
Step 2: Create a SAS token for the container
Generate a SAS scoped to the container, not the whole account. Microsoft's backup to URL tutorial creates a stored access policy on the container with read, write, list and delete (
rwld) permissions and then issues a SAS tied to that policy. The Azure CLI equivalent:Because permissions and expiry live in the stored access policy, you can revoke or extend the token by editing or deleting the policy, without rotating the account key. Avoid a user delegation SAS (
--as-user --auth-mode login) for this job: its user delegation key is valid for at most seven days, so the credential would need weekly rotation. Record the policy expiry somewhere you will see it, because an expired SAS breaks every backup.Step 3: Create the credential in SQL Server
The credential name must be the container URL exactly, and the secret is the SAS token without the leading
?. This is a common mistake (and one that an earlier version of this post got wrong).When the SAS rotates, update it in place:
Managed identity option. Two setups can authenticate with a managed identity instead of a SAS, which removes secret rotation entirely:
- SQL Server on an Azure VM running SQL Server 2022 CU17 or later, registered with the SQL IaaS Agent extension and configured for Microsoft Entra authentication (backup and restore to URL using managed identities).
- SQL Server 2025 enabled by Azure Arc with a primary managed identity assigned, currently in preview (back up to URL with managed identity).
In both cases the primary managed identity needs the Storage Blob Data Contributor role on the storage account. Then create the credential with
IDENTITY = 'Managed Identity' and no secret:Step 4: Back up one database to URL
Test a single database before automating anything:
COMPRESSIONreduces the bytes sent over the network and stored in Azure.CHECKSUMvalidates pages as they are read and writes a backup checksum you can verify later.- With a SAS credential, SQL Server writes block blobs. A block blob holds at most 50,000 blocks, so with a 4 MB
MAXTRANSFERSIZEa single backup file tops out at about 195.3 GB (limitations of backup to URL). SQL Server can write smaller blocks and hit the limit sooner, so Microsoft advises striping even below 200 GB, especially for differential or uncompressed backups. UseMAXTRANSFERSIZE = 4194304andBLOCKSIZE = 65536, as in the best practices and troubleshooting example:
Backup to URL supports up to 64 URLs per backup, which gives a maximum of about 12.8 TB (64 stripes x 50,000 blocks x 4 MB). Log backups to multiple files ignore
MAXTRANSFERSIZE and use 64 KB, so stripe log backups only when you must.Step 5: Back up all user databases in a loop
This is the original script from this post with three fixes: it skips offline and snapshot databases, it adds
CHECKSUM, and it builds the timestamp without the CONVERT(NVARCHAR, ...) length default.If a server name contains a backslash (named instance), replace it before using it in the URL, for example
REPLACE(@@SERVERNAME, '\', '_'). Because the TRY...CATCH swallows errors, add a final check that raises an error if any database failed, so the Agent job reports failure. If your databases are in the full recovery model, also schedule BACKUP LOG ... TO URL every 15 to 60 minutes, otherwise point-in-time restore is impossible and the log will keep growing.For a production estate, Ola Hallengren's maintenance solution supports
@URL and @Credential parameters and handles logs, retries and cleanup, and is worth considering over a home-grown loop.Step 6: Schedule it with SQL Server Agent
- In SSMS, expand SQL Server Agent > Jobs, right-click and choose New Job.
- Name it, for example
Azure Blob DB Backup. - On Steps, add a step of type Transact-SQL script (T-SQL), database
master, and paste the script from Step 5 (or call it from a stored procedure). - On Schedules, create a recurring schedule, for example daily at 00:00. Add a second job for log backups if you need them.
- On Notifications, email an operator on failure. Database Mail must be configured for this.
On an Azure VM you can instead use Automated Backup, which configures Managed Backup to a storage account for SQL Server 2016 or later (Standard, Enterprise or Developer) registered with the SQL IaaS Agent extension, with no T-SQL. Its retention setting (1 to 90 days) only trims metadata in
msdb; the blob files are not deleted, so you still need the lifecycle rule in Step 8.Step 7: Verify and test a restore
A backup you have never restored is a guess. Check the backup history first:
Then validate and restore to a different name on a test server (which needs the same credential):
Use
RESTORE FILELISTONLY FROM URL = ... to get the logical file names for MOVE. For a striped backup, list every URL in the FROM clause.Step 8: Retention with lifecycle management
SQL Server does not delete old blobs. Add a lifecycle management rule (how to configure one) on the storage account to move backups to the Cool tier after a few days and delete them after your retention period, filtered by the
sql-backup/ prefix. This replaces the cleanup step you would otherwise script.Part B: Azure SQL Database backup to a storage account
Running
BACKUP DATABASE ... TO URL against Azure SQL Database fails, because the service manages backups for you. You have three options.Option 1: Automated backups (already on)
Azure SQL Database takes full, differential and log backups automatically and supports point-in-time restore with a retention of 1 to 35 days (7 by default; Basic tier allows 1 to 7), configurable per database. See Automated backups in Azure SQL Database. These are stored in Microsoft-managed storage, not your account, and you restore them through the portal, CLI or PowerShell.
Option 2: Long-term retention
If the actual requirement is "keep monthly backups for seven years", use long-term retention rather than exporting files yourself. LTR keeps weekly, monthly and yearly full backups for up to 10 years:
Option 3: Export a .bacpac to your storage account
When you need a portable file in a container you own (for audits, moving off Azure, or seeding another environment), export a
.bacpac. A bacpac is a logical export of schema and data. It is only transactionally consistent if no writes happen during the export, so export from a database copy. Exports to Blob Storage are limited to 200 GB per bacpac; for larger databases export locally with SqlPackage. See Export to a BACPAC file.Portal: open the database, choose Export, pick the storage account and container, and enter SQL admin credentials.
Azure CLI (az sql db export):
--storage-key-type also accepts SharedAccessKey (then --storage-key is a SAS token, shown with a leading ? in the CLI reference example) and ManagedIdentity.SqlPackage runs the export from a machine you control, then you upload the file:
Scheduling an export. Azure SQL Database has no built-in export schedule. Common choices are an Azure Automation runbook calling
New-AzSqlDatabaseExport, a Logic App on a recurrence trigger, or a scheduled GitHub Actions workflow running the CLI command above with credentials stored as secrets. The secret handling pattern in automating Azure App Service deployments with GitHub Actions and secure secrets applies directly. Store passwords and keys in Key Vault or pipeline secrets, never in the script.Security checklist
- Scope the SAS to one container with only
rwdl, HTTPS only, and a defined expiry tied to a stored access policy so you can revoke it. - Prefer managed identity where your SQL Server version supports it, so there is no secret to leak or expire.
- Private endpoint: disable public network access on the storage account and reach it over a private endpoint for
blob. Backups will then fail with name resolution or 403 errors if private DNS is wrong; see private endpoints on Azure and the DNS gotchas. - Immutability: enable immutable storage (time-based retention) and soft delete on the container so ransomware or a compromised SAS cannot delete backups. Test this first, as immutability policies can block overwrites the backup job expects.
- Encrypt backups with
ENCRYPTION (ALGORITHM = AES_256, SERVER CERTIFICATE = ...)if they contain sensitive data, and back up the certificate separately.
Troubleshooting
These causes are drawn from Microsoft's best practices and troubleshooting page.
- Operating system error 50, "The request is not supported": the SAS token starts with
?, its stored access policy has expired, the account is not general purpose v1 or v2, the machine cannot reach the storage account, or the Azure root certificate is missing from the Trusted Root store. Also check that the credential name matches the container URL exactly. - Backup fails because the blob already exists: BACKUP will not overwrite an existing blob unless you add
WITH FORMAT. Use a unique file name per run (the timestamp in Step 5 handles this). On immutable storage,WITH FORMATfails too. - Write failed: 1117 (I/O device error) on large databases: typically the 50,000 block limit per blob. Use
COMPRESSION, stripe across multiple URLs and setMAXTRANSFERSIZE = 4194304, BLOCKSIZE = 65536. - (409) Conflict on restore: the blob has an active lease left by a failed backup. Delete the leased blob files.
- Timeouts or slow backups: network throughput or proxy settings. Check outbound connectivity to
*.blob.core.windows.neton port 443, and add stripes to parallelize. - Azure SQL Database "statement not supported": you tried
BACKUP TO URLon the PaaS service; use Part B. - Bacpac export to Blob Storage fails on a large database: the blob export limit is 200 GB; export to local storage with SqlPackage instead.
FAQ
Can I back up Azure SQL Database to Blob Storage with T-SQL?
No. Azure SQL Database does not support
BACKUP TO URL. Use automated backups, long-term retention, or export a .bacpac to your storage account.Should the SAS secret include the question mark?
No. For
CREATE CREDENTIAL, paste the SAS token without the leading ?. The az sql db export reference example with SharedAccessKey does include it.Block blob or page blob?
Use block blobs with a SAS (or managed identity) credential. Page blob backups use the storage account key credential and are capped at 1 TB.
How do I delete old backups automatically?
Use an Azure Storage lifecycle management rule filtered to the backup container prefix. SQL Server Agent cleanup tasks do not delete blobs.
