0% found this document useful (0 votes)
28 views3 pages

Differential Backup 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)
28 views3 pages

Differential Backup 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/ 3

drop table dba..

Differential_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

, convert(varchar, msdb.dbo.backupset.backup_finish_date, 103) 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..Differential_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..Differential_latest_backup_details


ADD backup_status VARCHAR(20)

go
declare cur cursor for select Latest_Backup_date,database_name from
dba..Differential_latest_backup_details where BackupType like 'DIFF%'
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 convert(varchar, getdate(), 103))
print @db14

update dba..Differential_latest_backup_details set backup_status='success'


where database_name =@db12 and Latest_Backup_date =@db14

update dba..Differential_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

select * from dba..Differential_latest_backup_details where BackupType like 'DIFF%'


--and database_name in('Dickies_BI','Dickies_BIVF' )

declare @var int


set @var=( select count(*) from dba..Differential_latest_backup_details where
BackupType like 'DIFF%' --and database_name in( 'Dickies_BI','Dickies_BIVF')
and server_name = @@servername and backup_status='failure')
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..Differential_latest_backup_details where BackupType like 'DIFF%' --and
database_name in( '')
and backup_status ='failure' order by Backup_status

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

SET @body ='<html><body><H3>Differential Backup report of fwgongcdb server</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
@subject = 'Differential Backup report of fwgongcdb server' ;
end
else if @var < 1
begin

SET @body ='<html><body><H3>ALL Differential Backups of fwgongcdb server are in


place.</H3>'
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]',--KSFT-VFC-
[email protected]', -- replace with your email address
@subject = 'Differential Backup report of fwgongcdb server' ;

end
--select * from dba..Differential_latest_backup_details

You might also like