Skip to content

Commit 4c35dc3

Browse files
author
Ajay Dwivedi
committed
Adding Restore Queries
Adding Restore Queries
1 parent 8155972 commit 4c35dc3

14 files changed

+624
-169
lines changed
3 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
DECLARE @c_database_name VARCHAR(125);
2+
DECLARE @c_physical_device_name VARCHAR(1000);
3+
declare @sqlRestoreText varchar(max);
4+
declare @p_Target_Data_Path varchar(255) = 'W:\MSSQLData\Data\';
5+
declare @p_Target_Log_Path varchar(255) = 'E:\MSSQLData\Logs\';
6+
7+
DECLARE cur_Backups CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
8+
WITH T_bkpHistory as
9+
(
10+
SELECT --ROW_NUMBER()over(partition by bs.database_name order by bs.backup_finish_date desc) as RowID,
11+
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SERVER
12+
,bs.database_name
13+
,bs.backup_start_date
14+
,bs.backup_finish_date
15+
,bs.expiration_date
16+
,CASE bs.type
17+
WHEN 'D'
18+
THEN 'Database'
19+
WHEN 'L'
20+
THEN 'Log'
21+
END AS backup_type
22+
,convert(decimal(18,3),(bs.backup_size)/1024/1024) as backup_size_MB
23+
,convert(decimal(18,3),(bs.backup_size)/1024/1024/1024) as backup_size_GB
24+
,bmf.logical_device_name
25+
,bmf.physical_device_name
26+
,bs.NAME AS backupset_name
27+
,bs.description
28+
,first_lsn
29+
,last_lsn
30+
,checkpoint_lsn
31+
,database_backup_lsn
32+
,is_copy_only
33+
FROM msdb.dbo.backupmediafamily AS bmf
34+
INNER JOIN msdb.dbo.backupset AS bs ON bmf.media_set_id = bs.media_set_id
35+
WHERE bs.backup_start_date >= (select max(bsi.backup_start_date) FROM msdb.dbo.backupmediafamily AS bmfi INNER JOIN msdb.dbo.backupset AS bsi ON bmfi.media_set_id = bsi.media_set_id where bsi.database_name = bs.database_name and bsi.type = 'D' --and bmfi.physical_device_name not like '\\Ann1vespdb01\%'
36+
)
37+
and bs.type = 'D' --and bmf.physical_device_name not like '\\Ann1vespdb01\%'
38+
)
39+
select database_name, physical_device_name
40+
from T_bkpHistory
41+
where database_name in ('AMGExtra','AMGMusic','AMGMusicMore')
42+
--(database_name not like 'RoviMusicShipping_EU_19%' and database_name not like 'RoviMusicShipping_EU_18%' AND database_name <> 'RoviMusicShipping_Archive'
43+
-- AND database_name not in ('master','model','msdb','tempdb'))
44+
ORDER BY backup_start_date;
45+
46+
OPEN cur_Backups;
47+
48+
FETCH NEXT FROM cur_Backups INTO @c_database_name, @c_physical_device_name;
49+
50+
WHILE @@FETCH_STATUS = 0
51+
BEGIN
52+
--PRINT '@c_database_name = '+@c_database_name+CHAR(13)+CHAR(10)+'@c_physical_device_name = '+@c_physical_device_name;
53+
set @sqlRestoreText = '
54+
RESTORE DATABASE '+QUOTENAME(@c_database_name)+' FROM DISK = N'''+( case when charindex(':',@c_physical_device_name)<>0 then '\\'+@@SERVERNAME+'\'+REPLACE(@c_physical_device_name,':','$') else @c_physical_device_name end )+'''
55+
WITH RECOVERY
56+
,STATS = 3
57+
,REPLACE
58+
';
59+
60+
select @sqlRestoreText += --name, physical_name,
61+
' ,MOVE N'''+name+''' TO N'''+(case when mf.type_desc = 'ROWS' then @p_Target_Data_Path ELSE @p_Target_Log_Path END )+ RIGHT(mf.physical_name,CHARINDEX('\',REVERSE(mf.physical_name))-1) +'''
62+
'
63+
from sys.master_files as mf
64+
where mf.database_id = DB_ID(@c_database_name);
65+
66+
SET @sqlRestoreText += '
67+
GO'
68+
69+
PRINT @sqlRestoreText;
70+
71+
FETCH NEXT FROM cur_Backups INTO @c_database_name, @c_physical_device_name;
72+
END

Backup-Restore/Backup-Restore.ssmssqlproj

+36-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
1616
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
1717
</ConnectionNode>
18+
<ConnectionNode Name="Ann1vesPDb01:CORPORATE\adwivedi">
19+
<Created>2019-04-25T12:55:36.3978942+05:30</Created>
20+
<Type>SQL</Type>
21+
<Server>Ann1vesPDb01</Server>
22+
<UserName />
23+
<Authentication>Windows Authentication</Authentication>
24+
<InitialDB>master</InitialDB>
25+
<LoginTimeout>30</LoginTimeout>
26+
<ExecutionTimeout>0</ExecutionTimeout>
27+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
28+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
29+
</ConnectionNode>
1830
<ConnectionNode Name="BAN-1ADWIVEDI-L:CORPORATE\adwivedi">
1931
<Created>2018-09-19T09:55:27.5891753+05:30</Created>
2032
<Type>SQL</Type>
@@ -87,6 +99,18 @@
8799
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
88100
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
89101
</ConnectionNode>
102+
<ConnectionNode Name="tul1cipcnpdb1:CORPORATE\adwivedi">
103+
<Created>2019-04-25T11:34:00.6832305+05:30</Created>
104+
<Type>SQL</Type>
105+
<Server>tul1cipcnpdb1</Server>
106+
<UserName />
107+
<Authentication>Windows Authentication</Authentication>
108+
<InitialDB />
109+
<LoginTimeout>30</LoginTimeout>
110+
<ExecutionTimeout>0</ExecutionTimeout>
111+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
112+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
113+
</ConnectionNode>
90114
</Items>
91115
</LogicalFolder>
92116
<LogicalFolder Name="Queries" Type="0" Sorted="true">
@@ -103,11 +127,17 @@
103127
<AssociatedConnUserName />
104128
<FullPath>2) Script Out DB Permissions in case of DB Refresh.sql</FullPath>
105129
</FileNode>
106-
<FileNode Name="3) Generate - Restore Script.sql">
107-
<AssociatedConnectionMoniker />
108-
<AssociatedConnSrvName />
130+
<FileNode Name="3) Generate - Restore Script - BackupHistory.sql">
131+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
132+
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
109133
<AssociatedConnUserName />
110-
<FullPath>3) Generate - Restore Script.sql</FullPath>
134+
<FullPath>3) Generate - Restore Script - BackupHistory.sql</FullPath>
135+
</FileNode>
136+
<FileNode Name="3) Generate - Restore Script - DiskFiles.sql">
137+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:Ann1vesPDb01:True</AssociatedConnectionMoniker>
138+
<AssociatedConnSrvName>Ann1vesPDb01</AssociatedConnSrvName>
139+
<AssociatedConnUserName />
140+
<FullPath>3) Generate - Restore Script - DiskFiles.sql</FullPath>
111141
</FileNode>
112142
<FileNode Name="4) Script Out DB_Owner from Source.sql">
113143
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:BAN-1ADWIVEDI-L:True</AssociatedConnectionMoniker>
@@ -145,15 +175,9 @@
145175
<AssociatedConnUserName />
146176
<FullPath>8) Fix Orphan Logins for All DBs.sql</FullPath>
147177
</FileNode>
148-
<FileNode Name="Get-LatestBackup - LogWalk Job.sql">
149-
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
150-
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
151-
<AssociatedConnUserName />
152-
<FullPath>Get-LatestBackup - LogWalk Job.sql</FullPath>
153-
</FileNode>
154178
<FileNode Name="Get-LatestBackups.sql">
155-
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
156-
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
179+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:tul1cipcnpdb1:True</AssociatedConnectionMoniker>
180+
<AssociatedConnSrvName>tul1cipcnpdb1</AssociatedConnSrvName>
157181
<AssociatedConnUserName />
158182
<FullPath>Get-LatestBackups.sql</FullPath>
159183
</FileNode>

Backup-Restore/Get-LatestBackup - LogWalk Job.sql

-114
This file was deleted.

Backup-Restore/Get-LatestBackups.sql

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---------------------------------------------------------------------------------
44
;WITH T_bkpHistory as
55
(
6-
SELECT ROW_NUMBER()over(partition by bs.database_name order by bs.backup_finish_date desc) as RowID,
6+
SELECT --ROW_NUMBER()over(partition by bs.database_name order by bs.backup_finish_date desc) as RowID,
77
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SERVER
88
,bs.database_name
99
,bs.backup_start_date
@@ -28,11 +28,9 @@ SELECT ROW_NUMBER()over(partition by bs.database_name order by bs.backup_finish_
2828
,is_copy_only
2929
FROM msdb.dbo.backupmediafamily AS bmf
3030
INNER JOIN msdb.dbo.backupset AS bs ON bmf.media_set_id = bs.media_set_id
31-
WHERE bs.type='D' --and database_name=''
32-
--database_name = 'Staging'
31+
WHERE bs.backup_start_date >= (select max(bsi.backup_start_date) FROM msdb.dbo.backupmediafamily AS bmfi INNER JOIN msdb.dbo.backupset AS bsi ON bmfi.media_set_id = bsi.media_set_id where bsi.database_name = bs.database_name and bsi.type = 'D')
32+
AND database_name = 'RGS'
3333
)
34-
select (select sum(backup_size_MB)/1024 from T_bkpHistory where RowID = 1) as bkp_size_total_GB,
35-
*
34+
select *
3635
from T_bkpHistory
37-
where RowID = 1
38-
ORDER BY backup_size_MB desc
36+
ORDER BY backup_start_date

Baselining/Baselining.ssmssqlproj

+22-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
6464
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
6565
</ConnectionNode>
66+
<ConnectionNode Name="TUL1MDQ1WDP01:CORPORATE\adwivedi">
67+
<Created>2019-04-24T13:48:50.816533+05:30</Created>
68+
<Type>SQL</Type>
69+
<Server>TUL1MDQ1WDP01</Server>
70+
<UserName />
71+
<Authentication>Windows Authentication</Authentication>
72+
<InitialDB />
73+
<LoginTimeout>30</LoginTimeout>
74+
<ExecutionTimeout>0</ExecutionTimeout>
75+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
76+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
77+
</ConnectionNode>
6678
</Items>
6779
</LogicalFolder>
6880
<LogicalFolder Name="Queries" Type="0" Sorted="true">
@@ -187,17 +199,17 @@
187199
<AssociatedConnUserName />
188200
<FullPath>What was Running - AsOf.sql</FullPath>
189201
</FileNode>
190-
<FileNode Name="What Was Running - Blocking Wait Time.sql">
202+
<FileNode Name="What Was Running - BlockingTree - At - Moment.sql">
191203
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:Ann1vesPDb01:True</AssociatedConnectionMoniker>
192204
<AssociatedConnSrvName>Ann1vesPDb01</AssociatedConnSrvName>
193205
<AssociatedConnUserName />
194-
<FullPath>What Was Running - Blocking Wait Time.sql</FullPath>
206+
<FullPath>What Was Running - BlockingTree - At - Moment.sql</FullPath>
195207
</FileNode>
196-
<FileNode Name="What Was Running - BlockingTree.sql">
208+
<FileNode Name="What Was Running - BlockingTree - During.sql">
197209
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:Ann1vesPDb01:True</AssociatedConnectionMoniker>
198210
<AssociatedConnSrvName>Ann1vesPDb01</AssociatedConnSrvName>
199211
<AssociatedConnUserName />
200-
<FullPath>What Was Running - BlockingTree.sql</FullPath>
212+
<FullPath>What Was Running - BlockingTree - During.sql</FullPath>
201213
</FileNode>
202214
<FileNode Name="What was Running - During - WhoIsActive.sql">
203215
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPEDB2:True</AssociatedConnectionMoniker>
@@ -211,6 +223,12 @@
211223
<AssociatedConnUserName />
212224
<FullPath>What Was Running.sql</FullPath>
213225
</FileNode>
226+
<FileNode Name="Z__What Was Running - Blocking Wait Time.sql">
227+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1MDQ1WDP01:True</AssociatedConnectionMoniker>
228+
<AssociatedConnSrvName>TUL1MDQ1WDP01</AssociatedConnSrvName>
229+
<AssociatedConnUserName />
230+
<FullPath>Z__What Was Running - Blocking Wait Time.sql</FullPath>
231+
</FileNode>
214232
</Items>
215233
</LogicalFolder>
216234
<LogicalFolder Name="Miscellaneous" Type="3" Sorted="true">

Baselining/What Was Running - Blocking Wait Time.sql

-18
This file was deleted.

0 commit comments

Comments
 (0)