1. 程式人生 > >SQL Server 批量主分割槽備份(Multiple Jobs)

SQL Server 批量主分割槽備份(Multiple Jobs)

USE [msdb]
GO
/****** 物件:  Job [Barefoot_Opinion_9565]    指令碼日期: 01/06/2013 14:07:27 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** 物件:  JobCategory [Database Maintenance]    指令碼日期: 01/06/2013 14:07:27 ******/
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_9565', @enabled=1, @notify_level_eventlog=0, @notify_level_email=0, @notify_level_netsend=0, @notify_level_page=0, @delete_level=0, @description=N'備份主分割槽', @category_name=N'Database Maintenance', @owner_login_name
=N'oofraBnimdA_gz', @job_id = @jobId OUTPUT IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback /****** 物件: Step [Bakcup] 指令碼日期: 01/06/2013 14:07:27 ******/ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Bakcup', @step_id=1, @cmdexec_success_code=0, @on_success_action=3, @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=N'TSQL', @command=N'--1設定完整模式 USE [master] GO ALTER DATABASE [DBName.9565] SET RECOVERY FULL WITH NO_WAIT GO --2備份主分割槽(完整備份) DECLARE @CurrentTime VARCHAR(50), @FileName VARCHAR(200) SET @CurrentTime = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 120 ),''-'',''_''),'' '',''_''),'':'','''') IF(DATEPART(DW, GETDATE()) = 2)--星期一 BEGIN SET @FileName = ''E:\DBBackup\DBName.9565\DBName.9565_Primary_Full_'' + @CurrentTime+''.bak'' BACKUP DATABASE [DBName.9565] FILEGROUP=''PRIMARY'' TO [email protected] WITH FORMAT END ELSE BEGIN SET @FileName = ''E:\DBBackup\DBName.9565\DBName.9565_Primary_Diff_'' + @CurrentTime+''.bak'' BACKUP DATABASE [DBName.9565] FILEGROUP=''PRIMARY'' TO [email protected] WITH DIFFERENTIAL,FORMAT END GO --3設定簡單模式 USE [master] GO ALTER DATABASE [DBName.9565] SET RECOVERY SIMPLE WITH NO_WAIT GO ', @database_name=N'DBName.9565', @flags=0 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback /****** 物件: Step [Delete] 指令碼日期: 01/06/2013 14:07:27 ******/ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Delete', @step_id=2, @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=N'TSQL', @command=N'--刪除之前的備份檔案 DECLARE @DeleteDate DATETIME SET @DeleteDate = DATEADD(DAY, -14, GETDATE()) EXECUTE MASTER.SYS.XP_DELETE_FILE 0, N''E:\DBBackup\DBName.9565\'', N''bak'', @DeleteDate', @database_name=N'DBName.9565', @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'Plan', @enabled=1, @freq_type=8, @freq_interval=34, @freq_subday_type=1, @freq_subday_interval=0, @freq_relative_interval=0, @freq_recurrence_factor=1, @active_start_date=20130105, @active_end_date=99991231, @active_start_time=10000, @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: