标签:connect not proc 批量创建 arch ups line tar --
注意事项:
exec sp_configure ‘show advanced_options‘,1
reconfigure
exec sp_configure ‘xp_cmdshell‘,1
reconfigure
exec sp_configure ‘show advanced_options‘,0
reconfigure
go
USE [master]
GO
/****** Object: StoredProcedure [dbo].[pri_logshipping_create] Script Date: 2020/06/17 周三 21:44:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[pri_logshipping_create](
@pri_server nvarchar(40),
@pri_port nvarchar(10),
@std_server nvarchar(40),
@std_port nvarchar(10),
@ls_dir nvarchar(60), --主库共享目录 D:\log..
@interval nvarchar(3),
@ls_Databases nvarchar(300)=null, --要做logshipping的数据库
@nonls_databases nvarchar(300)= null
)
as
begin
-- create shared folders which are logshiping databases in pri and std
declare @db_name nvarchar(40)
declare @pri_mkdir_stmt nvarchar(100)
declare @std_mkdir_stmt nvarchar(100)
--declare @create_drive_stmt nvarchar(200)
--declare @drop_drive_stmt nvarchar(200)
declare @backup_stmt nvarchar(800)
declare @restore_stmt nvarchar(400)
declare @bak_time nvarchar(20)
declare @pri_instance nvarchar(40)=@pri_server+‘,‘+@pri_port
declare @std_instance nvarchar(40)=@std_server+‘,‘+@std_port
declare @pri_shared_dir nvarchar(100)=N‘\\‘+@pri_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @std_shared_dir nvarchar(100)=N‘\\‘+@std_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @pri_job_stmt nvarchar(max)
-- logshipping databases
if @ls_databases is null
begin
declare c1 cursor for select name from sys.databases
where database_id >4
and recovery_model_desc=‘FULL‘
and state_desc=‘ONLINE‘
and name not in(select value from string_split(@nonls_Databases,‘,‘))
end
else
begin
declare c1 cursor for select name from sys.databases
where database_id >4
and recovery_model_desc=‘FULL‘
and state_desc=‘ONLINE‘
and name in(select value from string_split(@ls_Databases,‘,‘))
end
/*
set @create_drive_stmt = ‘net use ‘+@pri_shared_dir+‘ /user:test password‘
print @create_drive_stmt
print @pri_shared_dir
print @std_shared_dir
*/
create table #temp_restore(stmt nvarchar(200))
open c1
fetch next from c1 into @db_name
while @@FETCH_STATUS=0
begin
set @bak_time = format(getdate(),‘yyyyMMdd_HHmmss‘)
set @pri_mkdir_stmt=‘mkdir ‘+@ls_dir+‘\‘+@db_name
set @std_mkdir_stmt=‘mkdir ‘+@std_shared_dir+‘\‘+@db_name
set @backup_stmt=‘BACKUP DATABASE ‘+@db_name+‘ TO disk = ‘‘‘+@std_shared_dir+‘\‘+@db_name+‘\‘+@db_name+‘_‘+@bak_time+
‘.bak‘‘ WITH NOFORMAT, NOINIT, NAME = ‘‘‘+@db_name+‘-Full Database Backup‘‘, SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10‘
set @restore_stmt=‘RESTORE DATABASE ‘+@db_name+‘ FROM disk =‘‘‘+@std_shared_dir+‘\‘+@db_name+‘\‘+@db_name+‘_‘+@bak_time+‘.bak‘‘ with norecovery‘
set nocount on
exec xp_cmdshell @pri_mkdir_stmt,no_output
exec xp_cmdshell @std_mkdir_stmt,no_output
exec sp_executesql @backup_stmt
insert into #temp_restore values(@restore_stmt)
/*
print @pri_mkdir_stmt
print @std_mkdir_stmt
print @backup_stmt
print @restore_stmt
*/
set @pri_job_stmt=‘
DECLARE @LS_BackupJobId AS uniqueidentifier
DECLARE @LS_PrimaryId AS uniqueidentifier
DECLARE @SP_Add_RetCode As int
EXEC @SP_Add_RetCode = master.dbo.sp_add_log_shipping_primary_database
@database = ‘‘‘+@db_name+‘‘‘
,@backup_directory = ‘‘‘ +@ls_dir+‘\‘+@db_name+‘‘‘
,@backup_share = ‘‘‘+@pri_shared_dir+‘\‘+@db_name+‘‘‘
,@backup_job_name = N‘‘LSBackup_‘+@db_name+‘‘‘
,@backup_retention_period = 4320
,@backup_compression = 1
,@backup_threshold = 60
,@threshold_alert_enabled = 1
,@history_retention_period = 5760
,@backup_job_id = @LS_BackupJobId OUTPUT
,@primary_id = @LS_PrimaryId OUTPUT
,@overwrite = 1
IF (@@ERROR = 0 AND @SP_Add_RetCode = 0)
BEGIN
DECLARE @LS_BackUpScheduleUID As uniqueidentifier
DECLARE @LS_BackUpScheduleID AS int
EXEC msdb.dbo.sp_add_schedule
@schedule_name =N‘‘LSBackupSchedule_‘+@pri_instance+‘_‘+@db_name+‘‘‘
,@enabled = 1
,@freq_type = 4
,@freq_interval = 1
,@freq_subday_type = 4
,@freq_subday_interval = ‘+@interval+‘
,@freq_recurrence_factor = 0
,@active_start_date = 20200615
,@active_end_date = 99991231
,@active_start_time = 0
,@active_end_time = 235900
,@schedule_uid = @LS_BackUpScheduleUID OUTPUT
,@schedule_id = @LS_BackUpScheduleID OUTPUT
EXEC msdb.dbo.sp_attach_schedule
@job_id = @LS_BackupJobId
,@schedule_id = @LS_BackUpScheduleID
EXEC msdb.dbo.sp_update_job
@job_id = @LS_BackupJobId
,@enabled = 1
END
EXEC master.dbo.sp_add_log_shipping_alert_job
EXEC master.dbo.sp_add_log_shipping_primary_secondary
@primary_database = N‘‘‘+@db_name+‘‘‘
,@secondary_server = N‘‘‘+@std_instance+‘‘‘
,@secondary_database = N‘‘‘+@db_name+‘‘‘
,@overwrite = 1‘
--print @pri_job_stmt
exec sp_executesql @pri_job_stmt
fetch next from c1 into @db_name
end
close c1
deallocate c1
select * from #temp_restore
drop table #temp_restore
end
GO
--删除主库配置
USE [master]
GO
/****** Object: StoredProcedure [dbo].[del_pri_logshipping] Script Date: 2020/06/17 周三 21:46:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[del_pri_logshipping](
@pri_server nvarchar(40),
@pri_port nvarchar(10),
@std_server nvarchar(40),
@std_port nvarchar(10),
@ls_dir nvarchar(60), --主库共享目录 D:\log..
@is_del nvarchar(1)=0, --0不删除文件夹,1删除文件夹,默认0
@ls_Databases nvarchar(300)=null, --指定要删除的库
@nonls_databases nvarchar(300)= null
)
as
begin
-- create shared folders which are logshiping databases in pri and std
declare @db_name nvarchar(40)
declare @pri_instance nvarchar(40)=@pri_server+‘,‘+@pri_port
declare @std_instance nvarchar(40)=@std_server+‘,‘+@std_port
declare @pri_shared_dir nvarchar(100)=N‘\\‘+@pri_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @std_shared_dir nvarchar(100)=N‘\\‘+@std_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @pri_dir_del_stmt nvarchar(100)
declare @std_dir_del_stmt nvarchar(100)
-- logshipping databases
if @ls_databases is null
begin
declare c1 cursor for select primary_database
from msdb..log_shipping_primary_databases
where primary_database not in(select value from string_split(@nonls_Databases,‘,‘))
end
else
begin
declare c1 cursor for select primary_database
from msdb..log_shipping_primary_databases
where primary_database in(select value from string_split(@ls_Databases,‘,‘))
end
open c1
fetch next from c1 into @db_name
while @@FETCH_STATUS=0
begin
exec sp_delete_log_shipping_primary_secondary
@primary_database=@db_name,
@secondary_server=@std_instance,
@secondary_database=@db_name
exec sp_delete_log_shipping_primary_database @database=@db_name
if @is_del=‘1‘
begin
set @pri_dir_del_stmt=N‘rd /S /Q ‘+@ls_dir+‘\‘+@db_name
set @std_dir_del_stmt=N‘rd /S /Q ‘+@std_shared_dir+‘\‘+@db_name
exec xp_cmdshell @pri_dir_del_stmt,no_output
exec xp_cmdshell @std_dir_del_stmt,no_output
end
fetch next from c1 into @db_name
end
close c1
deallocate c1
end
GO
--先使用主库产生的语句恢复备库数据库
create proc std_logshipping_create(
@pri_server nvarchar(40),
@pri_port nvarchar(10),
@std_server nvarchar(40),
@std_port nvarchar(10),
@ls_dir nvarchar(60), --主库共享目录 c:\logshipping
@interval nvarchar(3),
@ls_Databases nvarchar(300)=null, --要做logshipping的数据库
@nonls_databases nvarchar(300)= null
)
as
begin
if @ls_databases is null
begin
declare c1 cursor for select name from sys.databases
where database_id >4
and state_desc=‘RESTORING‘
and name not in(select value from string_split(@nonls_Databases,‘,‘))
end
else
begin
declare c1 cursor for select name from sys.databases
where database_id >4
and state_desc=‘RESTORING‘
and name in(select value from string_split(@ls_Databases,‘,‘))
end
declare @db_name nvarchar(40)
--declare @create_drive_stmt nvarchar(200)
--declare @drop_drive_stmt nvarchar(200)
declare @pri_instance nvarchar(40)=@pri_server+‘,‘+@pri_port
declare @std_instance nvarchar(40)=@std_server+‘,‘+@std_port
declare @pri_shared_dir nvarchar(100)=N‘\\‘+@pri_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @std_shared_dir nvarchar(100)=N‘\\‘+@std_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @std_job_stmt nvarchar(max)
open c1
fetch next from c1 into @db_name
while @@FETCH_STATUS=0
begin
set @std_job_stmt=‘
DECLARE @LS_Secondary__CopyJobId AS uniqueidentifier
DECLARE @LS_Secondary__RestoreJobId AS uniqueidentifier
DECLARE @LS_Secondary__SecondaryId AS uniqueidentifier
DECLARE @LS_Add_RetCode As int
EXEC @LS_Add_RetCode = master.dbo.sp_add_log_shipping_secondary_primary
@primary_server = N‘‘‘+@pri_instance+‘‘‘
,@primary_database = N‘‘‘+@db_name+‘‘‘
,@backup_source_directory = N‘‘‘+@pri_shared_dir+‘\‘+@db_name+‘‘‘
,@backup_destination_directory = N‘‘‘+@std_shared_dir+‘\‘+@db_name+‘‘‘
,@copy_job_name = N‘‘LSCopy_‘+@pri_instance+‘_‘+@db_name+‘‘‘
,@restore_job_name = N‘‘LSRestore_‘+@pri_instance+‘_‘+@db_name+‘‘‘
,@file_retention_period = 4320
,@overwrite = 1
,@copy_job_id = @LS_Secondary__CopyJobId OUTPUT
,@restore_job_id = @LS_Secondary__RestoreJobId OUTPUT
,@secondary_id = @LS_Secondary__SecondaryId OUTPUT
IF (@@ERROR = 0 AND @LS_Add_RetCode = 0)
BEGIN
DECLARE @LS_SecondaryCopyJobScheduleUID As uniqueidentifier
DECLARE @LS_SecondaryCopyJobScheduleID AS int
EXEC msdb.dbo.sp_add_schedule
@schedule_name =N‘‘LSCopySchedule_‘+@pri_instance+‘_‘+@db_name+‘‘‘
,@enabled = 1
,@freq_type = 4
,@freq_interval = 1
,@freq_subday_type = 4
,@freq_subday_interval = ‘+@interval+‘
,@freq_recurrence_factor = 0
,@active_start_date = 20200615
,@active_end_date = 99991231
,@active_start_time = 0
,@active_end_time = 235900
,@schedule_uid = @LS_SecondaryCopyJobScheduleUID OUTPUT
,@schedule_id = @LS_SecondaryCopyJobScheduleID OUTPUT
EXEC msdb.dbo.sp_attach_schedule
@job_id = @LS_Secondary__CopyJobId
,@schedule_id = @LS_SecondaryCopyJobScheduleID
DECLARE @LS_SecondaryRestoreJobScheduleUID As uniqueidentifier
DECLARE @LS_SecondaryRestoreJobScheduleID AS int
EXEC msdb.dbo.sp_add_schedule
@schedule_name =N‘‘LSRestoreSchedule_‘+@pri_instance+‘_‘+@db_name+‘‘‘
,@enabled = 1
,@freq_type = 4
,@freq_interval = 1
,@freq_subday_type = 4
,@freq_subday_interval = ‘+@interval+‘
,@freq_recurrence_factor = 0
,@active_start_date = 20200615
,@active_end_date = 99991231
,@active_start_time = 0
,@active_end_time = 235900
,@schedule_uid = @LS_SecondaryRestoreJobScheduleUID OUTPUT
,@schedule_id = @LS_SecondaryRestoreJobScheduleID OUTPUT
EXEC msdb.dbo.sp_attach_schedule
@job_id = @LS_Secondary__RestoreJobId
,@schedule_id = @LS_SecondaryRestoreJobScheduleID
END
DECLARE @LS_Add_RetCode2 As int
IF (@@ERROR = 0 AND @LS_Add_RetCode = 0)
BEGIN
EXEC @LS_Add_RetCode2 = master.dbo.sp_add_log_shipping_secondary_database
@secondary_database = N‘‘‘+@db_name+‘‘‘
,@primary_server = N‘‘‘+@pri_instance+‘‘‘
,@primary_database = N‘‘‘+@db_name+‘‘‘
,@restore_delay = 0
,@restore_mode = 0
,@disconnect_users = 0
,@restore_threshold = 45
,@threshold_alert_enabled = 1
,@history_retention_period = 5760
,@overwrite = 1
END
IF (@@error = 0 AND @LS_Add_RetCode = 0)
BEGIN
EXEC msdb.dbo.sp_update_job
@job_id = @LS_Secondary__CopyJobId
,@enabled = 1
EXEC msdb.dbo.sp_update_job
@job_id = @LS_Secondary__RestoreJobId
,@enabled = 1
END ‘
--print @std_job_stmt
exec sp_executesql @std_job_stmt
fetch next from c1 into @db_name
end
close c1
deallocate c1
end
--删除备库配置
go
USE [master]
GO
/****** Object: StoredProcedure [dbo].[del_std_logshipping] Script Date: 2020/06/17 周三 00:42:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[del_std_logshipping](
@pri_server nvarchar(40),
@pri_port nvarchar(10),
@std_server nvarchar(40),
@std_port nvarchar(10),
@ls_dir nvarchar(60), --主库共享目录 D:\log..
@is_del nvarchar(1)=‘0‘, --0不删除文件夹,1删除文件夹,默认0
@ls_Databases nvarchar(300)=null, --指定要删除的库
@nonls_databases nvarchar(300)= null
)
as
begin
-- create shared folders which are logshiping databases in pri and std
declare @db_name nvarchar(40)
declare @pri_instance nvarchar(40)=@pri_server+‘,‘+@pri_port
declare @std_instance nvarchar(40)=@std_server+‘,‘+@std_port
declare @pri_shared_dir nvarchar(100)=N‘\\‘+@pri_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @std_shared_dir nvarchar(100)=N‘\\‘+@std_server+‘\‘+SUBSTRING(@ls_dir,4,len(@ls_dir)-3)
declare @drop_db_stmt nvarchar(60)
-- logshipping databases
if @ls_databases is null
begin
declare c1 cursor for select secondary_database
from msdb..log_shipping_secondary_databases
where secondary_database not in(select value from string_split(@nonls_Databases,‘,‘))
end
else
begin
declare c1 cursor for select secondary_database
from msdb..log_shipping_secondary_databases
where secondary_database in(select value from string_split(@ls_Databases,‘,‘))
end
open c1
fetch next from c1 into @db_name
while @@FETCH_STATUS=0
begin
exec sp_delete_log_shipping_secondary_database @secondary_database=@db_name
exec sp_delete_log_shipping_secondary_primary
@primary_server=@pri_instance,
@primary_database=@db_name
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = @db_name
if @is_del=‘1‘
begin
set @drop_db_stmt=‘drop database ‘+@db_name
exec sp_executesql @drop_db_stmt
end
fetch next from c1 into @db_name
end
close c1
deallocate c1
end
GO
exec pri_logshipping_create
@pri_server=‘192.168.70.151‘,
@pri_port=‘1433‘,
@std_server=‘192.168.70.152‘,
@std_port=‘1433‘,
@ls_dir=‘C:\logshipping‘, --主库共享目录
@interval=1,
@ls_Databases=null, --逗号分隔
@nonls_Databases=null
--复制产生的恢复语句至备库执行
--执行主库产生的恢复语句
exec std_logshipping_create
@pri_server=‘192.168.70.151‘,
@pri_port=‘1433‘,
@std_server=‘192.168.70.152‘,
@std_port=‘1433‘,
@ls_dir=‘C:\logshipping‘, --主库共享目录
@interval=1
exec del_std_logshipping
@pri_server=‘192.168.70.151‘,
@pri_port=‘1433‘,
@std_server=‘192.168.70.152‘,
@std_port=‘1433‘,
@ls_dir=‘C:\logshipping‘, --主库共享目录
@is_del=‘1‘, --是否删除数据.0-否,默认.1-是
@ls_Databases=null,
@nonls_Databases=null
exec del_pri_logshipping
@pri_server=‘192.168.70.151‘,
@pri_port=‘1433‘,
@std_server=‘192.168.70.152‘,
@std_port=‘1433‘,
@ls_dir=‘C:\logshipping‘, --主库共享目录
@is_del=‘1‘,
@ls_Databases=null,
@nonls_Databases=null
标签:connect not proc 批量创建 arch ups line tar --
原文地址:https://www.cnblogs.com/ls11736/p/13931208.html