Advertisement
PavloSerg

ProtectDB_Laba6_Task2

Dec 16th, 2023
1,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.41 KB | None | 0 0
  1. USE [msdb]
  2. GO
  3.  
  4. /****** Object:  Job [Laba6_Task2]    Script Date: 12/17/2023 1:10:59 AM ******/
  5. EXEC msdb.dbo.sp_delete_job @job_id=N'eabd4aa0-480e-4bb3-9aae-c3a0f8aaf711', @delete_unused_schedule=1
  6. GO
  7.  
  8. /****** Object:  Job [Laba6_Task2]    Script Date: 12/17/2023 1:10:59 AM ******/
  9. BEGIN TRANSACTION
  10. DECLARE @ReturnCode INT
  11. SELECT @ReturnCode = 0
  12. /****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 12/17/2023 1:10:59 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_Task2',
  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:10:59 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/diff/*.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:10:59 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\diff\diff'',@datetimepart,''.bak'')
  61. BACKUP DATABASE Laba6
  62. TO DISK = @backupFilePath
  63. with DIFFERENTIAL',
  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:10:59 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=3,
  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\diff\*.bak -Destination Microsoft.Powershell.Core\FileSystem:://WIN-E0QK7UCTJ3T/ShareFolder/backups/diff',
  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. /****** Object:  Step [Restore database Laba6_copy]    Script Date: 12/17/2023 1:10:59 AM ******/
  85. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Restore database Laba6_copy',
  86.         @step_id=4,
  87.         @cmdexec_success_code=0,
  88.         @on_success_action=1,
  89.         @on_success_step_id=0,
  90.         @on_fail_action=2,
  91.         @on_fail_step_id=0,
  92.         @retry_attempts=0,
  93.         @retry_interval=0,
  94.         @os_run_priority=0, @subsystem=N'TSQL',
  95.         @command=N'--Объявляем переменные для названий файлов
  96. declare @remoteFolderPath nvarchar(100) = N''\\WIN-E0QK7UCTJ3T\ShareFolder\backups''
  97. declare @fullBackupFileName nvarchar(50)
  98. declare @diffBackupFileName nvarchar(50)
  99.  
  100. --получаем название файла дифф бекапа
  101. declare @cmdQuery nvarchar(255) = ''PowerShell.exe "ls -Path "//WIN-E0QK7UCTJ3T/ShareFolder/backups/diff"| Sort Name| Select -last 1 Name| Select-Object -ExpandProperty Name"''
  102.  
  103. create table #output (id int identity(1,1), output nvarchar(255) null)
  104. insert #output (output) exec xp_cmdshell @cmdQuery
  105.  
  106. select @diffBackupFileName = output from #output where len(output) > 0
  107.  
  108. --получаем название файла полного бекапа
  109. set @cmdQuery  = replace(@cmdQuery, N''diff'', N''full'')
  110.  
  111. delete from #output;
  112. insert #output (output) exec xp_cmdshell @cmdQuery
  113.  
  114. select @fullBackupFileName = output from #output where len(output) > 0
  115. drop table #output
  116.  
  117. --собираем пути к файлам бекапа
  118. declare @diffBackupFilePath nvarchar(100) = CONCAT(@remoteFolderPath, ''\diff\'',@diffBackupFileName)
  119. declare @fullBackupFilePath nvarchar(100) = CONCAT(@remoteFolderPath, ''\full\'',@fullBackupFileName)
  120. --declare @diffBackupFileNameWithoutExtention nvarchar(100) = replace(@diffBackupFileName, N''.bak'', N'''')
  121. --declare @fullBackupFileNameWithoutExtention nvarchar(100) = replace(@fullBackupFileName, N''.bak'', N'''')
  122. --select concat(@diffBackupFilePath, ''|'', @fullBackupFilePath)
  123.  
  124. --убиваем все подключения к бд и дропаем базу
  125. DECLARE @DatabaseName nvarchar(50)
  126. SET @DatabaseName = N''Laba6_copy''
  127. DECLARE @SQL varchar(max)
  128. SELECT @SQL = COALESCE(@SQL,'''') + ''Kill '' + Convert(varchar, SPId) + '';''
  129. FROM MASTER..SysProcesses
  130. WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId
  131. EXEC(@SQL)
  132. drop database if exists Laba6_copy
  133.  
  134. --ресторим
  135. restore database Laba6_copy --полный
  136. from disk = @fullBackupFilePath
  137. with file = 1, norecovery, replace,
  138. move N''Laba6''
  139. TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\full\Laba6.mdf'',
  140. move N''Laba6_log''
  141. TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\full\Laba6_log.mdf''
  142.  
  143. restore database Laba6_copy--дифф
  144. from disk = @diffBackupFilePath
  145. with file = 1, recovery--, replace,
  146. --move N''Laba6''
  147. --TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\diff\Laba6.mdf'',
  148. --move N''Laba6_log''
  149. --TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\diff\Laba6_log.mdf''',
  150.         @database_name=N'master',
  151.         @output_file_name=N'D:\backups\log.txt',
  152.         @flags=2
  153. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  154. EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
  155. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  156. EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Laba6_Task2_EveryDay',
  157.         @enabled=1,
  158.         @freq_type=4,
  159.         @freq_interval=1,
  160.         @freq_subday_type=1,
  161.         @freq_subday_interval=0,
  162.         @freq_relative_interval=0,
  163.         @freq_recurrence_factor=0,
  164.         @active_start_date=20231217,
  165.         @active_end_date=99991231,
  166.         @active_start_time=20000,
  167.         @active_end_time=235959,
  168.         @schedule_uid=N'1089eaa2-886c-4e09-9ce1-3d411e00a656'
  169. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  170. EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
  171. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  172. COMMIT TRANSACTION
  173. GOTO EndSave
  174. QuitWithRollback:
  175.     IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
  176. EndSave:
  177. GO
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement