我有一周前的Database1备份。在调度程序中每周进行备份,我得到一个.bak文件。现在我想要处理一些数据,所以我需要将其恢复到另一个数据库- Database2。

我看到过这个问题:在同一台pc上使用不同的名称恢复SQL Server数据库,建议步骤是重命名原始db,但我不在这个选项中,因为我在生产服务器中,我不能真正做到这一点。

是否有其他方法将其恢复到Database2,或者至少如何浏览该.bak文件的数据?

谢谢。

Ps:上面链接的第二个答案看起来很有希望,但它总是以错误告终:

恢复文件列表异常终止


当前回答

用“复制数据库”选项从数据库中复制一个不同的名称 备份新复制的数据库 恢复它!

其他回答

如果不存在数据库,我使用以下代码:

ALTER PROCEDURE [dbo].[RestoreBackupToNewDB]    
         @pathToBackup  varchar(500),--where to take backup from
         @pathToRestoreFolder  varchar(500), -- where to put the restored db files 
         @newDBName varchar(100)
    AS
    BEGIN

            SET NOCOUNT ON
            DECLARE @fileListTable 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),
            [UniqueID]              UNIQUEIDENTIFIER,
            [ReadOnlyLSN]           NUMERIC(25,0),
            [ReadWriteLSN]          NUMERIC(25,0),
            [BackupSizeInBytes]     BIGINT,
            [SourceBlockSize]       INT,
            [FileGroupID]           INT,
            [LogGroupGUID]          UNIQUEIDENTIFIER,
            [DifferentialBaseLSN]   NUMERIC(25,0),
            [DifferentialBaseGUID]  UNIQUEIDENTIFIER,
            [IsReadOnly]            BIT,
            [IsPresent]             BIT,
            [TDEThumbprint]         VARBINARY(32) -- remove this column if using SQL 2005
            )
            INSERT INTO @fileListTable EXEC('RESTORE FILELISTONLY FROM DISK ='''+ @pathToBackup+'''')
            DECLARE @restoreDatabaseFilePath NVARCHAR(500)
            DECLARE @restoreLogFilePath NVARCHAR(500)
            DECLARE @databaseLogicName NVARCHAR(500)
            DECLARE @logLogicName NVARCHAR(500)
            DECLARE @pathSalt uniqueidentifier = NEWID()

            SET @databaseLogicName = (SELECT LogicalName FROM @fileListTable WHERE [Type]='D') 
            SET @logLogicName = (SELECT LogicalName FROM @fileListTable WHERE [Type]='L')           
            SET @restoreDatabaseFilePath= @pathToRestoreFolder + @databaseLogicName + convert(nvarchar(50), @pathSalt) + '.mdf'
            SET @restoreLogFilePath= @pathToRestoreFolder + @logLogicName + convert(nvarchar(50), @pathSalt) + '.ldf'

            RESTORE DATABASE @newDBName FROM DISK=@pathToBackup     
            WITH 
               MOVE @databaseLogicName TO @restoreDatabaseFilePath,
               MOVE @logLogicName TO @restoreLogFilePath

            SET NOCOUNT OFF
    END

SQL Server 2008 R2:

对于您希望“从不同数据库的备份恢复:”的现有数据库,请执行以下步骤:

From the toolbar, click the Activity Monitor button. Click processes. Filter by the database you want to restore. Kill all running processes by right clicking on each process and selecting "kill process". Right click on the database you wish to restore, and select Tasks-->Restore-->From Database. Select the "From Device:" radio button. Select ... and choose the backup file of the other database you wish to restore from. Select the backup set you wish to restore from by selecting the check box to the left of the backup set. Select "Options". Select Overwrite the existing database (WITH REPLACE) Important: Change the "Restore As" Rows Data file name to the file name of the existing database you wish to overwrite or just give it a new name. Do the same with the log file file name. Verify from the Activity Monitor Screen that no new processes were spawned. If they were, kill them. Click OK.

您可以创建一个新的数据库,然后使用“恢复向导”启用覆盖选项或:

查看备份文件内容:

RESTORE FILELISTONLY FROM DISK='c:\your.bak'

注意结果中.mdf和.ldf的逻辑名称,然后:

RESTORE DATABASE MyTempCopy FROM DISK='c:\your.bak'
WITH 
   MOVE 'LogicalNameForTheMDF' TO 'c:\MyTempCopy.mdf',
   MOVE 'LogicalNameForTheLDF' TO 'c:\MyTempCopy_log.ldf'

这将使用your.bak的内容创建数据库MyTempCopy。

(不要创建MyTempCopy,它是在恢复过程中创建的)


示例(恢复名为'creditline'的db的备份到'MyTempCopy'):

RESTORE FILELISTONLY FROM DISK='e:\mssql\backup\creditline.bak'

>LogicalName
>--------------
>CreditLine
>CreditLine_log

RESTORE DATABASE MyTempCopy FROM DISK='e:\mssql\backup\creditline.bak'
WITH 
   MOVE 'CreditLine' TO 'e:\mssql\MyTempCopy.mdf',
   MOVE 'CreditLine_log' TO 'e:\mssql\MyTempCopy_log.ldf'

>RESTORE DATABASE successfully processed 186 pages in 0.010 seconds (144.970 MB/sec).

当我使用旧数据库恢复新数据库时,遇到了与本主题相同的错误。(使用.bak也会出现同样的错误) 我用新数据库的名称更改了旧数据库的名称(与此图相同)。它工作。

实际上,在本地SQL Server术语中不需要恢复数据库,因为您“想要摆弄一些数据”和“浏览。bak文件中的数据”

您可以使用ApexSQL Restore -一个SQL Server工具,它将本地和本地压缩的SQL数据库备份和事务日志备份附加为活动数据库,可通过SQL Server Management Studio, Visual Studio或任何其他第三方工具访问。它允许附加单个或多个完整、差异和事务日志备份

此外,我认为您可以在工具处于全功能试用模式(14天)时完成这项工作。

免责声明:我是ApexSQL的产品支持工程师