Skip to content

Commit b2d6c36

Browse files
author
Ajay Dwivedi
committed
Updating Restore Code
Updating Restore Code
1 parent 65755e8 commit b2d6c36

File tree

6 files changed

+125
-35
lines changed

6 files changed

+125
-35
lines changed
1 KB
Binary file not shown.

Backup-Restore/Backup-Restore.ssmssqlproj

+6
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@
193193
<AssociatedConnUserName />
194194
<FullPath>Get-LatestBackups.sql</FullPath>
195195
</FileNode>
196+
<FileNode Name="Query - Backup History - Path Finding.sql">
197+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPRDB1:True</AssociatedConnectionMoniker>
198+
<AssociatedConnSrvName>TUL1CIPRDB1</AssociatedConnSrvName>
199+
<AssociatedConnUserName />
200+
<FullPath>Query - Backup History - Path Finding.sql</FullPath>
201+
</FileNode>
196202
<FileNode Name="Query - Backup History.sql">
197203
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True</AssociatedConnectionMoniker>
198204
<AssociatedConnSrvName>(local)</AssociatedConnSrvName>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---------------------------------------------------------------------------------
2+
--Database Backups for all databases For Previous Week
3+
---------------------------------------------------------------------------------
4+
;with t_BkpHistOry AS
5+
(
6+
SELECT CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SERVER
7+
,bs.database_name
8+
,bs.backup_start_date
9+
,bs.backup_finish_date
10+
,bs.expiration_date
11+
,CASE bs.type
12+
WHEN 'D'
13+
THEN 'Database'
14+
WHEN 'I'
15+
THEN 'Differential'
16+
WHEN 'L'
17+
THEN 'Log'
18+
END AS backup_type
19+
,bs.backup_size
20+
,bmf.logical_device_name
21+
,bmf.physical_device_name
22+
,bs.NAME AS backupset_name
23+
,bs.description
24+
,is_copy_only
25+
,ROW_NUMBER()OVER(PARTITION BY bs.database_name, bs.type ORDER BY bs.backup_start_date desc) as BackupID
26+
FROM msdb.dbo.backupmediafamily AS bmf
27+
INNER JOIN msdb.dbo.backupset AS bs ON bmf.media_set_id = bs.media_set_id
28+
WHERE bs.backup_start_date >= DATEADD(day,-15,getdate())
29+
and bmf.physical_device_name not like 'EMCAPM_BACKUP%'
30+
)
31+
,t_HistoryLastest as
32+
(
33+
SELECT h.SERVER, d.name as dbName, h.backup_type, h.backup_start_date, h.physical_device_name
34+
,left(h.physical_device_name,len(h.physical_device_name)-charindex('\',REVERSE(h.physical_device_name))) as bkpPath
35+
,right(h.physical_device_name,3) as Extension
36+
FROM sys.databases d
37+
left outer join
38+
t_BkpHistOry as h
39+
on h.database_name = d.name
40+
WHERE BackupID = 1
41+
)
42+
,t_BkpPaths as
43+
( select *
44+
,ROW_NUMBER()OVER(PARTITION BY backup_type, bkpPath, Extension order by dbname) as BkpPathID
45+
from t_HistoryLastest h
46+
)
47+
select --distinct backup_type, bkpPath, Extension
48+
*
49+
from t_BkpPaths-- order by bkpPath, dbName
50+
where BkpPathID = 1
51+
--where bkpPath = 'F:\Dump'
52+
--where backup_type = 'Log'
53+
--and bkpPath = 'G:\mssqldata\Backup'

LogShipping/v1.4 - [usp_GetLogWalkJobHistoryAlert_Suppress].sql

+43-30
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ BEGIN
5151
DECLARE @_collection_time_end datetime;
5252
DECLARE @IsBlockingIssue BIT;
5353
DECLARE @AreInDirectConnections BIT;
54+
DECLARE @IsBaseCapturingDoneInLast5Minutes BIT;
5455
DECLARE @_SendMailRequired BIT;
5556
DECLARE @JobSchedule varchar(255),
5657
@NextRunTime datetime;
@@ -356,9 +357,11 @@ BEGIN
356357
left join msdb.dbo.sysschedules SS on SS.schedule_id = SJ.schedule_id
357358
where s.name = @p_JobName
358359
)
359-
SELECT @JobSchedule = Frequency + ' (' + Interval + ') - ' + [Time]
360+
SELECT TOP (1)
361+
@JobSchedule = Frequency + ' (' + Interval + ') - ' + [Time]
360362
,@NextRunTime = NextRunTime
361-
FROM T_Schedules;
363+
FROM T_Schedules
364+
ORDER BY CAST(NextRunTime AS DATETIME) ASC;
362365

363366
-- Find Job Session along with its Blockers
364367
IF OBJECT_ID('tempdb..#JobSessionBlockers') IS NOT NULL
@@ -562,38 +565,48 @@ RCA: Kindly execute below query to find out details of Blockers.
562565
IF @p_Verbose = 1
563566
PRINT 'Logic processing when @p_PerformAutoExecutionOfLogWalkJob = 1';
564567

568+
-- Find if sp_WhoIsActive capturing is done within last 5 minutes
565569
IF @p_Verbose = 1
566-
PRINT 'Finding Direct/Indirect connections for database '+QUOTENAME(@p_DbName);
567-
INSERT #DatabaseConnections (session_id, dbName, IsDirectConnection)
568-
SELECT session_id, dbName, MAX(IsDirectConnection) AS IsDirectConnection
569-
FROM (
570-
SELECT s.session_id, DB_NAME(r.database_id) as dbName, 1 as IsDirectConnection
571-
FROM sys.dm_exec_sessions as s
572-
INNER JOIN sys.dm_exec_requests as r on r.session_id = s.session_id
573-
WHERE r.database_id = db_id(@p_DbName)
574-
--
575-
UNION ALL
576-
--
577-
SELECT l.request_session_id, db_name(l.resource_database_id) as dbName, 0 as IsDirectConnection
578-
FROM sys.dm_tran_locks as l left join sys.dm_exec_sessions as s on s.session_id = l.request_session_id
579-
WHERE l.resource_type = 'DATABASE' AND l.resource_database_id = db_id(@p_DbName)
580-
GROUP BY l.resource_database_id, l.request_session_id
581-
) AS c
582-
GROUP BY c.dbName, c.session_id;
583-
584-
IF NOT EXISTS(SELECT * FROM #DatabaseConnections WHERE dbName = @p_DbName AND IsDirectConnection = 0)
570+
PRINT 'Checking if sp_WhoIsActive capturing is done within last 5 minutes..';
571+
IF EXISTS(SELECT * FROM dbo.WhoIsActive_ResultSets as r WHERE r.collection_time >= DATEADD(MINUTE,-5,GETDATE()))
572+
SET @IsBaseCapturingDoneInLast5Minutes = 1;
573+
ELSE
585574
BEGIN
586-
IF DBA.dbo.fn_IsJobRunning(@p_JobName) = 0
575+
IF @p_Verbose = 1
576+
PRINT ' Starting sp_WhoIsActive capturing..';
577+
EXEC DBA.dbo.usp_SendWhoIsActiveMessage @p_JobName = @p_JobName;
578+
WAITFOR DELAY '00:05';
579+
SET @IsBaseCapturingDoneInLast5Minutes = 1;
580+
END
581+
582+
IF @IsBaseCapturingDoneInLast5Minutes = 1
583+
BEGIN
584+
IF @p_Verbose = 1
585+
PRINT ' Checking for Indirect connections for database '+QUOTENAME(@p_DbName);
586+
;WITH t_results as
587+
(
588+
SELECT @p_DbName as dbName, *
589+
FROM [DBA]..[WhoIsActive_ResultSets] r
590+
WHERE r.collection_time >= DATEADD(MINUTE,-5,GETDATE())
591+
AND r.database_name <> @p_DbName
592+
)
593+
SELECT * INTO #ActiveIndirectDbSessions from t_results as r
594+
WHERE r.locks.exist( '/Database[@name=sql:column("dbName")]') = 1;
595+
596+
IF EXISTS(SELECT * FROM #ActiveIndirectDbSessions)
597+
PRINT 'There are inDirect connections against '+QUOTENAME(@p_DbName)+' database. So cannot run Log Walk job.';
598+
ELSE
587599
BEGIN
588-
IF @p_Verbose = 1
589-
PRINT 'Trying to start job '''+@p_JobName+'''';
590-
EXEC msdb..sp_start_job @job_name = @p_JobName;
600+
IF DBA.dbo.fn_IsJobRunning(@p_JobName) = 0
601+
BEGIN
602+
IF @p_Verbose = 1
603+
PRINT 'Trying to start job '''+@p_JobName+'''';
604+
EXEC msdb..sp_start_job @job_name = @p_JobName;
605+
END
606+
ELSE
607+
PRINT 'Job '''+@p_JobName+''' is already running.';
591608
END
592-
ELSE
593-
PRINT 'Job '''+@p_JobName+''' is already running.';
594-
END
595-
ELSE
596-
PRINT 'There are inDirect connections against '+QUOTENAME(@p_DbName)+' database. So cannot run Log Walk job.';
609+
END
597610
END
598611
END -- Block -> Logic if Job Failure is due to Blocking Issue
599612
ELSE IF @NoOfContinousFailures <> 0 AND @IsBlockingIssue = 0

SQL Jobs/Jobs_Schedule.sql

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
DECLARE @p_JobName VARCHAR(125) = NULL;
2-
SET @p_JobName = 'DBA Log Walk - Restore Cosmo';
2+
SET @p_JobName = 'DBA Log Walk - Restore Staging as Staging';
33

44
DECLARE @JobSchedule varchar(255);
55
DECLARE @NextRunTime datetime;
@@ -84,6 +84,12 @@ DECLARE @NextRunTime datetime;
8484
where s.name = @p_JobName
8585
)
8686
SELECT *
87-
--,@JobSchedule = Frequency + ' (' + Interval + ') - ' + [Time]
87+
--TOP (1)
88+
--@JobSchedule = Frequency + ' (' + Interval + ') - ' + [Time]
8889
--,@NextRunTime = NextRunTime
89-
FROM T_Schedules;
90+
FROM T_Schedules
91+
WHERE CAST(NextRunTime AS DATETIME) >= GETDATE()
92+
ORDER BY CAST(NextRunTime AS DATETIME) ASC
93+
94+
print @JobSchedule
95+
print @NextRunTime

SQL Jobs/SQL Jobs.ssmssqlproj

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
5252
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
5353
</ConnectionNode>
54+
<ConnectionNode Name="TUL1CIPXDB20:CORPORATE\adwivedi">
55+
<Created>2019-08-10T14:07:01.3153052+05:30</Created>
56+
<Type>SQL</Type>
57+
<Server>TUL1CIPXDB20</Server>
58+
<UserName />
59+
<Authentication>Windows Authentication</Authentication>
60+
<InitialDB />
61+
<LoginTimeout>30</LoginTimeout>
62+
<ExecutionTimeout>0</ExecutionTimeout>
63+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
64+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
65+
</ConnectionNode>
5466
</Items>
5567
</LogicalFolder>
5668
<LogicalFolder Name="Queries" Type="0" Sorted="true">
@@ -68,8 +80,8 @@
6880
<FullPath>JobHistory_Duration.sql</FullPath>
6981
</FileNode>
7082
<FileNode Name="Jobs_Schedule.sql">
71-
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
72-
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
83+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPXDB20:True</AssociatedConnectionMoniker>
84+
<AssociatedConnSrvName>TUL1CIPXDB20</AssociatedConnSrvName>
7385
<AssociatedConnUserName />
7486
<FullPath>Jobs_Schedule.sql</FullPath>
7587
</FileNode>

0 commit comments

Comments
 (0)