-
Notifications
You must be signed in to change notification settings - Fork 0
/
How to restore SQL Agent jobs from a MSDB backup file in SQL Server
102 lines (89 loc) · 2.48 KB
/
How to restore SQL Agent jobs from a MSDB backup file in SQL Server
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
96
97
98
99
100
101
102
DECLARE @JobID UNIQUEIDENTIFIER
declare @jobname nvarchar(128)
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT DISTINCT job_id
FROM msdb_recover.dbo.sysjobs
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @JobID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @jobname = name FROM msdb_recover.dbo.sysjobs WHERE job_id=@jobid
print @jobname
--inserting in sysjobs
INSERT msdb.dbo.sysjobs
SELECT * FROM msdb_recover.dbo.sysjobs
WHERE job_id=@JobID
--inserting in sysjobsteps
INSERT msdb.dbo.sysjobsteps
SELECT * FROM msdb_recover.dbo.sysjobsteps
WHERE job_id=@JobID
--insert in sysjobhistory
SET IDENTITY_INSERT msdb.dbo.sysjobhistory ON
INSERT msdb.dbo.sysjobhistory
(instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
[message],run_status,run_date,run_time,run_duration,operator_id_emailed,
operator_id_netsent,operator_id_paged,retries_attempted,[server])
SELECT
instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
[message],run_status,run_date,run_time,run_duration,operator_id_emailed,
operator_id_netsent,operator_id_paged,retries_attempted,[server]
FROM msdb_recover.dbo.sysjobhistory
WHERE job_id=@JobID
SET IDENTITY_INSERT msdb.dbo.sysjobhistory OFF
--inserting in sysschedules
SET IDENTITY_INSERT msdb.dbo.sysschedules ON
INSERT msdb.dbo.sysschedules
( [schedule_id]
,[schedule_uid]
,[originating_server_id]
,[name]
,[owner_sid]
,[enabled]
,[freq_type]
,[freq_interval]
,[freq_subday_type]
,[freq_subday_interval]
,[freq_relative_interval]
,[freq_recurrence_factor]
,[active_start_date]
,[active_end_date]
,[active_start_time]
,[active_end_time]
,[date_created]
,[date_modified]
,[version_number])
SELECT s.[schedule_id]
,s.[schedule_uid]
,s.[originating_server_id]
,s.[name]
,s.[owner_sid]
,s.[enabled]
,s.[freq_type]
,s.[freq_interval]
,s.[freq_subday_type]
,s.[freq_subday_interval]
,s.[freq_relative_interval]
,s.[freq_recurrence_factor]
,s.[active_start_date]
,s.[active_end_date]
,s.[active_start_time]
,s.[active_end_time]
,s.[date_created]
,s.[date_modified]
,s.[version_number]
FROM msdb_recover.dbo.sysschedules s, msdb_recover.dbo.sysjobschedules j
WHERE j.job_id=@JobID and s.schedule_id = j.schedule_id
SET IDENTITY_INSERT msdb.dbo.sysschedules OFF
--inserting in sysjobschedules
INSERT msdb.dbo.sysjobschedules
SELECT * FROM msdb_recover.dbo.sysjobschedules
WHERE job_id=@JobID
--adding jobs in server
use msdb
EXEC dbo.sp_add_jobserver @job_id = @jobid
FETCH NEXT FROM MY_CURSOR INTO @JobID
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR