Sadly, backupset
does not contain failed backups, and I don't know of anywhere else in msdb
these may be stored, unless you can rely on sysjobhistory
, which doesn't contain all of time (depending on your retention settings), and which would ignore any backup attempts that were made outside the context of a job, and which - in the case of a job that backs up many databases - would not provide differentiation about which database actually failed, unless it happened to happen early on in the job - this is because the messaging is quite verbose but gets truncated.
If you absolutely know that Job n
only backs up the one database, and that every failure of that job means that the database wasn't backed up (since the job could also fail after the backup succeeded, e.g. trying to shrink or perform other maintenance), then you could use a query like this:
DECLARE @job sysname, @db sysname;
SELECT @job = N'Job 1', @db = N'db_name';
SELECT
bs.database_name,
bs.backup_start_date,
bs.backup_finish_date,
[Total Time] = CAST((DATEDIFF(SECOND, bs.backup_start_date,bs.backup_finish_date))
AS varchar(30))+ ' secs',
CAST(bs.backup_size/1024/1024 AS decimal(10,2)) AS 'Backup Size(MB)',
h.[message]
FROM msdb.dbo.sysjobhistory AS h
INNER JOIN msdb.dbo.sysjobs AS j
ON h.job_id = j.job_id
AND h.step_id = 0
LEFT OUTER JOIN msdb.dbo.backupset AS bs
ON bs.database_name = @db
AND
ABS(DATEDIFF(SECOND, bs.backup_start_date, CONVERT(DATETIME,convert(char(8),h.run_date)
+ ' ' + STUFF(STUFF(RIGHT('0'+CONVERT(char(6),h.run_time),6),3,0,':'),6,0,':')))) < 5
WHERE j.name = @job
ORDER BY bs.backup_start_date;
Yes, it's really ugly, because sysjobhistory
still, in SQL Server 2014 even, stores run_date
and run_time
as separate integers. I bet whoever made that decision is still on the background of dartboards all over building 35. It also assumes that the backup is the very first step in the job, hence the rather less than scientific date/time comparison to make sure we've properly correlated the right instance of the job to the right instance of the backup. Oh, how I wish I could redesign the schema for backups and jobs.
If you want broader scope outside of the job, you can look for failed backups in the SQL Server error log (if they haven't been cycled away):
EXEC sp_readerrorlog 0, 1, 'BACKUP failed'; -- current
EXEC sp_readerrorlog 1, 1, 'BACKUP failed'; -- .1 (previous)
EXEC sp_readerrorlog 2, 1, 'BACKUP failed'; -- .2 (the one before that)
....
(But I don't know of a nice and easy way to incorporate that output into your existing query.)
You can also correlate "missing" successful backups from the default trace, e.g.
DECLARE @path nvarchar(260);
SELECT
@path = REVERSE(SUBSTRING(REVERSE([path]),
CHARINDEX(CHAR(92), REVERSE([path])), 260)) + N'log.trc'
FROM sys.traces
WHERE is_default = 1;
SELECT dt.DatabaseName, dt.StartTime, bs.backup_start_date, bs.backup_finish_date,
[Status] = CASE WHEN bs.backup_start_date IS NULL
THEN 'Probably failed'
ELSE 'Seems like success'
END
FROM sys.fn_trace_gettable(@path, DEFAULT) AS dt
LEFT OUTER JOIN msdb.dbo.backupset AS bs
ON dt.DatabaseName = bs.database_name
AND ABS(DATEDIFF(SECOND, dt.StartTime, bs.backup_start_date)) < 5
WHERE dt.EventClass = 115 -- backup/restore events
AND UPPER(CONVERT(nvarchar(max),dt.TextData)) LIKE N'BACKUP%DATABASE%'
--AND dt.DatabaseName = N'db_name' -- to filter to a single database
--AND bs.database_name = N'db_name'
ORDER BY dt.StartTime;
Of course this also relies on the data from the default trace cycling away, the database name not having changed, etc. And unfortunately, the default trace doesn't differentiate between successful and failed backups, and the start time will not precisely match the MSDB data, but as long as you're not running backups in a loop, this should be okay for eyeballing. I've tried to incorporate these issues into the query.
Finally, you may want to use a FULL OUTER JOIN
there, in case backupset has longer history than the default trace. This changes the semantics of [Status]
slightly.
You also might want to give this nasty thing a try, though I didn't have much luck with it. I was only able to see the current or most recent status, so that only helped when the job failed the last time it ran, and - like sysjobhistory
- wasn't able to obtain information about any backups that were attempted but not through a job.
Does your backup file contain multiple backups (known as backup sets), and are you restoring just the earliest backup set from the backup file?
To see the backup sets in a file run the following and note the Position
column, which denotes the backup set:
RESTORE HEADERONLY FROM DISK='mybackup.bak'
To restore a specific backup set from a backup:
RESTORE DATABASE ... WITH FILE = <position from previous statement>
Why does this happen?
If you use the same filename every time in your backup script (e.g. BACKUP DATABASE MyDb TO DISK='backup.bak'
), and that file is not deleted or changed between backups, then your backup is added to the backup sets in that file. If you then run RESTORE DATABASE ...
without a WITH FILE=<position>
clause, you will always restore the first backup set in the backup media - usually your earliest one.
The details:
The RESTORE HEADERONLY
command is useful for a backup file (if you are looking at it on a different server to the one it was created on). You can also see the backups on the server that created them by running the command:
SELECT backup_set_id, media_set_id, position, backup_finish_date
FROM msdb.dbo.backupset
If you want to check which backup set you just restored from a multi-file media then you can run:
SELECT * FROM msdb.dbo.restorehistory
Note the backup_set_id
which corresponds to the msdb backupset
table and in turn to the media file and position within that file.
References:
From MSDN: RESTORE Arguments (Transact-SQL):
Backup Set Options
FILE ={ backup_set_file_number | @backup_set_file_number }
...
When not specified, the default is 1.
Best Answer
"It just does". Its just meta data about the backup.
The Books Online article http://msdn.microsoft.com/en-us/library/ms186299.aspx hints at it, albeit not very well in the "Remarks" section. From that article you'll see a reference to sp_delete_backuphistory. So yes you can safely remove it if you need to.
The info that goes into that backupset table, from a restore, is pulled out of the backup itself. Run:
and you'll find the information.