备份与还原是数据库避不开的主题,而作为DBA,经常会面临将一台机器上的所有数据库重新构建到一台新机器上的要求; 在现在都讲究自动化管理的时代,传统的界面操作备份还原的做法不仅浪费时间和精力,而且还很容易出错,并且这次完成后, 下次再有这样的要求
备份与还原是数据库避不开的主题,而作为DBA,经常会面临将一台机器上的所有数据库重新构建到一台新机器上的要求;
在现在都讲究自动化管理的时代,传统的界面操作备份还原的做法不仅浪费时间和精力,而且还很容易出错,并且这次完成后,
下次再有这样的要求,必须又重头开始(估计做5次就能做得人狂吐);于是,我们需要一套应对这种频繁操作、耗时、耗精力
的通用处理方法,所以以下批处理脚本就诞生了。
脚本主要的功能:
1. 备份一个服务器上的所有数据库(当然你也可以选择),备份文件按数据库名+日期生成,以.bak 结尾;
2. 将所有的备份文件还原到一台新机器上;
3. 验证磁盘和路径的正确性;
说明:
脚本合适 SQLServer 2005 & 2008 版本;
批量备份数据库:
<span>--</span><span>---------------------------批量备份数据-------------------------------------------</span><span><br></span><span>Use</span><span> master<br></span><span>GO</span><span><br></span><span>/*</span><span>=================Usp_BackUp_DataBase========================<br> =====BackUp Sigle DataBase ======<br> =====Ken.Guo ======<br> =====2010.9.10 ======<br> =====Version: 2005 & 2008 SQL Server ======<br> =====EXEC Usp_BackUp_DataBase 'MyDB','D:\BackUp' ======<br> ============================================================<br></span><span>*/</span><span><br></span><span>CREATE</span><span>PROC</span><span>[</span><span>dbo</span><span>]</span><span>.</span><span>[</span><span>Usp_BackUp_DataBase</span><span>]</span><span>@DatabaseName</span><span>nvarchar</span><span>(</span><span>200</span><span>),</span><span>@Path</span><span>nvarchar</span><span>(</span><span>200</span><span>) <br></span><span>AS</span><span> <br> </span><span>BEGIN</span><span> <br> </span><span>DECLARE</span><span>@fn</span><span>varchar</span><span>(</span><span>200</span><span>)<br> ,</span><span>@sql</span><span>varchar</span><span>(</span><span>1000</span><span>) <br> </span><span>SET</span><span>@fn</span><span>=</span><span>@Path</span><span>+</span><span>(</span><span>case</span><span>when</span><span>right</span><span>(</span><span>@Path</span><span>,</span><span>1</span><span>) </span><span></span><span>'</span><span>\</span><span>'</span><span>then</span><span>'</span><span>\</span><span>'</span><span>else</span><span>''</span><span>end</span><span>) <br> </span><span>+</span><span>@DatabaseName</span><span>+</span><span>'</span><span>_</span><span>'</span><span> <br> </span><span>+</span><span>convert</span><span>(</span><span>char</span><span>(</span><span>8</span><span>),</span><span>getdate</span><span>(),</span><span>112</span><span>)</span><span>+</span><span>'</span><span>_</span><span>'</span><span> <br> </span><span>+</span><span>replace</span><span>(</span><span>convert</span><span>(</span><span>char</span><span>(</span><span>8</span><span>),</span><span>getdate</span><span>(),</span><span>108</span><span>),</span><span>'</span><span>:</span><span>'</span><span>,</span><span>''</span><span>) <br> </span><span>+</span><span>'</span><span>.bak</span><span>'</span><span> <br> </span><span>set</span><span>@sql</span><span>=</span><span>'</span><span>backup database </span><span>'</span><span>+</span><span>@DatabaseName</span><span>+</span><span>'</span><span> to disk = N</span><span>'''</span><span>+</span><span>@fn</span><span>+</span><span>''''</span><span> <br> </span><span>--</span><span>SELECT @sql </span><span><br></span><span>EXEC</span><span>(</span><span>@sql</span><span>) <br> </span><span>END</span><span><br><br></span><span>GO</span><span><br><br></span><span>Use</span><span> master<br></span><span>GO</span><span><br></span><span>/*</span><span>=============BackUp Mutile DataBase=========================</span><span>*/</span><span><br></span><span>DECLARE</span><span>@dbname</span><span>nvarchar</span><span>(</span><span>200</span><span>)<br> ,</span><span>@backup_path</span><span>nvarchar</span><span>(</span><span>200</span><span>)<br></span><span>SET</span><span>@backup_path</span><span>=</span><span>'</span><span>D:\BackUp\</span><span>'</span><span><br></span><span>DECLARE</span><span> db_info </span><span>CURSOR</span><span> <br> LOCAL <br> STATIC <br> READ_ONLY <br> FORWARD_ONLY <br></span><span>FOR</span><span>--</span><span>根据查询,添加其他筛选条件</span><span><br></span><span>SELECT</span><span> <br> name <br> </span><span>FROM</span><span> master.sys.databases </span><span>WITH</span><span>(NOLOCK) <br> </span><span>WHERE</span><span> <br> database_id</span><span>></span><span>4</span><span><br><br></span><span>OPEN</span><span> db_info<br></span><span>FETCH</span><span>NEXT</span><span>FROM</span><span> db_info </span><span>INTO</span><span>@dbname</span><span><br><br></span><span>WHILE</span><span>@@FETCH_STATUS</span><span>=</span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>EXEC</span><span> master.dbo.<span>Usp_BackUp_DataBase </span></span><span>@dbname</span><span>,</span><span>@backup_path</span><span><br> </span><span>FETCH</span><span>NEXT</span><span>FROM</span><span> db_info </span><span>INTO</span><span>@dbname</span><span><br> </span><span>END</span><span><br></span><span>close</span><span> db_info<br></span><span>deallocate</span><span> db_info<br><br></span><span>--</span><span>-------------------------------BackUp DataBase End------------------------------------</span>
检查还原磁盘:
<span>Use</span><span> master<br></span><span>GO</span><span><br></span><span>/*</span><span>=================Check Restore Path Drives Exists==========================<br> =====Ken.Guo ======<br> =====2010.9.10 ======<br> =====EXEC Usp_Check_DriveExists @RestoreDataPath,@ResultCount OUTPUT ======<br> ===========================================================================<br></span><span>*/</span><span><br></span><span>CREATE</span><span>PROC</span><span> Usp_Check_DriveExists(<br> </span><span>@RestoreDataPath</span><span>nvarchar</span><span>(</span><span>200</span><span>)<br> ,</span><span>@ResultCount</span><span>int</span><span> OUTPUT) <br></span><span>AS</span><span><br></span><span>BEGIN</span><span><br></span><span>--</span><span>Check Restore Path and Size >1000M</span><span><br></span><span>if</span><span>CHARINDEX</span><span>(</span><span>'</span><span>:</span><span>'</span><span>,</span><span>@RestoreDataPath</span><span>)</span><span>></span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>DECLARE</span><span>@Drive</span><span>nvarchar</span><span>(</span><span>10</span><span>)<br> ,</span><span>@errorinfo</span><span>nvarchar</span><span>(</span><span>500</span><span>)<br><br> </span><span>DECLARE</span><span>@DriveList</span><span>TABLE</span><span> <br> ( <br> Drive </span><span>nvarchar</span><span>(</span><span>10</span><span>) <br> ,DSize </span><span>bigint</span><span> <br> )<br> </span><span>INSERT</span><span>INTO</span><span>@DriveList</span><span><br> </span><span>EXEC</span><span> master.dbo.xp_fixeddrives<br><br> </span><span>SET</span><span>@Drive</span><span>=Left</span><span>(</span><span>@RestoreDataPath</span><span>,</span><span>CHARINDEX</span><span>(</span><span>'</span><span>:</span><span>'</span><span>,</span><span>@RestoreDataPath</span><span>)</span><span>-</span><span>1</span><span>)<br> </span><span>if</span><span>not</span><span>exists</span><span>(</span><span>SELECT</span><span> <br> </span><span>*</span><span> <br> </span><span>FROM</span><span>@DriveList</span><span> <br> </span><span>WHERE</span><span> <br> Drive</span><span>=</span><span>@Drive</span><span> <br> </span><span>AND</span><span> DSize</span><span>></span><span>1024</span><span><br> <br> )<br> </span><span>begin</span><span><br> </span><span>set</span><span>@errorinfo</span><span>=</span><span>N</span><span>'</span><span>找不到还原磁盘:</span><span>'</span><span>+</span><span>@Drive</span><span>+</span><span>N</span><span>'</span><span> ,或者磁盘剩余空间小于1G</span><span>'</span><span><br> </span><span>RAISERROR</span><span>50001</span><span>@errorinfo</span><span> <br> </span><span>set</span><span>@ResultCount</span><span>=</span><span>0</span><span><br> </span><span>return</span><span><br> </span><span>end</span><span><br> </span><span>end</span><span><br></span><span>else</span><span>if</span><span>(</span><span>LEN</span><span>(</span><span>@RestoreDataPath</span><span>)</span><span>></span><span>1</span><span>) </span><span>AND</span><span>CHARINDEX</span><span>(</span><span>'</span><span>:</span><span>'</span><span>,</span><span>@RestoreDataPath</span><span>)</span><span>=</span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>set</span><span>@errorinfo</span><span>=</span><span>N</span><span>'</span><span>还原路径错误:</span><span>'</span><span>+</span><span>@RestoreDataPath</span><span>+</span><span>N</span><span>'</span><span>,必须包含":" 号</span><span>'</span><span><br> </span><span>Raiserror</span><span>50001</span><span>@errorinfo</span><span> <br> </span><span>set</span><span>@ResultCount</span><span>=</span><span>0</span><span><br> </span><span>return</span><span> <br> </span><span>end</span><span><br> </span><span>set</span><span>@ResultCount</span><span>=</span><span>1</span><span><br></span><span>end</span><span><br></span><span>GO</span>
还原单个数据库:
<span>Use</span><span> master<br></span><span>GO</span><span><br></span><span>/*</span><span>=================Usp_RestoreDataBaseFormPath=======================================<br> =====Restore Single DataBase From a Back File ======<br> =====Ken.Guo ======<br> =====2010.9.10 ======<br> =====Version: 2005 & 2008 SQL Server ======<br> =====Usp_RestoreDataBaseFormPath 'D:\databack\dbcenter.bak','D:\Data',0 ======<br> =====Key Point Info: ======<br> --Restore HeaderOnly from disk='D:\data\xx.bak'<br> --Restore FileListOnly from disk='D:\data\xx.bak'<br> ===================================================================================<br></span><span>*/</span><span><br></span><span>CREATE</span><span>PROC</span><span> Usp_RestoreDataBaseFormPath<br>(</span><span>@DatabBaseBakPath</span><span>nvarchar</span><span>(</span><span>400</span><span>),<br> </span><span>@RestoreDataPath</span><span>nvarchar</span><span>(</span><span>400</span><span>)</span><span>=</span><span>''</span><span>, </span><span>--</span><span>RESTORE DATABASE PATH </span><span><br></span><span>@IsRun</span><span>smallint</span><span>=</span><span>0</span><span>--</span><span> 0 PRINT 1 run </span><span><br></span><span>) <br></span><span>AS</span><span><br></span><span>BEGIN</span><span><br></span><span>set</span><span> nocount </span><span>on</span><span><br><br></span><span>declare</span><span>@dbname</span><span>nvarchar</span><span>(</span><span>200</span><span>),</span><span>@SQL</span><span>nvarchar</span><span>(</span><span>4000</span><span>),</span><span>@DirSQL</span><span>nvarchar</span><span>(</span><span>1000</span><span>),</span><span>@errorinfo</span><span>nvarchar</span><span>(</span><span>300</span><span>)<br></span><span>--</span><span>add path \</span><span><br></span><span>if</span><span> (</span><span>@RestoreDataPath</span><span>is</span><span>not</span><span>null</span><span>) </span><span>and</span><span>len</span><span>(</span><span>@RestoreDataPath</span><span>)</span><span>></span><span>1</span><span> <br> </span><span>and</span><span> (</span><span>right</span><span>(</span><span>@RestoreDataPath</span><span>,</span><span>1</span><span>)</span><span></span><span>'</span><span>\</span><span>'</span><span>)<br> </span><span>set</span><span>@RestoreDataPath</span><span>=</span><span>@RestoreDataPath</span><span>+</span><span>'</span><span>\</span><span>'</span><span><br><br></span><span>declare</span><span>@checkdrive</span><span>int</span><span><br></span><span>set</span><span>@checkdrive</span><span>=</span><span>1</span><span><br> </span><span>exec</span><span> master.dbo.Usp_Check_DriveExists </span><span>@RestoreDataPath</span><span>,</span><span>@checkdrive</span><span> output<br><br> </span><span>if</span><span>(</span><span>@checkdrive</span><span></span><span>1</span><span>)<br> </span><span>Goto</span><span> ExitFLag <br><br></span><span>DECLARE</span><span>@BakFileList</span><span>TABLE</span><span> <br> ( LogicalName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,PhysicalName </span><span>nvarchar</span><span>(</span><span>260</span><span>)<br> )<br><br></span><span>DECLARE</span><span>@BakHeaderInfo</span><span>TABLE</span><span><br> (<br> DatabaseName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> )<br><br></span><span>if</span><span>Charindex</span><span>(</span><span>'</span><span>Microsoft SQL Server 2008</span><span>'</span><span>,</span><span>@@VERSION</span><span>)</span><span>></span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>--</span><span>SQL Server 2008 </span><span><br></span><span>DECLARE</span><span>@BakFileList2008</span><span>TABLE</span><span> <br> ( LogicalName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,PhysicalName </span><span>nvarchar</span><span>(</span><span>260</span><span>)<br> ,Type </span><span>char</span><span>(</span><span>1</span><span>)<br> ,FileGroupName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,SIZE numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,MaxSize numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,FileID </span><span>bigint</span><span><br> ,CreateLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,DropLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,UniqueID </span><span>uniqueidentifier</span><span><br> ,ReadOnlyLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,ReadWriteLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,BackupSizeInBytes </span><span>bigint</span><span><br> ,SourceBlockSize </span><span>int</span><span><br> ,FileGroupID </span><span>int</span><span><br> ,LogGroupGUID </span><span>uniqueidentifier</span><span>NULL</span><span><br> ,DifferentialBaseLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,DifferentialBaseGUID </span><span>uniqueidentifier</span><span><br> ,IsReadOnly </span><span>bit</span><span><br> ,IsPresent </span><span>bit</span><span><br> ,TDEThumbprint </span><span>varbinary</span><span>(</span><span>32</span><span>)<br> ) <br> <br> </span><span>INSERT</span><span>INTO</span><span>@BakFileList2008</span><span> <br> </span><span>EXEC</span><span> sp_executesql N</span><span>'</span><span>Restore FileListOnly From Disk=@DatabBaseBakPath</span><span>'</span><span>,N</span><span>'</span><span>@DatabBaseBakPath nvarchar(260)</span><span>'</span><span>,</span><span>@DatabBaseBakPath</span><span> <br><br> </span><span>DECLARE</span><span>@BakHeaderInfo2008</span><span>TABLE</span><span><br> (<br> BackupName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,BackupDescription </span><span>nvarchar</span><span>(</span><span>255</span><span>)<br> ,BackupType </span><span>smallint</span><span><br> ,ExpirationDate </span><span>datetime</span><span><br> ,Compressed </span><span>tinyint</span><span><br> ,POSITION </span><span>smallint</span><span><br> ,DeviceType </span><span>tinyint</span><span><br> ,UserName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,ServerName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,DatabaseName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,DatabaseVersion </span><span>int</span><span><br> ,DatabaseCreationDate </span><span>datetime</span><span><br> ,BackupSize numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,FirstLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,LastLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,CheckpointLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,DatabaseBackupLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,BackupStartDate </span><span>datetime</span><span><br> ,BackupFinishDate </span><span>datetime</span><span><br> ,SortOrder </span><span>smallint</span><span><br> ,CodePage </span><span>smallint</span><span><br> ,UnicodeLocaleId </span><span>int</span><span><br> ,UnicodeComparisonStyle </span><span>int</span><span><br> ,CompatibilityLevel </span><span>tinyint</span><span><br> ,SoftwareVendorId </span><span>int</span><span><br> ,SoftwareVersionMajor </span><span>int</span><span><br> ,SoftwareVersionMinor </span><span>int</span><span><br> ,SoftwareVersionBuild </span><span>int</span><span><br> ,MachineName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,Flags </span><span>int</span><span><br> ,BindingID </span><span>uniqueidentifier</span><span><br> ,RecoveryForkID </span><span>uniqueidentifier</span><span><br> ,COLLATION </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,FamilyGUID </span><span>uniqueidentifier</span><span><br> ,HasBulkLoggedData </span><span>bit</span><span><br> ,IsSnapshot </span><span>bit</span><span><br> ,IsReadOnly </span><span>bit</span><span><br> ,IsSingleUser </span><span>bit</span><span><br> ,HasBackupChecksums </span><span>bit</span><span><br> ,IsDamaged </span><span>bit</span><span><br> ,BeginsLogChain </span><span>bit</span><span><br> ,HasIncompleteMetaData </span><span>bit</span><span><br> ,IsForceOffline </span><span>bit</span><span><br> ,IsCopyOnly </span><span>bit</span><span><br> ,FirstRecoveryForkID </span><span>uniqueidentifier</span><span><br> ,ForkPointLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,RecoveryModel </span><span>nvarchar</span><span>(</span><span>60</span><span>)<br> ,DifferentialBaseLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,DifferentialBaseGUID </span><span>uniqueidentifier</span><span><br> ,BackupTypeDescription </span><span>nvarchar</span><span>(</span><span>60</span><span>)<br> ,BackupSetGUID </span><span>uniqueidentifier</span><span>NULL</span><span><br> ,CompressedBackupSize numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ) <br><br> </span><span>INSERT</span><span>INTO</span><span>@BakHeaderInfo2008</span><span> <br> </span><span>EXEC</span><span> sp_executesql N</span><span>'</span><span>Restore HeaderOnly From Disk=@DatabBaseBakPath</span><span>'</span><span>,N</span><span>'</span><span>@DatabBaseBakPath nvarchar(260)</span><span>'</span><span>,</span><span>@DatabBaseBakPath</span><span> <br> <br> </span><span>insert</span><span>into</span><span>@BakHeaderInfo</span><span>(DatabaseName)<br> </span><span>select</span><span> DatabaseName </span><span>from</span><span>@BakHeaderInfo2008</span><span><br><br> </span><span>insert</span><span>into</span><span>@BakFileList</span><span>(LogicalName ,PhysicalName)<br> </span><span>select</span><span> LogicalName ,PhysicalName </span><span>from</span><span>@BakFileList2008</span><span><br> </span><span>end</span><span><br></span><span>else</span><span><br> </span><span>begin</span><span><br> </span><span>--</span><span>SQL Server 2005 </span><span><br></span><span>DECLARE</span><span>@BakFileList2005</span><span>TABLE</span><span> <br> (<br> LogicalName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,PhysicalName </span><span>nvarchar</span><span>(</span><span>260</span><span>)<br> ,Type </span><span>char</span><span>(</span><span>1</span><span>)<br> ,FileGroupName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,SIZE numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,MaxSize numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,FileID </span><span>bigint</span><span><br> ,CreateLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,DropLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,UniqueID </span><span>uniqueidentifier</span><span><br> ,ReadOnlyLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,ReadWriteLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,BackupSizeInBytes </span><span>bigint</span><span><br> ,SourceBlockSize </span><span>int</span><span><br> ,FileGroupID </span><span>int</span><span><br> ,LogGroupGUID </span><span>uniqueidentifier</span><span>NULL</span><span><br> ,DifferentialBaseLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,DifferentialBaseGUID </span><span>uniqueidentifier</span><span><br> ,IsReadOnly </span><span>bit</span><span><br> ,IsPresent </span><span>bit</span><span><br> ) <br><br> </span><span>INSERT</span><span>INTO</span><span>@BakFileList2005</span><span> <br> </span><span>EXEC</span><span> sp_executesql N</span><span>'</span><span>Restore FileListOnly From Disk=@DatabBaseBakPath</span><span>'</span><span>,N</span><span>'</span><span>@DatabBaseBakPath nvarchar(260)</span><span>'</span><span>,</span><span>@DatabBaseBakPath</span><span> <br> <br> </span><span>DECLARE</span><span>@BakHeaderInfo2005</span><span>TABLE</span><span> <br> (<br> BackupName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,BackupDescription </span><span>nvarchar</span><span>(</span><span>255</span><span>)<br> ,BackupType </span><span>smallint</span><span><br> ,ExpirationDate </span><span>datetime</span><span><br> ,Compressed </span><span>tinyint</span><span><br> ,POSITION </span><span>smallint</span><span><br> ,DeviceType </span><span>tinyint</span><span><br> ,UserName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,ServerName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,DatabaseName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,DatabaseVersion </span><span>int</span><span><br> ,DatabaseCreationDate </span><span>datetime</span><span><br> ,BackupSize numeric(</span><span>20</span><span>,</span><span>0</span><span>)<br> ,FirstLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,LastLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,CheckpointLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,DatabaseBackupLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>)<br> ,BackupStartDate </span><span>datetime</span><span><br> ,BackupFinishDate </span><span>datetime</span><span><br> ,SortOrder </span><span>smallint</span><span><br> ,CodePage </span><span>smallint</span><span><br> ,UnicodeLocaleId </span><span>int</span><span><br> ,UnicodeComparisonStyle </span><span>int</span><span><br> ,CompatibilityLevel </span><span>tinyint</span><span><br> ,SoftwareVendorId </span><span>int</span><span><br> ,SoftwareVersionMajor </span><span>int</span><span><br> ,SoftwareVersionMinor </span><span>int</span><span><br> ,SoftwareVersionBuild </span><span>int</span><span><br> ,MachineName </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,Flags </span><span>int</span><span><br> ,BindingID </span><span>uniqueidentifier</span><span><br> ,RecoveryForkID </span><span>uniqueidentifier</span><span><br> ,COLLATION </span><span>nvarchar</span><span>(</span><span>128</span><span>)<br> ,FamilyGUID </span><span>uniqueidentifier</span><span><br> ,HasBulkLoggedData </span><span>bit</span><span><br> ,IsSnapshot </span><span>bit</span><span><br> ,IsReadOnly </span><span>bit</span><span><br> ,IsSingleUser </span><span>bit</span><span><br> ,HasBackupChecksums </span><span>bit</span><span><br> ,IsDamaged </span><span>bit</span><span><br> ,BeginsLogChain </span><span>bit</span><span><br> ,HasIncompleteMetaData </span><span>bit</span><span><br> ,IsForceOffline </span><span>bit</span><span><br> ,IsCopyOnly </span><span>bit</span><span><br> ,FirstRecoveryForkID </span><span>uniqueidentifier</span><span><br> ,ForkPointLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,RecoveryModel </span><span>nvarchar</span><span>(</span><span>60</span><span>)<br> ,DifferentialBaseLSN numeric(</span><span>25</span><span>,</span><span>0</span><span>) </span><span>NULL</span><span><br> ,DifferentialBaseGUID </span><span>uniqueidentifier</span><span><br> ,BackupTypeDescription </span><span>nvarchar</span><span>(</span><span>60</span><span>)<br> ,BackupSetGUID </span><span>uniqueidentifier</span><span>NULL</span><span><br> ) <br><br> </span><span>INSERT</span><span>INTO</span><span>@BakHeaderInfo2005</span><span> <br> </span><span>EXEC</span><span> sp_executesql N</span><span>'</span><span>Restore HeaderOnly From Disk=@DatabBaseBakPath</span><span>'</span><span>,N</span><span>'</span><span>@DatabBaseBakPath nvarchar(260)</span><span>'</span><span>,</span><span>@DatabBaseBakPath</span><span> <br><br> </span><span>insert</span><span>into</span><span>@BakHeaderInfo</span><span>(DatabaseName)<br> </span><span>select</span><span> DatabaseName </span><span>from</span><span>@BakHeaderInfo2005</span><span><br><br> </span><span>insert</span><span>into</span><span>@BakFileList</span><span>(LogicalName ,PhysicalName)<br> </span><span>select</span><span> LogicalName ,PhysicalName </span><span>from</span><span>@BakFileList2005</span><span><br><br> </span><span>end</span><span><br><br></span><span>--</span><span>Check back file info</span><span><br></span><span>if</span><span>not</span><span>exists</span><span> (</span><span>select</span><span>1</span><span>from</span><span>@BakFileList</span><span>) </span><span>OR</span><span>not</span><span>exists</span><span> (</span><span>select</span><span>1</span><span>from</span><span>@BakHeaderInfo</span><span>)<br> </span><span>begin</span><span><br> </span><span>set</span><span>@errorinfo</span><span>=</span><span>N</span><span>'</span><span>取不到备份文件:</span><span>'</span><span>+</span><span>@DatabBaseBakPath</span><span>+</span><span>N</span><span>'</span><span> 的信息,请检查备份文件是否正确或者版本是否兼容</span><span>'</span><span><br> </span><span>Raiserror</span><span>50001</span><span>@errorinfo</span><span> <br> </span><span>Goto</span><span> ExitFLag<br> </span><span>end</span><span><br><br></span><span>--</span><span>Get DataBase Name</span><span><br></span><span>SELECT</span><span>TOP</span><span>1</span><span>@dbname</span><span>=</span><span>databasename </span><span>FROM</span><span>@BakHeaderInfo</span><span><br><br></span><span>if</span><span>exists</span><span> (</span><span>select</span><span>1</span><span>from</span><span> master.sys.databases </span><span>with</span><span>(nolock) </span><span>where</span><span> name</span><span>=</span><span>@dbname</span><span>)<br> </span><span>begin</span><span><br> <br> </span><span>set</span><span>@errorinfo</span><span>=</span><span>N</span><span>'</span><span>数据库:</span><span>'</span><span>+</span><span>@dbname</span><span>+</span><span>N</span><span>'</span><span>已经存在,不能还原</span><span>'</span><span> <br> </span><span>Raiserror</span><span>50001</span><span>@errorinfo</span><span> <br> </span><span>Goto</span><span> ExitFLag<br> </span><span>end</span><span><br><br></span><span>DECLARE</span><span>@LogicalName</span><span>nvarchar</span><span>(</span><span>200</span><span>),</span><span>@PhysicalName</span><span>nvarchar</span><span>(</span><span>400</span><span>)<br> ,</span><span>@pos</span><span>int</span><span> ,</span><span>@endpos</span><span>int</span><span>,</span><span>@LastPhysicalName</span><span>nvarchar</span><span>(</span><span>400</span><span>)<br><br></span><span>DECLARE</span><span> db_file </span><span>CURSOR</span><span> <br> LOCAL <br> READ_ONLY <br> FORWARD_ONLY <br> STATIC <br></span><span>FOR</span><span><br> </span><span>SELECT</span><span> <br> LogicalName<br> ,PhysicalName <br> </span><span>FROM</span><span>@BakFileList</span><span><br><br></span><span>OPEN</span><span> db_file<br><br></span><span>set</span><span>@DirSQL</span><span>=</span><span>''</span><span><br></span><span>set</span><span>@SQL</span><span>=+</span><span>N</span><span>'</span><span>RESTORE DATABASE </span><span>'</span><span>+</span><span>QUOTENAME</span><span>(</span><span>@dbname</span><span>)</span><span>+</span><span>'</span><span> from disk=N</span><span>'''</span><span>+</span><span>@DatabBaseBakPath</span><span>+</span><span>''''</span><span><br></span><span>set</span><span>@SQL</span><span>=</span><span>@SQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>Char</span><span>(</span><span>10</span><span>)</span><span>+</span><span>N</span><span>'</span><span> WITH FILE=1 </span><span>'</span><span><br><br></span><span>FETCH</span><span>NEXT</span><span>FROM</span><span> db_file </span><span>INTO</span><span>@LogicalName</span><span>,</span><span>@PhysicalName</span><span><br><br></span><span>WHILE</span><span>@@FETCH_STATUS</span><span>=</span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>--</span><span>-Get DB PhysicalName</span><span><br></span><span>set</span><span>@endpos</span><span>=</span><span>0</span><span><br> </span><span>while</span><span>CHARINDEX</span><span>(</span><span>'</span><span>\</span><span>'</span><span>,</span><span>@PhysicalName</span><span>)</span><span>></span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>set</span><span>@pos</span><span>=</span><span>CHARINDEX</span><span>(</span><span>'</span><span>\</span><span>'</span><span>,</span><span>@PhysicalName</span><span>,</span><span>@endpos</span><span>)<br> </span><span>if</span><span>(</span><span>@pos</span><span>=</span><span>0</span><span>)<br> </span><span>break</span><span>;<br> </span><span>set</span><span>@endpos</span><span>=</span><span>@pos</span><span>+</span><span>1</span><span>;<br> </span><span>end</span><span><br> <br> </span><span>--</span><span>create new db path</span><span><br></span><span>if</span><span>(</span><span>len</span><span>(</span><span>@RestoreDataPath</span><span>)</span><span>></span><span>1</span><span>)<br> </span><span>begin</span><span><br> </span><span>set</span><span>@PhysicalName</span><span>=</span><span>@RestoreDataPath</span><span>+</span><span>@dbname</span><span>+</span><span>'</span><span>\</span><span>'</span><span>+</span><span>SUBSTRING</span><span>(</span><span>@PhysicalName</span><span>,</span><span>@endpos</span><span>,</span><span>LEN</span><span>(</span><span>@PhysicalName</span><span>)</span><span>-</span><span>@endpos</span><span>+</span><span>1</span><span>)<br> </span><span>set</span><span>@DirSQL</span><span>=</span><span>N</span><span>'</span><span>EXEC master.sys.xp_create_subdir N</span><span>'''</span><span>+</span><span>@RestoreDataPath</span><span>+</span><span>@dbname</span><span>+</span><span>''''</span><span><br> </span><span>END</span><span><br> </span><span>else</span><span><br> </span><span>begin</span><span><br> </span><span>if</span><span>len</span><span>(</span><span>@DirSQL</span><span>)</span><span><span>1</span><span>OR</span><span> (</span><span>SUBSTRING</span><span>(</span><span>@PhysicalName</span><span>,</span><span>1</span><span>,</span><span>@endpos</span><span>-</span><span>1</span><span>)</span><span></span><span>@LastPhysicalName</span><span>)<br> </span><span>if</span><span>(</span><span>len</span><span>(</span><span>@DirSQL</span><span>)</span><span><span>1</span><span>)<br> </span><span>set</span><span>@DirSQL</span><span>=</span><span>N</span><span>'</span><span>EXEC master.sys.xp_create_subdir N</span><span>'''</span><span>+</span><span>SUBSTRING</span><span>(</span><span>@PhysicalName</span><span>,</span><span>1</span><span>,</span><span>@endpos</span><span>-</span><span>1</span><span>)</span><span>+</span><span>''''</span><span><br> </span><span>else</span><span><br> </span><span>set</span><span>@DirSQL</span><span>=</span><span>@DirSQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>N</span><span>'</span><span>EXEC master.sys.xp_create_subdir N</span><span>'''</span><span>+</span><span>SUBSTRING</span><span>(</span><span>@PhysicalName</span><span>,</span><span>1</span><span>,</span><span>@endpos</span><span>-</span><span>1</span><span>)</span><span>+</span><span>''''</span><span><br> <br> </span><span>--</span><span>-Check Drives</span><span><br></span><span>set</span><span>@checkdrive</span><span>=</span><span>1</span><span><br> </span><span>exec</span><span> master.dbo.Usp_Check_DriveExists </span><span>@PhysicalName</span><span>,</span><span>@checkdrive</span><span> output<br><br> </span><span>if</span><span>(</span><span>@checkdrive</span><span></span><span>1</span><span>)<br> </span><span>Goto</span><span> ExitFLag <br><br> </span><span>set</span><span>@LastPhysicalName</span><span>=</span><span>SUBSTRING</span><span>(</span><span>@PhysicalName</span><span>,</span><span>1</span><span>,</span><span>@endpos</span><span>-</span><span>1</span><span>);<br> </span><span>END</span><span><br> <br> </span><span>set</span><span>@SQL</span><span>=</span><span>@SQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>Char</span><span>(</span><span>10</span><span>)</span><span>+</span><span>N</span><span>'</span><span> ,Move N</span><span>'''</span><span>+</span><span>@LogicalName</span><span>+</span><span>''''</span><span>+</span><span>'</span><span> TO N</span><span>'''</span><span>+</span><span>@PhysicalName</span><span>+</span><span>''''</span><span><br> <br> </span><span>FETCH</span><span>NEXT</span><span>FROM</span><span> db_file </span><span>INTO</span><span>@LogicalName</span><span>,</span><span>@PhysicalName</span><span><br> </span><span>end</span><span><br> </span><span>set</span><span>@SQL</span><span>=</span><span>@SQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>Char</span><span>(</span><span>10</span><span>)</span><span>+</span><span>N</span><span>'</span><span> ,NOUNLOAD,Recovery,STATS = 10</span><span>'</span><span><br><br></span><span>if</span><span>(</span><span>@IsRun</span><span>=</span><span>0</span><span>)<br> </span><span>print</span><span>( </span><span>@DirSQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>char</span><span>(</span><span>10</span><span>)</span><span>+</span><span>'</span><span>GO</span><span>'</span><span>+</span><span>char</span><span>(</span><span>13</span><span>)</span><span>+</span><span>Char</span><span>(</span><span>10</span><span>)</span><span>+</span><span>@SQL</span><span>+</span><span>char</span><span>(</span><span>13</span><span>))<br></span><span>else</span><span><br> </span><span>begin</span><span><br> </span><span>print</span><span>(</span><span>'</span><span>-----------Begin Restore Database:</span><span>'</span><span>+</span><span>@dbname</span><span>+</span><span>'</span><span>------------------</span><span>'</span><span>)<br> </span><span>exec</span><span>(</span><span>@DirSQL</span><span>)<br> </span><span>exec</span><span>(</span><span>@SQL</span><span>)<br> </span><span>print</span><span>(</span><span>'</span><span>-----------End Restore Database:</span><span>'</span><span>+</span><span>@dbname</span><span>+</span><span>'</span><span>---------------------</span><span>'</span><span>+</span><span>char</span><span>(</span><span>13</span><span>))<br> </span><span>end</span><span><br><br> </span><span>close</span><span> db_file<br> </span><span>deallocate</span><span> db_file<br><br>ExitFLag:<br></span><span>set</span><span> nocount </span><span>off</span><span><br></span><span>end</span></span></span>
批量还原数据库:
<span>Use</span><span> master<br></span><span>GO</span><span><br></span><span>/*</span><span>=================Usp_RestoreMuiteDataBaseFromPath========================<br> =====Restore Mutite DataBase File From a Path ======<br> =====Ken.Guo ======<br> =====2010.9.10 ======<br> =====Version: 2005 & 2008 SQL Server ======<br> =====EXEC Usp_RestoreMuiteDataBaseFromPath 'D:\databack','',0 ======<br> =========================================================================<br></span><span>*/</span><span><br></span><span>CREATE</span><span>PROC</span><span> Usp_RestoreMuiteDataBaseFromPath<br>( </span><span>@DatabBaseBakPath</span><span>nvarchar</span><span>(</span><span>400</span><span>)<br> ,</span><span>@RestoreDataPath</span><span>nvarchar</span><span>(</span><span>400</span><span>)</span><span>=</span><span>''</span><span>--</span><span>RESTORE DATABASE PATH </span><span><br></span><span> ,</span><span>@IsRun</span><span>smallint</span><span>=</span><span>0</span><span>--</span><span> 0 PRINT 1 run </span><span><br></span><span>) <br></span><span>AS</span><span><br></span><span>BEGIN</span><span><br></span><span>set</span><span> nocount </span><span>on</span><span><br></span><span>DECLARE</span><span>@BackUpFileName</span><span>nvarchar</span><span>(</span><span>200</span><span>) <br> ,</span><span>@DbName</span><span>nvarchar</span><span>(</span><span>200</span><span>) <br> ,</span><span>@errorinfo</span><span>nvarchar</span><span>(</span><span>400</span><span>)<br><br></span><span>IF</span><span>not</span><span>exists</span><span>(</span><span>SELECT</span><span>1</span><span> <br> </span><span>FROM</span><span> master.sys.procedures </span><span>WITH</span><span>(NOLOCK) <br> </span><span>WHERE</span><span> <br> name</span><span>=</span><span>N</span><span>'</span><span>Usp_RestoreDataBaseFormPath</span><span>'</span><span> <br> <br> )<br> </span><span>begin</span><span><br> </span><span>Raiserror</span><span>50001</span><span> N</span><span>'</span><span>找不到存储过程SP_RestoreDataBaseFormPath </span><span>'</span><span> <br> </span><span>Goto</span><span> ExitFLag<br> </span><span>end</span><span><br><br></span><span>--</span><span>add path \</span><span><br></span><span>if</span><span> (</span><span>@DatabBaseBakPath</span><span>is</span><span>not</span><span>null</span><span>) </span><span>and</span><span>len</span><span>(</span><span>@DatabBaseBakPath</span><span>)</span><span>></span><span>1</span><span> <br> </span><span>and</span><span> (</span><span>right</span><span>(</span><span>@DatabBaseBakPath</span><span>,</span><span>1</span><span>)</span><span></span><span>'</span><span>\</span><span>'</span><span>)<br> </span><span>set</span><span>@DatabBaseBakPath</span><span>=</span><span>@DatabBaseBakPath</span><span>+</span><span>'</span><span>\</span><span>'</span><span><br><br></span><span>--</span><span>Check Restore Path and Size >1000M</span><span><br></span><span>DECLARE</span><span>@checkdrive</span><span>int</span><span><br></span><span>SET</span><span>@checkdrive</span><span>=</span><span>1</span><span><br> </span><span>EXEC</span><span> master.dbo.Usp_Check_DriveExists </span><span>@RestoreDataPath</span><span>,</span><span>@checkdrive</span><span> OUTPUT<br><br> </span><span>IF</span><span>(</span><span>@checkdrive</span><span></span><span>1</span><span>)<br> </span><span>Goto</span><span> ExitFLag <br> <br></span><span>DECLARE</span><span>@Dir</span><span>TABLE</span><span> <br>( <br> BackDBFileName </span><span>nvarchar</span><span>(</span><span>100</span><span>) <br> ,DEPTH </span><span>int</span><span> <br> ,</span><span>[</span><span>File</span><span>]</span><span>int</span><span> <br>)<br><br></span><span>INSERT</span><span>INTO</span><span>@Dir</span><span>EXEC</span><span> xp_dirtree </span><span>@DatabBaseBakPath</span><span><br> ,</span><span>1</span><span><br> ,</span><span>1</span><span><br><br></span><span>DELETE</span><span>FROM</span><span>@Dir</span><span> <br></span><span>WHERE</span><span>charindex</span><span>(</span><span>'</span><span>.bak</span><span>'</span><span>,BackDBFileName)</span><span>=</span><span>0</span><span><br><br></span><span>if</span><span>not</span><span>exists</span><span> (</span><span>select</span><span>top</span><span>1</span><span>1</span><span>from</span><span>@Dir</span><span>)<br> </span><span>begin</span><span><br> </span><span>Raiserror</span><span>50001</span><span> N</span><span>'</span><span>在提供的路径下没有找到合符要求的备份文件</span><span>'</span><span> <br> </span><span>Goto</span><span> ExitFLag<br> </span><span>end</span><span><br><br></span><span>declare</span><span> db_file </span><span>Cursor</span><span> Local Static Read_Only Forward_Only<br></span><span>for</span><span><br></span><span>select</span><span> BackDBFileName </span><span>from</span><span>@Dir</span><span><br><br></span><span>Open</span><span> db_file<br></span><span>Fetch</span><span>Next</span><span>from</span><span> db_file </span><span>into</span><span>@BackUpFileName</span><span><br></span><span>while</span><span>@@FETCH_STATUS</span><span>=</span><span>0</span><span><br> </span><span>begin</span><span><br> </span><span>--</span><span>Restore DataBase</span><span><br></span><span>set</span><span>@BackUpFileName</span><span>=</span><span>@DatabBaseBakPath</span><span>+</span><span>@BackUpFileName</span><span><br> </span><span>exec</span><span> master.dbo.Usp_RestoreDataBaseFormPath </span><span>@BackUpFileName</span><span>,</span><span>@RestoreDataPath</span><span>,</span><span>@IsRun</span><span><br> </span><span>Fetch</span><span>Next</span><span>from</span><span> db_file </span><span>into</span><span>@BackUpFileName</span><span><br> </span><span>end</span><span><br></span><span>Close</span><span> db_file<br></span><span>deallocate</span><span> db_file<br><br>ExitFLag:<br></span><span>set</span><span> nocount </span><span>off</span><span><br></span><span>end</span>