Technical Article

Display Databases with X days without backup

,

This script helps you to know wich databases have not been backed up within the last X days. You can also see wich databases have never been backed up.

You must run like:

EXEC msdb.dbo.get_backup_info -X

where X are the days ago (0 gives all the information).

This is what is shown:

USE [msdb]
GO

/****** Object:  StoredProcedure [dbo].[get_backup_info]    Script Date: 30/08/2013 12:51:28 ******/SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[get_backup_info]  @daysago int 
AS

-- Displays the information of the date of the latest backup that was done back to @daysago. When value is -0 it means from today
-- Display databases that never have been backed up (value fecha = NULL).
-- Example: EXEC msdb.dbo.get_backup_info -0

SET NOCOUNT ON

SELECT BS.database_name AS 'Data_bases', BS.backup_start_date AS 'Backupstartat',BS.backup_finish_date AS 'Backuptakenat', 
BMF.physical_device_name AS 'FullBackupLocation',
CONVERT(INT,ROUND (BS.backup_size/(1024*1024),0)) AS 'BackupSize'
 INTO #FullBack
  FROMmsdb..backupmediafamily BMF
 JOIN msdb..backupmediaset BMS ON BMF.media_set_id = BMS.media_set_id
  JOIN msdb.dbo.backupset BS ON BS.media_set_id = BMS.media_set_id
 JOIN master.dbo.sysdatabases SDB ON SDB.name = BS.database_name
  WHERE BS.backup_set_id = (SELECT MAX(SBS.backup_set_id) FROM  msdb.dbo.backupset SBS
WHERE SBS.database_name = BS.database_name AND SBS.type = 'D' 
AND SBS.database_name NOT IN ('tempdb','pubs','AdventureWorks','AdventureWorks2012','Northwind')) 
SELECT name as Database_Name,
 Backupstartat,
  Backuptakenat,
  BackupSize, 
  DATEDIFF(hour,Backupstartat,Backuptakenat) as 'Duracion',
  case 
  when FullBackupLocation is null THEN 'BACKUP NEVER DONE'
  ELSE
  FullBackupLocation
  end
  as 'Destino'
   FROM #FullBack RIGHT OUTER JOIN master..sysdatabases 
ON name = Data_bases
WHERE ((name NOT IN ('tempdb','pubs','AdventureWorks','AdventureWorks2012','Northwind')) AND (Backuptakenat < DATEADD(DAY, @daysago, (getdate()))) or (Backuptakenat is null and (name NOT IN ('tempdb','pubs','AdventureWorks','AdventureWorks2012','Northwind')))) and CONVERT(sysname,DatabasePropertyEx([name],'Status'))  = 'ONLINE'
ORDER BY Backuptakenat DESC
drop table #FullBack

GO

Rate

3.67 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

3.67 (3)

You rated this post out of 5. Change rating