HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Backup files are lost while backing up to Azure URL from SQL Server 2016

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
lostwhilearesqlbackingfilesazureserverfromurl

Problem

I have a job script like this which should backup SQL Server 2016 dbs to Azure blob storage with storage key credential. Usually it works fine, but sometimes a few backups are lost (I mean there is no backup file on the storage account) and I do not get any kind or job failure or errors on job log file. Dbs are just skipped. I have noticed that when backup job overlaps maintenance procedures this happens quite often when I have changed times it happened once a month or less. But I am not sure that maintenance(index maint, dbcc, stats update) is the reason for this anomaly.

I would like to know whether had you any prior experience like this and may know what is the core reason?

The interesting part is that I have restore verifyonly which also just skips databases. It does not try to restore those dbs that's why I do not get fail errors, just skips

```
DECLARE @dbname sysname
DECLARE @path nvarchar(120)
DECLARE @credential sysname = 'BackupStorageCredential'
DECLARE @date nvarchar(250) = CAST( GETDATE() AS Date )
SET @path = N'[my_storage_url]'

DECLARE db_cursor CURSOR FOR
SELECT name FROM sys.databases
WHERE name IN ('db1','db2','db3')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @query_backupToAzBLOB NVARCHAR(max)
DECLARE @query_verify NVARCHAR(max)

SET @query_backupTOAzBLOB = 'BACKUP DATABASE [' + @dbname + '] TO URL =''' + @path + @dbname + '/' + @dbname + '_' + @date +'.bak''
WITH CREDENTIAL = ''' + @credential + ''',NOFORMAT, NOINIT, NAME =''' + @dbname + ''',
NOSKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM'
EXEC (@query_backupTOAzBLOB)
SET @query_verify = 'RESTORE VERIFYONLY FROM URL =''' + @path + @dbname + '/' + @dbname + '_' + @date +'.bak''
WITH CREDENTIAL = ''' + @credential + ''', FILE = 1, NOUNLOAD, STATS = 5'
EXEC(@query_verify)
FETCH NEXT FROM db_cur

Solution

Declaring a cursor over sys.databases is a classic. Sometimes, for whatever reason, some databases are skipped.

Change the cursor type to STATIC and you will likely see that this doesn't happen. To be certain that this is the reason, log to a table of yours using an INSERT inside the cursor to be absolutely certain that the database name was returned (or not) from the cursor query. I.e., rule out all backup related question marks.

While at it, you might want to add a condition for the database to be online. Here's an example from my sp_dbinfo procedure:

DECLARE db CURSOR STATIC FOR SELECT name FROM sys.databases WHERE state_desc = 'ONLINE'

Code Snippets

DECLARE db CURSOR STATIC FOR SELECT name FROM sys.databases WHERE state_desc = 'ONLINE'

Context

StackExchange Database Administrators Q#305465, answer score: 4

Revisions (0)

No revisions yet.