Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [msdb]
- GO
- /****** Object: Job [Laba6_Task1] Script Date: 12/17/2023 1:14:43 AM ******/
- EXEC msdb.dbo.sp_delete_job @job_id=N'7670497f-1b71-403a-8090-4348040eab16', @delete_unused_schedule=1
- GO
- /****** Object: Job [Laba6_Task1] Script Date: 12/17/2023 1:14:43 AM ******/
- BEGIN TRANSACTION
- DECLARE @ReturnCode INT
- SELECT @ReturnCode = 0
- /****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 12/17/2023 1:14:43 AM ******/
- 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=N'JOB', @type=N'LOCAL', @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=N'Laba6_Task1',
- @enabled=1,
- @notify_level_eventlog=0,
- @notify_level_email=0,
- @notify_level_netsend=0,
- @notify_level_page=0,
- @delete_level=0,
- @description=N'No description available.',
- @category_name=N'[Uncategorized (Local)]',
- @owner_login_name=N'WIN-E0QK7UCTJ3T\user', @job_id = @jobId OUTPUT
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** Object: Step [Clear old backups] Script Date: 12/17/2023 1:14:43 AM ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Clear old backups',
- @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'PowerShell',
- @command=N'rm D:/backups/full/*.bak',
- @database_name=N'master',
- @output_file_name=N'D:\backups\log.txt',
- @flags=2
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** Object: Step [Backup database Laba6] Script Date: 12/17/2023 1:14:43 AM ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Backup database Laba6',
- @step_id=2,
- @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'declare @datetimepart nvarchar(50) = FORMAT(GETDATE(), ''_yyyy_MM_dd_HH_mm_ss'')
- declare @backupFilePath nvarchar(50) = CONCAT(''D:\Backups\full\full'',@datetimepart,''.bak'')
- BACKUP DATABASE Laba6
- TO DISK = @backupFilePath
- with noinit',
- @database_name=N'master',
- @output_file_name=N'D:\backups\log.txt',
- @flags=2
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** Object: Step [Copy on other computer] Script Date: 12/17/2023 1:14:43 AM ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Copy on other computer',
- @step_id=3,
- @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'PowerShell',
- @command=N'cp D:\backups\full\*.bak -Destination Microsoft.Powershell.Core\FileSystem:://WIN-E0QK7UCTJ3T/ShareFolder/backups/full',
- @database_name=N'master',
- @output_file_name=N'D:\backups\log.txt',
- @flags=2
- 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'Laba6_Task1_EveryMonday',
- @enabled=1,
- @freq_type=8,
- @freq_interval=2,
- @freq_subday_type=1,
- @freq_subday_interval=0,
- @freq_relative_interval=0,
- @freq_recurrence_factor=1,
- @active_start_date=20231216,
- @active_end_date=99991231,
- @active_start_time=10000,
- @active_end_time=235959,
- @schedule_uid=N'cbf17759-0540-4263-abb1-4a41f527a392'
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement