我有一周前的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 2012,使用SQL Server Management Studio,我发现从微软页面恢复到不同的数据库文件和名称的这些步骤很有用:(参考:http://technet.microsoft.com/en-us/library/ms175510.aspx)

注意,为了不覆盖现有数据库,设置步骤4和7很重要。


To restore a database to a new location, and optionally rename the database Connect to the appropriate instance of the SQL Server Database Engine, and then in Object Explorer, click the server name to expand the server tree. Right-click Databases, and then click Restore Database. The Restore Database dialog box opens. On the General page, use the Source section to specify the source and location of the backup sets to restore. Select one of the following options: Database Select the database to restore from the drop-down list. The list contains only databases that have been backed up according to the msdb backup history. Note If the backup is taken from a different server, the destination server will not have the backup history information for the specified database. In this case, select Device to manually specify the file or device to restore. Device Click the browse (...) button to open the Select backup devices dialog box. In the Backup media type box, select one of the listed device types. To select one or more devices for the Backup media box, click Add. After you add the devices you want to the Backup media list box, click OK to return to the General page. In the Source: Device: Database list box, select the name of the database which should be restored. Note This list is only available when Device is selected. Only databases that have backups on the selected device will be available. In the Destination section, the Database box is automatically populated with the name of the database to be restored. To change the name of the database, enter the new name in the Database box. In the Restore to box, leave the default as To the last backup taken or click on Timeline to access the Backup Timeline dialog box to manually select a point in time to stop the recovery action. In the Backup sets to restore grid, select the backups to restore. This grid displays the backups available for the specified location. By default, a recovery plan is suggested. To override the suggested recovery plan, you can change the selections in the grid. Backups that depend on the restoration of an earlier backup are automatically deselected when the earlier backup is deselected. To specify the new location of the database files, select the Files page, and then click Relocate all files to folder. Provide a new location for the Data file folder and Log file folder. Alternatively you can keep the same folders and just rename the database and log file names.

实际上,这比恢复到同一台服务器要简单一些。基本上,您只需浏览“恢复数据库”选项。这里有一个教程给你:

http://www.techrepublic.com/blog/window-on-windows/how-do-i-restore-a-sql-server-database-to-a-new-server/454

特别是因为这是一个非生产恢复,您可以放心地尝试它,而不必担心过多的细节。只需将SQL文件放在新服务器上您想要的位置,并给它起任何您想要的名称,就可以开始了。

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

下面介绍如何将备份恢复为具有唯一db名称的附加db。

对于SQL 2005,这工作得非常快。我相信更新的版本也会有同样的效果。

首先,您不必让原始db脱机。但为了安全起见,我还是想。在我的例子中,我将挂载我的“账单”数据库的克隆,它将被命名为“billingclone”。

1)做好计费数据库的备份

2)为了安全起见,我将原件下线如下:

3)打开一个新的查询窗口

* *重要!保持这个查询窗口打开,直到全部完成!你需要从这个窗口恢复数据库!

现在输入以下代码:

-- 1) free up all USER databases
USE master;
GO
-- 2) kick all other users out:
ALTER DATABASE billing SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
-- 3) prevent sessions from re-establishing connection:
ALTER DATABASE billing SET OFFLINE;

3)接下来,在管理工作室,在对象资源管理器中单击数据库,选择“恢复数据库”

4)在“To Database”字段中输入新的名称。即billingclone

5)在“Source for Restore”中,点击“From Device”,点击…导航按钮

6)单击Add并导航到您的备份

7)在“恢复”旁打勾(选择要恢复的备份集)

8)接下来选择上方LH角的OPTIONS页面

9)现在在RESTORE AS中编辑数据库文件名。对db和日志都这样做。即billingclone。MDF和billingclone_log.ldf

10)现在点击OK,等待任务完成。

11)在对象资源管理器中点击刷新,你将看到你的新db

12)现在你可以把你的账单数据库重新上线了。使用脱机计费时使用的相同查询窗口。使用该命令:

-- 1) free up all USER databases
USE master; GO
-- 2) restore access to all users:
ALTER DATABASE billing SET MULTI_USER WITH ROLLBACK IMMEDIATE;GO
-- 3) put the db back online:
ALTER DATABASE billing SET ONLINE;

完成了!

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

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

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

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