码迷,mamicode.com
首页 > 数据库 > 详细

SQLFullbackup

时间:2017-09-12 09:53:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ros   version   run   select   min   ini   ret   start   blog   

/*    ==Scripting Parameters==

    Source Server Version : SQL Server 2016 (13.0.1601)
    Source Database Engine Edition : Microsoft SQL Server Enterprise Edition
    Source Database Engine Type : Standalone SQL Server

    Target Server Version : SQL Server 2016
    Target Database Engine Edition : Microsoft SQL Server Enterprise Edition
    Target Database Engine Type : Standalone SQL Server
*/

USE [msdb]
GO

/****** Object:  Job [FullBackup]    Script Date: 9/11/2017 7:50:54 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 9/11/2017 7:50:54 PM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N[Uncategorized (Local)] AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=NJOB, @type=NLOCAL, @name=N[Uncategorized (Local)]
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=NFullBackup, 
        @enabled=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=NNo description available., 
        @category_name=N[Uncategorized (Local)], 
        @owner_login_name=NCLOUD-88\Administrator, @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [FullBackup]    Script Date: 9/11/2017 7:50:54 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=NFullBackup, 
        @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=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=NTSQL, 
        @command=NBACKUP DATABASE [master] TO  DISK = N‘‘\\SQL-AGBACKUP\AG\master.bak‘‘ WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N‘‘master_backup‘‘, SKIP, REWIND, NOUNLOAD,  STATS = 10
GO
BACKUP DATABASE [model] TO  DISK = N‘‘\\SQL-AGBACKUP\AG\model.bak‘‘ WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N‘‘model_backup‘‘, SKIP, REWIND, NOUNLOAD,  STATS = 10
GO
BACKUP DATABASE [msdb] TO  DISK = N‘‘\\SQL-AGBACKUP\AG\msdb.bak‘‘ WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N‘‘msdb_backup‘‘, SKIP, REWIND, NOUNLOAD,  STATS = 10
GO
DECLARE @preferredReplica int

SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(‘‘OnlineLoansCommon‘‘))

IF (@preferredReplica = 1)
BEGIN
    BACKUP DATABASE [OnlineLoansCommon] TO  DISK = N‘‘\\SQL-AGBACKUP\AG\OnlineLoansCommon.bak‘‘ WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N‘‘OnlineLoansCommon‘‘, SKIP, REWIND, NOUNLOAD,  STATS = 10
END
GO
DECLARE @preferredReplica int

SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(‘‘OnlineLoansCommonQA‘‘))

IF (@preferredReplica = 1)
BEGIN
    BACKUP DATABASE [OnlineLoansCommonQA] TO  DISK = N‘‘\\SQL-AGBACKUP\AG\OnlineLoansCommonQA.bak‘‘ WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N‘‘OnlineLoansCommonQA‘‘, SKIP, REWIND, NOUNLOAD,  STATS = 10
END
, 
        @database_name=Nmaster, 
        @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_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

 

SQLFullbackup

标签:ros   version   run   select   min   ini   ret   start   blog   

原文地址:http://www.cnblogs.com/stragon/p/7508241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!