-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript for T-log mp job
115 lines (97 loc) · 4.38 KB
/
script for T-log mp job
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
103
104
105
106
107
108
Script to provide the default backup path (need to append \<DBNAME> and create the folder)
declare @BackupDirectory varchar(500)
declare @instanceName varchar(25) = cast((SELECT SERVERPROPERTY('InstanceName')) as nvarchar)
If @instanceName is null
begin
DECLARE @HkeyLocal nvarchar(18)
DECLARE @MSSqlServerRegPath nvarchar(31)
DECLARE @InstanceRegPath sysname
SELECT @HkeyLocal=N'HKEY_LOCAL_MACHINE'
SELECT @MSSqlServerRegPath=N'SOFTWARE\Microsoft\MSSQLServer'
SELECT @InstanceRegPath=@MSSqlServerRegPath + N'\MSSQLServer'
EXEC master.dbo.xp_instance_regread @HkeyLocal, @InstanceRegPath, N'BackupDirectory', @BackupDirectory OUTPUT
end
else
begin
declare @dir nvarchar(4000)
declare @key nvarchar(1000)
declare @instanceDir nvarchar(4000)
exec master.sys.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instanceName, @instanceDir output, 'no_output'
SET @key = N'Software\Microsoft\Microsoft SQL Server\' + RTRIM(@instanceDir) + '\MSSQLServer'
exec master.sys.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='BackupDirectory',@value=@BackupDirectory OUTPUT
end
select @BackupDirectory
Backup job creation script
USE [msdb]
GO
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'<DBNAME> TLog Backup',
@enabled=1,
@notify_level_eventlog=2,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'Database Maintenance',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Subplan_1',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=2,
@retry_interval=5,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'Declare @Current_Time varchar(20)
Set @Current_Time = (select replace(replace(replace(convert(varchar(50), getdate(), 120),''-'',''''),'' '',''_''),'':'',''''))
Declare @BackupLocation Varchar(250)
Set @BackupLocation = (''F:\Backup\MSSQL\MSSQL11.I01\MSSQL\Backup\<DBNAME>\<DBNAME>_''+@Current_Time+''.trn'')
If (select COUNT(*) from sys.databases
where name like ''<DBNAME>'' and state_desc like ''ONLINE'' and user_access_desc like ''MULTI_USER'' and is_read_only = 0 and recovery_model_desc not like ''SIMPLE''
and name not in (select primary_database from msdb.dbo.log_shipping_monitor_primary)) < 1
Begin
exec msdb.dbo.sp_stop_job @job_name = ''MP-<DBNAME> TLog Backup''
End
Else
Begin
BACKUP LOG [<DBNAME>] To Disk = @BackupLocation
End
',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Run Database Task',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=15,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20151015,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO