Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [msdb]
- GO
- /****** Object: Job [Laba6_Task2] Script Date: 12/17/2023 1:10:59 AM ******/
- EXEC msdb.dbo.sp_delete_job @job_id=N'eabd4aa0-480e-4bb3-9aae-c3a0f8aaf711', @delete_unused_schedule=1
- GO
- /****** Object: Job [Laba6_Task2] Script Date: 12/17/2023 1:10:59 AM ******/
- BEGIN TRANSACTION
- DECLARE @ReturnCode INT
- SELECT @ReturnCode = 0
- /****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 12/17/2023 1:10:59 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_Task2',
- @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:10:59 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/diff/*.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:10:59 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\diff\diff'',@datetimepart,''.bak'')
- BACKUP DATABASE Laba6
- TO DISK = @backupFilePath
- with DIFFERENTIAL',
- @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:10:59 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=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'cp D:\backups\diff\*.bak -Destination Microsoft.Powershell.Core\FileSystem:://WIN-E0QK7UCTJ3T/ShareFolder/backups/diff',
- @database_name=N'master',
- @output_file_name=N'D:\backups\log.txt',
- @flags=2
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** Object: Step [Restore database Laba6_copy] Script Date: 12/17/2023 1:10:59 AM ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Restore database Laba6_copy',
- @step_id=4,
- @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 @remoteFolderPath nvarchar(100) = N''\\WIN-E0QK7UCTJ3T\ShareFolder\backups''
- declare @fullBackupFileName nvarchar(50)
- declare @diffBackupFileName nvarchar(50)
- --получаем название файла дифф бекапа
- declare @cmdQuery nvarchar(255) = ''PowerShell.exe "ls -Path "//WIN-E0QK7UCTJ3T/ShareFolder/backups/diff"| Sort Name| Select -last 1 Name| Select-Object -ExpandProperty Name"''
- create table #output (id int identity(1,1), output nvarchar(255) null)
- insert #output (output) exec xp_cmdshell @cmdQuery
- select @diffBackupFileName = output from #output where len(output) > 0
- --получаем название файла полного бекапа
- set @cmdQuery = replace(@cmdQuery, N''diff'', N''full'')
- delete from #output;
- insert #output (output) exec xp_cmdshell @cmdQuery
- select @fullBackupFileName = output from #output where len(output) > 0
- drop table #output
- --собираем пути к файлам бекапа
- declare @diffBackupFilePath nvarchar(100) = CONCAT(@remoteFolderPath, ''\diff\'',@diffBackupFileName)
- declare @fullBackupFilePath nvarchar(100) = CONCAT(@remoteFolderPath, ''\full\'',@fullBackupFileName)
- --declare @diffBackupFileNameWithoutExtention nvarchar(100) = replace(@diffBackupFileName, N''.bak'', N'''')
- --declare @fullBackupFileNameWithoutExtention nvarchar(100) = replace(@fullBackupFileName, N''.bak'', N'''')
- --select concat(@diffBackupFilePath, ''|'', @fullBackupFilePath)
- --убиваем все подключения к бд и дропаем базу
- DECLARE @DatabaseName nvarchar(50)
- SET @DatabaseName = N''Laba6_copy''
- DECLARE @SQL varchar(max)
- SELECT @SQL = COALESCE(@SQL,'''') + ''Kill '' + Convert(varchar, SPId) + '';''
- FROM MASTER..SysProcesses
- WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId
- EXEC(@SQL)
- drop database if exists Laba6_copy
- --ресторим
- restore database Laba6_copy --полный
- from disk = @fullBackupFilePath
- with file = 1, norecovery, replace,
- move N''Laba6''
- TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\full\Laba6.mdf'',
- move N''Laba6_log''
- TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\full\Laba6_log.mdf''
- restore database Laba6_copy--дифф
- from disk = @diffBackupFilePath
- with file = 1, recovery--, replace,
- --move N''Laba6''
- --TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\diff\Laba6.mdf'',
- --move N''Laba6_log''
- --TO ''C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Backup\diff\Laba6_log.mdf''',
- @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_Task2_EveryDay',
- @enabled=1,
- @freq_type=4,
- @freq_interval=1,
- @freq_subday_type=1,
- @freq_subday_interval=0,
- @freq_relative_interval=0,
- @freq_recurrence_factor=0,
- @active_start_date=20231217,
- @active_end_date=99991231,
- @active_start_time=20000,
- @active_end_time=235959,
- @schedule_uid=N'1089eaa2-886c-4e09-9ce1-3d411e00a656'
- 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