我的方法是把作业导出成文件备份起来,因为当你服务器维护的多了的时候很多你的作业 就很成问题,很麻烦
作业备份,不是备份数据库,是备份作业。
我的方法是把作业导出成文件备份起来,因为当你服务器维护的多了的时候很多你的作业 就很成问题,很麻烦。
最好能够作业实现同步,这个也是第一步,保存成文件,之后个人设想使用powershell 来把作业同步起来
DECLARE @jobname VARCHAR(30),@category_calss_i INT ,@category_calss VARCHAR(50),@category_name VARCHAR(50)
,@category_type VARCHAR(30),@category_id int
,@category_type_i int
SELECT @jobname = 'powershell',@category_calss = '',@category_name='',@category_type = ''
SELECT @category_calss = CASE WHEN tshc.category_class = 1 THEN 'JOB'
WHEN tshc.category_class = 2 THEN 'ALERT'
else 'OPERATOR'
END
, @category_type = CASE WHEN tshc.category_type = 1 THEN 'LOCAL'
WHEN tshc.category_type = 2 THEN 'MULTI-SERVER'
else 'NONE'
END
,@category_name = tshc.name
,@category_type_i = category_type
,@category_calss_i = tshc.category_class
,@category_id = tshc.category_id
FROM
msdb.dbo.sysjobs_view AS sv
INNER JOIN msdb.dbo.syscategories AS tshc ON sv.category_id = tshc.category_id
WHERE
(sv.name=@jobname AND tshc.category_class = 1)
PRINT ' BEGIN TRANSACTION'
PRINT 'DECLARE @ReturnCode INT'
PRINT 'IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N''' + @category_name +'''AND category_class=' +rtrim(@category_calss_i)+')'
PRINT 'BEGIN'
PRINT 'EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'''+ @category_calss+''', @type=N'''+@category_type+''', @name=N'''+@category_name+''''
PRINT &#39;IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback&#39;
PRINT &#39;end&#39;
DECLARE &#64;EventLogLevel INT,&#64;EmailLevel INT,&#64;NetSendLevel INT,&#64;PageLevel INT
DECLARE &#64;EmailLeveloprid NVARCHAR(256),&#64;NetSendLeveloprid NVARCHAR(256),&#64;PageLeveloprid NVARCHAR(256)
DECLARE &#64;isenable INT , &#64;description NVARCHAR(1024),&#64;owner_log_name Nvarchar(512),&#64;delete_level INT
DECLARE &#64;jobId UNIQUEIDENTIFIER,&#64;start_step_id INT,&#64;server NVARCHAR(512)
SELECT
&#64;EventLogLevel&#61;sv.notify_level_eventlog
,&#64;EmailLevel&#61;sv.notify_level_email
,&#64;NetSendLevel&#61;sv.notify_level_netsend
,&#64;PageLevel&#61;sv.notify_level_page
,&#64;EmailLeveloprid &#61; ISNULL((SELECT TOP 1 name FROM msdb..sysoperators WHERE id &#61; sv.notify_email_operator_id),&#39;&#39;)
,&#64;NetSendLeveloprid &#61; ISNULL((SELECT TOP 1 name FROM msdb..sysoperators WHERE id &#61; sv.notify_netsend_operator_id),&#39;&#39;)
,&#64;PageLeveloprid &#61; ISNULL((SELECT TOP 1 name FROM msdb..sysoperators WHERE id &#61; sv.notify_page_operator_id),&#39;&#39;)
,&#64;isenable &#61; sv.enabled
,&#64;description &#61; sv.description
,&#64;owner_log_name &#61; ISNULL(suser_sname(sv.owner_sid), N&#39;&#39;&#39;&#39;)
,&#64;delete_level &#61; sv.delete_level
,&#64;jobId &#61; sv.job_id
,&#64;start_step_id &#61; start_step_id
,&#64;server &#61; originating_server
FROM msdb.dbo.sysjobs_view AS sv
WHERE (sv.name&#61;&#64;jobname and sv.category_id&#61;0)
PRINT &#39;DECLARE &#64;jobId BINARY(16)&#39;
PRINT &#39;EXEC &#64;ReturnCode &#61; msdb.dbo.sp_add_job &#64;job_name&#61;N&#39;&#39;&#39;&#43;&#64;jobname&#43;&#39;&#39;&#39;,&#39;
PRINT &#39; &#64;enabled&#61;&#39;&#43;RTRIM(&#64;isenable)&#43;&#39;, &#39;
PRINT &#39; &#64;notify_level_eventlog&#61;&#39;&#43;RTRIM(&#64;EventLogLevel)&#43;&#39;, &#39;
PRINT &#39; &#64;notify_level_email&#61;&#39;&#43;RTRIM(&#64;EmailLevel)&#43;&#39;, &#39;
PRINT &#39; &#64;notify_level_netsend&#61;&#39;&#43;RTRIM(&#64;NetSendLevel)&#43;&#39;, &#39;
PRINT &#39; &#64;notify_level_page&#61;&#39;&#43;RTRIM(&#64;PageLevel)&#43;&#39;, &#39;
PRINT &#39; &#64;notify_email_operator_name &#61;&#39;&#39;&#39;&#43;RTRIM(&#64;EmailLeveloprid)&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;notify_netsend_operator_name&#61;&#39;&#39;&#39;&#43;RTRIM(&#64;NetSendLeveloprid)&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;notify_page_operator_name&#61;&#39;&#39;&#39;&#43;RTRIM(&#64;PageLeveloprid)&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;delete_level&#61;&#39;&#43;RTRIM(&#64;delete_level)&#43;&#39;, &#39;
PRINT &#39; &#64;description&#61;N&#39;&#39;&#39;&#43;&#64;description&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;category_name&#61;N&#39;&#39;&#39;&#43;&#64;category_name&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;owner_login_name&#61;N&#39;&#39;&#39;&#43;&#64;owner_log_name&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;job_id &#61; &#64;jobId OUTPUT&#39;
PRINT &#39;IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback&#39;
--SELECT * FROM msdb.dbo.syscategories
DECLARE &#64;step_id INT
declare &#64;step_name nvarchar(512) ,&#64;cmdexec_success_code INT,&#64;on_success_action INT,&#64;on_success_step_id INT
,&#64;on_fail_action INT,&#64;on_fail_step_id INT,&#64;retry_attempts INT,&#64;retry_interval INT,&#64;os_run_priority INT
,&#64;subsystem NVARCHAR(512),&#64;database_name NVARCHAR(512),&#64;flags INT,&#64;command NVARCHAR(max)
DECLARE jbcur CURSOR FOR SELECT step_id FROM msdb..sysjobsteps WHERE job_id &#61; &#64;jobid ORDER BY step_id ;
OPEN jbcur;
FETCH NEXT FROM jbcur INTO &#64;step_id
WHILE &#64;&#64;FETCH_STATUS &#61; 0
BEGIN
SELECT &#64;step_name &#61; step_name
,&#64;cmdexec_success_code&#61; cmdexec_success_code
,&#64;on_success_action &#61; on_success_action
,&#64;on_success_step_id &#61; on_success_step_id
,&#64;on_fail_action &#61; on_fail_action
,&#64;on_fail_step_id &#61; on_fail_step_id
,&#64;retry_attempts &#61; retry_attempts
,&#64;retry_interval &#61; retry_interval
,&#64;os_run_priority &#61; os_run_priority
,&#64;subsystem &#61; subsystem
,&#64;database_name &#61; database_name
,&#64;command &#61; command
,&#64;flags &#61; flags
FROM msdb..sysjobsteps a WHERE job_id &#61; &#64;jobid and step_id &#61; &#64;step_id
PRINT &#39; EXEC &#64;ReturnCode &#61; msdb.dbo.sp_add_jobstep &#64;job_id&#61;&#64;jobId, &#39;
PRINT &#39; &#64;step_name&#61;N&#39;&#39;&#39;&#43;&#64;step_name&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;step_id&#61;&#39;&#43;RTRIM(&#64;step_id)&#43;&#39;, &#39;
PRINT &#39; &#64;cmdexec_success_code&#61;&#39;&#43;RTRIM(&#64;cmdexec_success_code)&#43;&#39;, &#39;
PRINT &#39; &#64;on_success_action&#61;&#39;&#43;RTRIM(&#64;on_success_action)&#43;&#39;, &#39;
PRINT &#39; &#64;on_success_step_id&#61;&#39;&#43;RTRIM(&#64;on_success_step_id)&#43;&#39;, &#39;
PRINT &#39; &#64;on_fail_action&#61;&#39;&#43;RTRIM(&#64;on_fail_action)&#43;&#39;, &#39;
PRINT &#39; &#64;on_fail_step_id&#61;&#39;&#43;RTRIM(&#64;on_fail_step_id)&#43;&#39;, &#39;
PRINT &#39; &#64;retry_attempts&#61;&#39;&#43;RTRIM(&#64;retry_attempts)&#43;&#39;, &#39;
PRINT &#39; &#64;retry_interval&#61;&#39;&#43;RTRIM(&#64;retry_interval)&#43;&#39;, &#39;
PRINT &#39; &#64;os_run_priority&#61;&#39;&#43;RTRIM(&#64;os_run_priority)&#43;&#39;, &#64;subsystem&#61;N&#39;&#39;&#39;&#43;&#64;subsystem&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;database_name&#61;N&#39;&#39;&#39;&#43;&#64;database_name&#43;&#39;&#39;&#39;,&#39;
PRINT &#39; &#64;flags&#61;&#39;&#43;RTRIM(&#64;flags)&#43;&#39; ,&#39;
PRINT &#39; &#64;command&#61;N&#39;&#39;&#39;&#43;REPLACE(&#64;command,&#39;&#39;&#39;&#39;,&#39;&#39;&#39;&#39;&#39;&#39;)&#43;&#39;&#39;&#39;&#39;
PRINT &#39; IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback&#39;
FETCH NEXT FROM jbcur INTO &#64;step_id
END
CLOSE jbcur
DEALLOCATE jbcur
PRINT &#39; EXEC &#64;ReturnCode &#61; msdb.dbo.sp_update_job &#64;job_id &#61; &#64;jobId, &#64;start_step_id &#61; &#39;&#43;rtrim(&#64;start_step_id)
PRINT &#39; IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback &#39;
DECLARE &#64;enabled INT,&#64;freq_type INT,&#64;freq_interval INT,&#64;freq_subday_type INT,&#64;freq_subday_interval INT
,&#64;freq_relative_interval INT,&#64;freq_recurrence_factor INT,&#64;active_start_date INT,&#64;active_end_date INT
,&#64;active_start_time INT,&#64;active_end_time INT,&#64;name VARCHAR(512)
SELECT
&#64;name &#61; a.name
,&#64;enabled &#61; enabled
,&#64;freq_interval &#61; freq_interval
,&#64;freq_type &#61; freq_type
,&#64;freq_subday_type&#61;freq_subday_type
,&#64;freq_subday_interval&#61;freq_subday_interval
,&#64;freq_relative_interval&#61;freq_relative_interval
,&#64;freq_recurrence_factor&#61;freq_recurrence_factor
,&#64;active_start_date&#61;active_start_date
,&#64;active_end_date&#61;active_end_date
,&#64;active_start_time&#61;active_start_time
,&#64;active_end_time&#61;active_end_time
FROM msdb..sysschedules a
INNER JOIN msdb.dbo.sysjobschedules b ON a.schedule_id &#61; b.schedule_id
WHERE job_id &#61; &#64;jobId
IF(&#64;name IS not null)
begin
PRINT &#39; EXEC &#64;ReturnCode &#61; msdb.dbo.sp_add_jobschedule &#64;job_id&#61;&#64;jobId, &#64;name&#61;N&#39;&#39;&#39;&#43;&#64;name&#43;&#39;&#39;&#39;, &#39;
PRINT &#39; &#64;enabled&#61;&#39;&#43;RTRIM(&#64;enabled)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_type&#61;&#39;&#43;RTRIM(&#64;freq_type)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_interval&#61;&#39;&#43;RTRIM(&#64;freq_interval)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_subday_type&#61;&#39;&#43;RTRIM(&#64;freq_subday_type)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_subday_interval&#61;&#39;&#43;RTRIM(&#64;freq_subday_interval)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_relative_interval&#61;&#39;&#43;RTRIM(&#64;freq_relative_interval)&#43;&#39;, &#39;
PRINT &#39; &#64;freq_recurrence_factor&#61;&#39;&#43;RTRIM(&#64;freq_recurrence_factor)&#43;&#39;, &#39;
PRINT &#39; &#64;active_start_date&#61;&#39;&#43;RTRIM(&#64;active_start_date)&#43;&#39;, &#39;
PRINT &#39; &#64;active_end_date&#61;&#39;&#43;RTRIM(&#64;active_end_date)&#43;&#39;, &#39;
PRINT &#39; &#64;active_start_time&#61;&#39;&#43;RTRIM(&#64;active_start_time)&#43;&#39;, &#39;
PRINT &#39; &#64;active_end_time&#61;&#39;&#43;RTRIM(&#64;active_end_time)&#43;&#39;, &#39;
PRINT &#39; &#64;schedule_uid&#61;N&#39;&#39;&#39;&#43;RTRIM(NEWID())&#43;&#39;&#39;&#39;&#39;
PRINT &#39; IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback&#39;
END
PRINT &#39; EXEC &#64;ReturnCode &#61; msdb.dbo.sp_add_jobserver &#64;job_id &#61; &#64;jobId, &#64;server_name &#61; N&#39;&#39;&#39;&#43;&#64;server&#43;&#39;&#39;&#39;&#39;
PRINT &#39; IF (&#64;&#64;ERROR <> 0 OR &#64;ReturnCode <> 0) GOTO QuitWithRollback&#39;
PRINT &#39;COMMIT TRANSACTION&#39;
PRINT &#39;GOTO EndSave&#39;
PRINT &#39;QuitWithRollback:&#39;
PRINT &#39; IF(&#64;&#64;TRANCOUNT>0)ROLLBACK TRANSACTION&#39;
PRINT &#39;EndSave:&#39;
PRINT &#39; &#39;
PRINT &#39;GO&#39;