-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbackup history oldest latest.sql
95 lines (91 loc) · 2.37 KB
/
backup history oldest latest.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
--sql2005 and above
select
backuptype
, recovery_model_desc
, state_desc
, is_read_only
, OldestLatestBackupDate = MIN(BackupDate)
FROM
(
select
database_name
, backuptype
, d.recovery_model_desc
, BackupDate = MAX(BackupDate)
, d.state_desc
, d.is_read_only
from sys.databases d
inner join
(
select distinct
database_name
, backuptype = case type WHEN 'D' then 'Database'
WHEN 'I' then 'Differential database'
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' END
, BackupDate = MAX(backup_start_date)
from msdb.dbo.backupset bs
group by database_name, type
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Database'
, null
FROM master.sys.databases d
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Transaction Log'
, null
FROM master.sys.databases d
where d.recovery_model_desc in ('FULL', 'BULK_LOGGED')
) a
on db_name(d.database_id) = a.database_name
WHERE backuptype = 'transaction log'
group by database_name, backuptype, d.recovery_model_desc, d.state_desc, d.is_read_only
) x
group by backuptype, recovery_model_desc, state_desc, is_read_only
order by backuptype, recovery_model_desc
GO
select
database_name
, backuptype
, d.recovery_model_desc
, BackupDate = MAX(BackupDate)
, d.state_desc
, d.is_read_only
from sys.databases d
inner join
(
select distinct
database_name
, backuptype = case type WHEN 'D' then 'Database'
WHEN 'I' then 'Differential database'
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' END
, BackupDate = MAX(backup_start_date)
from msdb.dbo.backupset bs
group by database_name, type
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Database'
, null
FROM master.sys.databases d
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Transaction Log'
, null
FROM master.sys.databases d
where d.recovery_model_desc in ('FULL', 'BULK_LOGGED')
) a
on db_name(d.database_id) = a.database_name
--WHERE backuptype = 'transaction log'
group by database_name, backuptype, d.recovery_model_desc, d.state_desc, d.is_read_only