When the last backup happend at Database Level in SQL Server

;WITH CTE_Backup AS
(
SELECT  database_name,backup_start_date,type,physical_device_name,has_backup_checksums
       ,Row_Number() OVER(PARTITION BY database_name,BS.type
        ORDER BY
backup_start_date DESC) AS RowNum
FROM    msdb..backupset BS
JOIN    msdb.dbo.backupmediafamily BMF
ON      BS.media_set_id=BMF.media_set_id
)
SELECT D.name,
cte.has_backup_checksums
           ,ISNULL(CONVERT(VARCHAR,backup_start_date),'No backups') AS last_backup_time
           ,D.recovery_model_desc
           ,state_desc,
            CASE WHEN type ='D' THEN 'Full database'
            WHEN type ='I' THEN 'Differential database'
database'
database'
            WHEN type ='L' THEN 'Log'
            WHEN type ='F' THEN 'File or filegroup'
filegroup'
filegroup'
            WHEN type ='G' THEN 'Differential file'
file'
file'
            WHEN type ='P' THEN 'Partial'
            WHEN type ='Q' THEN 'Differential partial'
partial'
partial'
            ELSE 'Unknown' END AS backup_type
           ,physical_device_name
FROM        sys.databases D
LEFT JOIN   CTE_Backup CTE
ON          D.name = CTE.database_name
AND         RowNum = 1 and
cte.has_backup_checksums=1
ORDER BY    D.name,type
;WITH CTE_Backup AS
(
SELECT  database_name,backup_start_date,type,physical_device_name,has_backup_checksums
       ,Row_Number() OVER(PARTITION BY database_name,BS.type
        ORDER BY
backup_start_date DESC) AS RowNum
FROM    msdb..backupset BS
JOIN    msdb.dbo.backupmediafamily BMF
ON      BS.media_set_id=BMF.media_set_id
)
SELECT D.name,
cte.has_backup_checksums
           ,ISNULL(CONVERT(VARCHAR,backup_start_date),'No backups') AS last_backup_time
           ,D.recovery_model_desc
           ,state_desc,
            CASE WHEN type ='D' THEN 'Full database'
            WHEN type ='I' THEN 'Differential
database'

            WHEN type ='L' THEN 'Log'
            WHEN type ='F' THEN 'File or
filegroup'

            WHEN type ='G' THEN 'Differential
file'

            WHEN type ='P' THEN 'Partial'
            WHEN type ='Q' THEN 'Differential
partial'

            ELSE 'Unknown' END AS backup_type
           ,physical_device_name
FROM        sys.databases D
LEFT JOIN   CTE_Backup CTE
ON          D.name = CTE.database_name
AND         RowNum = 1 and
cte.has_backup_checksums=1 
ORDER BY    D.name,type
The below query will give information on When the Azure SQL Database backup happened on that particular database. Before you run this query go that specific database and run.
--Select the databse before you run this command
SELECT * FROM sys.databases
WHERE Physical_database_name = 'valuehere;
GO
Select * from sys.dm_database_backups 
where physical_database_name='valuehere;
ORDER BY backup_finish_date DESC
The below query will give us a more explanation.
SELECT bs.database_name,
	backuptype = CASE
			WHEN bs.type = 'D'
			AND bs.is_copy_only = 0 THEN 'Full Database'
			WHEN bs.type = 'D'
			AND bs.is_copy_only = 1 THEN 'Full Copy-Only Database'
			WHEN bs.type = 'I' THEN 'Differential database backup'
			WHEN bs.type = 'L' THEN 'Transaction Log'
			WHEN bs.type = 'F' THEN 'File or filegroup'
			WHEN bs.type = 'G' THEN 'Differential file'
			WHEN bs.type = 'P' THEN 'Partial'
			WHEN bs.type = 'Q' THEN 'Differential partial'
		END + ' Backup',
	CASE bf.device_type
			WHEN 2 THEN 'Disk'
			WHEN 5 THEN 'Tape'
			WHEN 7 THEN 'Virtual device'
			WHEN 9 THEN 'Azure Storage'
			WHEN 105 THEN 'A permanent backup device'
			ELSE 'Other Device'
		END AS DeviceType,
	bms.software_name AS backup_software,
	bs.recovery_model,
	bs.compatibility_level,
	BackupStartDate = bs.Backup_Start_Date,
	BackupFinishDate = bs.Backup_Finish_Date,
	LatestBackupLocation = bf.physical_device_name,
	backup_size_mb = CONVERT(decimal(10, 2), bs.backup_size/1024./1024.),
	compressed_backup_size_mb = CONVERT(decimal(10, 2), bs.compressed_backup_size/1024./1024.),
	database_backup_lsn, -- For tlog and differential backups, this is the checkpoint_lsn of the FULL backup it is based on.
	checkpoint_lsn,
	begins_log_chain,
	bms.is_password_protected,
	bms.is_compressed
FROM msdb.dbo.backupset bs
LEFT OUTER JOIN msdb.dbo.backupmediafamily bf ON bs.[media_set_id] = bf.[media_set_id]
INNER JOIN msdb.dbo.backupmediaset bms ON bs.[media_set_id] = bms.[media_set_id]
WHERE bs.backup_start_date > DATEADD(MONTH, -2, sysdatetime()) --only look at last two months
ORDER BY bs.database_name ASC, bs.Backup_Start_Date DESC;


Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server