亚洲综合社区欧美综合色-欧美逼逼一区二区三区-国产老熟女高潮精品网站-国产日韩最新视频在线看

始創(chuàng)于2000年 股票代碼:831685
咨詢熱線:0371-60135900 注冊有禮 登錄
  • 掛牌上市企業(yè)
  • 60秒人工響應(yīng)
  • 99.99%連通率
  • 7*24h人工
  • 故障100倍補(bǔ)償
全部產(chǎn)品
您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

如何還原某一個(gè)目錄下的所有數(shù)據(jù)庫備份文件(2)

發(fā)布時(shí)間:  2012/6/16 21:37:58

●還原單個(gè)數(shù)據(jù)庫的存儲過程代碼

根據(jù)上面分析的方法,這里演示我寫的一個(gè)還原單個(gè)數(shù)據(jù)庫的存儲過程代碼,因?yàn)榇a是之前寫的,中間因真實(shí)世界的特殊情況,修改成幾個(gè)版本的存儲過程。當(dāng)然,如有可能,你可以根據(jù)自己所在的真實(shí)環(huán)境,修改對應(yīng)部分的代碼,以便滿足自己的需要。

存儲過程sp_RestoreDataBase代碼:

  1. Use master
  2. Go
  3. if object_ID('[sp_restoredatabase]') is not null
  4. Drop Procedure [sp_restoredatabase]
  5. Go
  6. /*
  7. --還原數(shù)據(jù)庫(v3.2) Andy 2011-2-22
  8. @DatabBaseBakPath nvarchar(260), --數(shù)據(jù)庫備份路徑(包含備份文件名)
  9. @DatabBaseNewPath nvarchar(260) --新數(shù)據(jù)庫路徑
  10. @NewDataBaseName nvarchar(128) --新數(shù)據(jù)庫名
  11. e.g:
  12. --Exec sp_RestoreDataBase 'E:\DatabaseBackup\DE\Support_2008722_14_33_39.bak','D:\SQL2005\DE'
  13. V3.0版本修改說明:
  14. 1.修正了之前版本在還原包含全文索引文件的時(shí)候發(fā)生的錯誤。
  15. 2.修正了之前版本在還原包含多個(gè)數(shù)據(jù)庫文件和日志文件時(shí)發(fā)生的錯誤。
  16. V3.1版本說明,增加了參數(shù)@Keep_Replication,表示是否保存復(fù)制設(shè)定
  17. V3.2版本說明,增加了FILE ={ file_number | @file_number } 邏輯判斷,在包含多個(gè)備份組,還原最后一個(gè)備份組
  18. V3.3 版本說明 ,還原發(fā)生錯誤時(shí)返回 1
  19. */
  20. CREATE Proc sp_RestoreDataBase
  21. (
  22. @DatabBaseBakPath nvarchar(260),
  23. @DatabBaseNewPath nvarchar(260),
  24. @NewDataBaseName nvarchar(128)=null,
  25. @Keep_Replication bit=0
  26. )
  27. As
  28. Set Nocount On
  29. Begin Try
  30. Declare
  31. @DataBaseName nvarchar(128),
  32. @Sql nvarchar(max),
  33. @SqlDatabaseRename nvarchar(max),
  34. @Enter nvarchar(10)
  35. --檢查文件路徑是否正確
  36. Declare
  37. @Dir nvarchar(4000),
  38. @i int
  39. -- Set @Dir='Dir '+@DatabBaseBakPath
  40. -- Exec @i=xp_cmdshell @Dir,no_output
  41. -- If @i<>0
  42. Exec master.dbo.xp_fileexist @DatabBaseBakPath,@i Output
  43. If @i=0
  44. Begin
  45. Raiserror 50001 N'無效的備份數(shù)據(jù)庫路徑/文件名!'
  46. Return 1
  47. End
  48. If Charindex('\\',@DatabBaseNewPath)>0
  49. Begin
  50. Raiserror 50001 N'數(shù)據(jù)庫還原路徑中不能含有''\\''!'
  51. Return 1
  52. End
  53. If Right(Rtrim(@DatabBaseNewPath),1)='\'
  54. Begin
  55. Raiserror 50001 N'數(shù)據(jù)庫還原路徑的最后一位能含有''\''!'
  56. Return 1
  57. End
  58. Set @Dir='Dir '+@DatabBaseNewPath
  59. Exec @i=xp_cmdshell @Dir,no_output
  60. If @i<>0
  61. Begin
  62. Raiserror 50001 N'無效的數(shù)據(jù)庫還原路徑!'
  63. Return 1
  64. End
  65. set @DatabBaseNewPath=replace(@DatabBaseNewPath,'"','')
  66. /*
  67. --SQL Server 2005
  68. Declare @BakFileList Table(LogicalName nvarchar(128),PhysicalName nvarchar(260),Type char(1),FileGroupName nvarchar(128),Size numeric(20,0),MaxSize numeric(20,0),FileID bigint,CreateLSN numeric(25,0),DropLSN numeric(25,0) NULL,UniqueID uniqueidentifier,ReadOnlyLSN numeric(25,0) NULL,ReadWriteLSN numeric(25,0) NULL,BackupSizeInBytes bigint,SourceBlockSize int,FileGroupID int,LogGroupGUID uniqueidentifier NULL,DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,IsReadOnly bit,IsPresent bit)
  69. */
  70. --SQL Server 2008
  71. Declare @BakFileList Table(LogicalName nvarchar(128),PhysicalName nvarchar(260),Type char(1),FileGroupName nvarchar(128),Size numeric(20,0),MaxSize numeric(20,0),FileID bigint,CreateLSN numeric(25,0),DropLSN numeric(25,0) NULL,UniqueID uniqueidentifier,ReadOnlyLSN numeric(25,0) NULL,ReadWriteLSN numeric(25,0) NULL,BackupSizeInBytes bigint,SourceBlockSize int,FileGroupID int,LogGroupGUID uniqueidentifier NULL,DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,IsReadOnly bit,IsPresent bit,TDEThumbprint varbinary(32))
  72. Insert Into @BakFileList
  73. Exec sp_executesql N'Restore FileListOnly From Disk=@DatabBaseBakPath',N'@DatabBaseBakPath nvarchar(260)',@DatabBaseBakPath
  74. /*
  75. --SQL Server 2005
  76. Declare @BakHeaderInfo Table(BackupName nvarchar(128),BackupDescription nvarchar(255),BackupType smallint,ExpirationDate datetime,Compressed tinyint,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) NULL,RecoveryModel nvarchar(60),DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,BackupTypeDescription nvarchar(60),BackupSetGUID uniqueidentifier NULL)
  77. */
  78. --SQL Server 2008
  79. Declare @BakHeaderInfo Table(BackupName nvarchar(128),BackupDescription nvarchar(255),BackupType smallint,ExpirationDate datetime,Compressed tinyint,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) NULL,RecoveryModel nvarchar(60),DifferentialBaseLSN numeric(25,0) NULL,DifferentialBaseGUID uniqueidentifier,BackupTypeDescription nvarchar(60),BackupSetGUID uniqueidentifier NULL,CompressedBackupSize numeric(20,0))
  80. Insert Into @BakHeaderInfo
  81. Exec sp_executesql N'Restore HeaderOnly From Disk=@DatabBaseBakPath',N'@DatabBaseBakPath nvarchar(260)',@DatabBaseBakPath
  82. If Isnull(@NewDataBaseName,'')>'' --使用新的數(shù)據(jù)庫名,要是沒有指定就使用原來的數(shù)據(jù)庫名
  83. Set @DataBaseName=@NewDataBaseName
  84. Else
  85. Begin
  86. Select @DataBaseName=DatabaseName From @BakHeaderInfo
  87. End
  88. Set @Enter=char(13)+Char(10)
  89. Select @Sql=Isnull(@Sql+@Enter,'')+'Kill '+Rtrim(spid) From master.sys.sysprocesses Where dbid=db_id(@DataBaseName)
  90. Exec(@Sql)
  91. Set @Sql=N'Restore DataBase @DataBaseName From Disk=@DatabBaseBakPath With File=??,' --+(Select 'File='+rtrim(max(Position))+', ' From @BakHeaderInfo)
  92. Select @Sql=@Sql+'Move '''+LogicalName+''' To '''+@DatabBaseNewPath+'\'+@DataBaseName+
  93. Case
  94. When [Type]='D' And Not Exists(Select 1 From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID) Then ''
  95. When [Type]='D' Then '_'+(Select Rtrim(Count(*)) From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID)
  96. When [Type]='L' And Not Exists(Select 1 From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID) Then '_Log'
  97. When [Type]='L' Then '_Log'+(Select Rtrim(Count(*)) From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID)
  98. Else ''
  99. End+Right(PhysicalName,Charindex('.',Reverse(PhysicalName)))+''',',
  100. @SqlDatabaseRename=Isnull(@SqlDatabaseRename+@Enter,'')+
  101. Case
  102. When [Type]='D' And LogicalName=@DataBaseName Then ''
  103. When [Type]='D' And LogicalName Like @DataBaseName+'[_]%' Then ''
  104. When [Type]='L' And LogicalName Like @DataBaseName+'[_]Log%' Then ''
  105. When [Type]='F' Then ''
  106. Else
  107. 'Alter DataBase '+Quotename(@DataBaseName)+' Modify File(Name='''+LogicalName+''',NewName='''+@DataBaseName+
  108. Case
  109. When [Type]='D' And Not Exists(Select 1 From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID) Then ''
  110. When [Type]='D' Then '_'+(Select Rtrim(Count(*)) From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID)
  111. When [Type]='L' And Not Exists(Select 1 From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID) Then '_Log'
  112. When [Type]='L' Then '_Log'+(Select Rtrim(Count(*)) From @BakFileList Where [Type]=a.[Type] And FileID<a.FileID)
  113. Else ''
  114. End+''')'
  115. End
  116. From @BakFileList As a
  117. Set @Sql=@Sql+'Replace'
  118. If @Keep_Replication=1
  119. Set @Sql=@Sql+'Keep_Replication'
  120. Declare @sql1 nvarchar(max),
  121. @MaxPosition int
  122. Select @MaxPosition=Position
  123. From @BakHeaderInfo As a
  124. Where a.BackupType=1
  125. And Not Exists(Select 1 From @BakHeaderInfo Where BackupType=a.BackupType And Position>a.Position)
  126. Select @sql1=isnull(@sql1+char(13)+char(10),'')+replace(@sql,'??',rtrim(Position)) +
  127. Case When Exists(Select 1 From @BakHeaderInfo Where Position>a.Position) Then N',Norecovery' Else N',Recovery' End
  128. From @BakHeaderInfo As a
  129. Where Position>=@MaxPosition
  130. Order By Position
  131. Print '還原數(shù)據(jù)庫: '+@DataBaseName
  132. -- Print @sql1
  133. Exec sp_executesql @sql1,N'@DataBaseName nvarchar(128),@DatabBaseBakPath nvarchar(260)',@DataBaseName,@DatabBaseBakPath
  134. If @SqlDatabaseRename>''
  135. Exec(@SqlDatabaseRename)
  136. End Try
  137. Begin Catch
  138. Declare @Error nvarchar(1024)
  139. Set @Error=ERROR_MESSAGE()
  140. Raiserror 50001 @Error
  141. Return 1
  142. End Catch
  143. Set Nocount Off
  144. Go

存儲過程測試:

  1. use master
  2. Go
  3. Exec dbo.sp_RestoreDataBase 'E:\DBBackup\dbA2011-09-05.bak','E:\DATA\SQL2008DE01'
  4. go


更多請聯(lián)系:億恩科技
QQ:1613285598   0371-63322220


本文出自:億恩科技【1tcdy.com】

服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營性ICP/ISP證:贛B2-20080012
  • 服務(wù)器/云主機(jī) 24小時(shí)售后服務(wù)電話:0371-60135900
  • 虛擬主機(jī)/智能建站 24小時(shí)售后服務(wù)電話:0371-60135900
  • 專注服務(wù)器托管17年
    掃掃關(guān)注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權(quán)所有  地址:鄭州市高新區(qū)翠竹街1號總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務(wù)所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號
      1
     
     
     
     

    0371-60135900
    7*24小時(shí)客服服務(wù)熱線