备份与还原是数据库避不开的主题,而作为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>

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
