首页 > 其他 > 详细

批量还原V2

时间:2020-04-06 15:24:09      阅读:74      评论:0      收藏:0      [点我收藏+]

--批量还原完整备份

declare  @dbname varchar(500)
        ,@filepath varchar(500)
declare @sqltxt varchar(max)
declare @filename varchar(1000)

SET NOCOUNT on

set @filepath = D:\tmp\

set @sqltxt = xp_cmdshell ‘‘forfiles -p +@filepath+ /s /M *.full /D +convert(char(10),getdate()-10,111)+ -C "cmd /c echo @path"‘‘‘


create table #tb(fname varchar(500))
insert into #tb
exec (@sqltxt)
delete from #tb where fname is null
update #tb set fname = replace(fname,",‘‘)
from #tb

create table #lsntb
(
BackupName nvarchar(128)
,BackupDescription nvarchar(255)
,BackupType smallint
,ExpirationDate datetime
,Compressed int
,Position smallint
,DeviceType tinyint
,UserName    nvarchar(128)
,ServerName    nvarchar(128)
,DatabaseName    nvarchar(128)
,DatabaseVersion    int
,DatabaseCreationDate    datetime
,BackupSize    numeric(20,0)
,FirstLSN    numeric(25,0)
,LastLSN    numeric(25,0)
,CheckpointLSN    numeric(25,0)
,DatabaseBackupLSN    numeric(25,0)
,BackupStartDate    datetime
,BackupFinishDate    datetime
,SortOrder    smallint
,CodePage    smallint
,UnicodeLocaleId    int
,UnicodeComparisonStyle    int
,CompatibilityLevel    tinyint
,SoftwareVendorId    int
,SoftwareVersionMajor    int
,SoftwareVersionMinor    int
,SoftwareVersionBuild    int
,MachineName    nvarchar(128)
,flags    int
,BindingID    uniqueidentifier
,RecoveryForkID    uniqueidentifier
,collation    nvarchar(128)
,FamilyGUID    uniqueidentifier
,HasBulkLoggedData    bit
,IsSnapshot    bit
,IsReadOnly    bit
,IsSingleUser    bit
,HasBackupChecksums    bit
,IsDamaged    bit
,BeginsLogChain    bit
,HasIncompleteMetaData    bit
,IsForceOffline    bit
,IsCopyOnly    bit
,FirstRecoveryForkID    uniqueidentifier
,ForkPointLSN    numeric(25,0)
,RecoveryModel    nvarchar(60)
,DifferentialBaseLSN    numeric(25,0)
,DifferentialBaseGUID    uniqueidentifier
,BackupTypeDescription    nvarchar(60)
,BackupSetGUID    uniqueidentifier
,CompressedBackupSize    bigint
,containment    tinyint
)

create table #lastres(dbname varchar(500), filepath varchar(500),firstlsn numeric(25,0), lastlsn numeric(25,0),backupstartdate datetime)


declare mycursor cursor for select fname from #tb where fname like %.full
open mycursor
fetch next from mycursor into @filename 
while @@FETCH_STATUS = 0
begin 
    set @sqltxt = restore HEADERONLY  from disk = ‘‘‘+@filename+‘‘‘‘
    truncate table #lsntb
    begin try
        insert into #lsntb
        exec (@sqltxt)

        insert into #lastres
        select DatabaseName,@filename,FirstLSN,LastLSN,BackupStartDate
        from #lsntb
    end try
    begin catch
        print -------------
        print ERROR_MESSAGE()
        print @sqltxt
        print -------------
    end catch
    fetch next from mycursor into @filename
end
close mycursor
deallocate mycursor

declare restore_cur cursor for
select dbname,filepath from #lastres

open restore_cur
fetch next from restore_cur into @dbname,@filename
while @@FETCH_STATUS = 0
begin 
    set @sqltxt = restore database [+@dbname+] from disk = ‘‘‘+@filename+‘‘‘ with norecovery
    print @sqltxt
    --exec (@sqltxt)
    fetch next from restore_cur into  @dbname,@filename
end
close restore_cur
deallocate restore_cur

drop table #lastres
drop table #tb
drop table #lsntb

 

批量还原V2

原文:https://www.cnblogs.com/luck001221/p/12642031.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!