Advertisement
PavloSerg

ProtectDB_Laba6_Task1

Dec 16th, 2023 (edited)
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.22 KB | None | 0 0
  1. USE [msdb]
  2. GO
  3.  
  4. /****** Object:  Job [Laba6_Task1]    Script Date: 12/17/2023 1:14:43 AM ******/
  5. EXEC msdb.dbo.sp_delete_job @job_id=N'7670497f-1b71-403a-8090-4348040eab16', @delete_unused_schedule=1
  6. GO
  7.  
  8. /****** Object:  Job [Laba6_Task1]    Script Date: 12/17/2023 1:14:43 AM ******/
  9. BEGIN TRANSACTION
  10. DECLARE @ReturnCode INT
  11. SELECT @ReturnCode = 0
  12. /****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 12/17/2023 1:14:43 AM ******/
  13. IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
  14. BEGIN
  15. EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
  16. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  17.  
  18. END
  19.  
  20. DECLARE @jobId BINARY(16)
  21. EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Laba6_Task1',
  22.         @enabled=1,
  23.         @notify_level_eventlog=0,
  24.         @notify_level_email=0,
  25.         @notify_level_netsend=0,
  26.         @notify_level_page=0,
  27.         @delete_level=0,
  28.         @description=N'No description available.',
  29.         @category_name=N'[Uncategorized (Local)]',
  30.         @owner_login_name=N'WIN-E0QK7UCTJ3T\user', @job_id = @jobId OUTPUT
  31. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  32. /****** Object:  Step [Clear old backups]    Script Date: 12/17/2023 1:14:43 AM ******/
  33. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Clear old backups',
  34.         @step_id=1,
  35.         @cmdexec_success_code=0,
  36.         @on_success_action=3,
  37.         @on_success_step_id=0,
  38.         @on_fail_action=2,
  39.         @on_fail_step_id=0,
  40.         @retry_attempts=0,
  41.         @retry_interval=0,
  42.         @os_run_priority=0, @subsystem=N'PowerShell',
  43.         @command=N'rm D:/backups/full/*.bak',
  44.         @database_name=N'master',
  45.         @output_file_name=N'D:\backups\log.txt',
  46.         @flags=2
  47. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  48. /****** Object:  Step [Backup database Laba6]    Script Date: 12/17/2023 1:14:43 AM ******/
  49. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Backup database Laba6',
  50.         @step_id=2,
  51.         @cmdexec_success_code=0,
  52.         @on_success_action=3,
  53.         @on_success_step_id=0,
  54.         @on_fail_action=2,
  55.         @on_fail_step_id=0,
  56.         @retry_attempts=0,
  57.         @retry_interval=0,
  58.         @os_run_priority=0, @subsystem=N'TSQL',
  59.         @command=N'declare @datetimepart nvarchar(50) = FORMAT(GETDATE(), ''_yyyy_MM_dd_HH_mm_ss'')
  60. declare @backupFilePath nvarchar(50) = CONCAT(''D:\Backups\full\full'',@datetimepart,''.bak'')
  61. BACKUP DATABASE Laba6
  62. TO DISK = @backupFilePath
  63. with noinit',
  64.         @database_name=N'master',
  65.         @output_file_name=N'D:\backups\log.txt',
  66.         @flags=2
  67. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  68. /****** Object:  Step [Copy on other computer]    Script Date: 12/17/2023 1:14:43 AM ******/
  69. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Copy on other computer',
  70.         @step_id=3,
  71.         @cmdexec_success_code=0,
  72.         @on_success_action=1,
  73.         @on_success_step_id=0,
  74.         @on_fail_action=2,
  75.         @on_fail_step_id=0,
  76.         @retry_attempts=0,
  77.         @retry_interval=0,
  78.         @os_run_priority=0, @subsystem=N'PowerShell',
  79.         @command=N'cp D:\backups\full\*.bak -Destination Microsoft.Powershell.Core\FileSystem:://WIN-E0QK7UCTJ3T/ShareFolder/backups/full',
  80.         @database_name=N'master',
  81.         @output_file_name=N'D:\backups\log.txt',
  82.         @flags=2
  83. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  84. EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
  85. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  86. EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Laba6_Task1_EveryMonday',
  87.         @enabled=1,
  88.         @freq_type=8,
  89.         @freq_interval=2,
  90.         @freq_subday_type=1,
  91.         @freq_subday_interval=0,
  92.         @freq_relative_interval=0,
  93.         @freq_recurrence_factor=1,
  94.         @active_start_date=20231216,
  95.         @active_end_date=99991231,
  96.         @active_start_time=10000,
  97.         @active_end_time=235959,
  98.         @schedule_uid=N'cbf17759-0540-4263-abb1-4a41f527a392'
  99. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  100. EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
  101. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  102. COMMIT TRANSACTION
  103. GOTO EndSave
  104. QuitWithRollback:
  105.     IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
  106. EndSave:
  107. GO
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement