0% found this document useful (0 votes)
15 views5 pages

FULL Backup Failure Reports

Uploaded by

sunindfra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

FULL Backup Failure Reports

Uploaded by

sunindfra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

drop table dba..

full_latest_backup_details
-- Query to find latest "Full" as well as "T-Log" backup of all specified Databases
-- Query to find latest "Full" as well as "T-Log" backup of all specified Databases

USE DBA
GO

SELECT msdb.dbo.backupset.server_name

,msdb.dbo.backupset.database_name

,CASE msdb.dbo.backupset.[type] -- Let's decode the main types of backup here

WHEN 'D'

THEN 'Full'

WHEN 'I'

THEN 'Differential'

WHEN 'L'

THEN 'Transaction Log'

WHEN 'F'

THEN 'File or filegroup'

WHEN 'G'

THEN 'Differential file'

WHEN 'P'

THEN 'Partial'

WHEN 'Q'

THEN 'Differential partial'

ELSE msdb.dbo.backupset.[type]

END AS BackupType

, msdb.dbo.backupset.backup_finish_date as Latest_backup_Date

,CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date) AS INT)) / 3600 AS VARCHAR) + ' hours, ' +
CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date) AS INT)) / 60 AS VARCHAR) + ' minutes, ' +
CAST((CAST(DATEDIFF(s, msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date) AS INT)) % 60 AS VARCHAR) + ' seconds' AS
[Total_Time_Taken_For_Backup]

,msdb.dbo.backupmediafamily.physical_device_name AS Backup_File_Location

into dba..full_latest_backup_details
FROM msdb.dbo.backupmediafamily

INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id =


msdb.dbo.backupset.media_set_id

WHERE (

backup_set_ID IN (

SELECT MAX(backup_set_id)

FROM msdb.dbo.backupset

WHERE msdb.dbo.backupset.[type] = 'D'

GROUP BY database_name

OR backup_set_ID IN (

SELECT MAX(backup_set_id)

FROM msdb.dbo.backupset

WHERE msdb.dbo.backupset.[type] = 'L'

GROUP BY database_name

) OR

backup_set_ID IN (

SELECT MAX(backup_set_id)

FROM msdb.dbo.backupset

WHERE msdb.dbo.backupset.[type] = 'I'

GROUP BY database_name

ORDER BY msdb.dbo.backupset.database_name

,BackupType

GO

ALTER TABLE dba..full_latest_backup_details

ADD backup_status VARCHAR(20)

go
declare cur1 cursor for SELECT name FROM sys.databases where is_read_only<>1

declare @db15 varchar(90)

open cur1

fetch next from cur1 into @db15

while @@fetch_status=0

begin

declare cur cursor for select Latest_Backup_date,database_name from


dba..full_latest_backup_details where BackupType like 'full%' and database_name
=@db15

declare @db13 varchar(90)

declare @db14 varchar(90)

declare @db12 varchar(90)

open cur

fetch next from cur into @db13,@db12

while @@fetch_status=0

begin

--declare @db14 date

set @db14=(select getdate()-6)

print @db14

update dba..full_latest_backup_details set


backup_status='success' where database_name =@db12 and Latest_Backup_date >=@db14
update dba..full_latest_backup_details set
backup_status='failure' where database_name =@db12 and Latest_Backup_date < @db14

fetch next from cur into @db13,@db12

end

close cur

deallocate cur

fetch next from cur1 into @db15

end

update
dba..full_latest_backup_details set backup_status='NA' where backup_status='NULL'

close cur1

deallocate cur1

select * from dba..full_latest_backup_details where BackupType like


'full%' and server_name = @@servername and backup_status='failure'-- and
database_name not in('')

--select * from dba..full_latest_backup_details where BackupType


like 'full%' and server_name = @@servername and backup_status='success'

declare @var int

set @var=( select count(*) from


dba..full_latest_backup_details where BackupType like 'full%' and server_name =
@@servername and backup_status='failure') --and database_name not in('Dickies)

if @var >= 1

begin

DECLARE @xml NVARCHAR(MAX)

DECLARE @body NVARCHAR(MAX)


SET @xml = CAST(( SELECT server_name AS 'td','',database_name AS 'td','',
BackupType AS 'td','', Latest_Backup_date AS 'td','', Total_Time_Taken_For_Backup
AS 'td','', Backup_File_Location AS 'td','', Backup_status AS 'td'

FROM dba..full_latest_backup_details where BackupType like '%full%' and server_name


= @@servername and backup_status ='failure' and database_name not
in('mosaic','Staging')

FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

SET @body ='<html><body><H3>*IMP* IMMEDIATE ACTION REQUIRED - SQL SERVER DATABASES


-FULL BACKUP FAILURE REPORT</H3>

<table border = 1>

<tr>

<th> server_name </th> <th> database_name </th> <th> BackupType </th> <th>
Latest_Backup_date </th><th> Total_Time_Taken_For_Backup </th><th>
Backup_File_Location </th><th> Backup_Status </th></tr>'

SET @body = @body + @xml +'</table></body></html>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name='DBA',

-- replace with your SQL Database Mail Profile

@body = @body,

@body_format ='HTML',

@recipients ='[email protected];[email protected]', -- replace with your


email address
[email protected]

@subject = '*IMP* IMMEDIATE ACTION REQUIRED - SQL SERVER DATABASES -FULL BACKUP
FAILURE REPORT ' ;

end

You might also like