使用SMO Restore对象还原差异备份

时间:2021-10-22 22:48:34

I am trying to restore a database by first restoring a full backup and then restoring a differential backup by using the Microsoft.SqlServer.Management.Smo.Restore class. The full backup is restored with the following code:

我试图通过首先还原完整备份然后使用Microsoft.SqlServer.Management.Smo.Restore类还原差异备份来还原数据库。使用以下代码恢复完整备份:

Restore myFullRestore = new Restore();
myFullRestore.Database = "DatabaseName";
myFullRestore.Action = RestoreActionType.Database;
myFullRestore.AddDevice(@"C:\BackupFile.bak", DeviceType.File);
myFullRestore.FileNumber = 1;
myFullRestore.SqlRestore(myServer); // myServer is an already-existing instance of Microsoft.SqlServer.Management.Smo.Server

After restoring the full backup (which completes successfully), my code for restoring the differential backup is as follows:

恢复完整备份(成功完成)后,恢复差异备份的代码如下:

Restore myDiffRestore = new Restore();
myDiffRestore.Database = "DatabaseName";
myDiffRestore.Action = RestoreActionType.Database;
myDiffRestore.AddDevice(@"C:\BackupFile.bak", DeviceType.File);
myDiffRestore.FileNumber = 4; // file contains multiple backup sets, this is the index of the set I want to use
myDiffRestore.SqlRestore(myServer);

However, this code will throw a Microsoft.SqlServer.Management.Smo.FailedOperationException, with the message "Restore failed for server 'servername'". Do I need to explicitly state that I am restoring a differential backup, and if so, how do I go about doing this? Or is the problem less obvious than this? Any suggestions as to what I am doing wrong (or neglecting to do) would be greatly appreciated.

但是,此代码将抛出Microsoft.SqlServer.Management.Smo.FailedOperationException,并显示消息“为服务器'servername'恢复失败”。我是否需要明确声明我正在恢复差异备份,如果是,我该如何进行此操作?或者这个问题不是很明显吗?任何关于我做错事(或忽视做)的建议都将不胜感激。

Update: Not sure if this was originally a typo or whether earlier versions had this form of the API, but for later versions this line

更新:不确定这是否是最初的拼写错误或早期版本是否具有此形式的API,但对于此行的更高版本

fullRestore.AddDevice(...);

should be

fullRestore.Devices.AddDevice(...)

1 个解决方案

#1


After a little bit more digging I figured this one out. In order for the differential backup restore to work, the full restore needs to be performed with NoRecovery set to true:

经过一点点的挖掘后,我想出了这个。为了使差异备份还原起作用,需要在NoRecovery设置为true的情况下执行完全还原:

// before executing the SqlRestore command for myFullRestore...
myFullRestore.NoRecovery = true;

This specifies that another transaction log needs to be applied, which in this case is the differential backup. This page has some more information that I found useful: http://doc.ddart.net/mssql/sql70/ra-rz_9.htm

这指定需要应用另一个事务日志,在这种情况下是差异备份。这个页面有一些我发现有用的信息:http://doc.ddart.net/mssql/sql70/ra-rz_9.htm

#1


After a little bit more digging I figured this one out. In order for the differential backup restore to work, the full restore needs to be performed with NoRecovery set to true:

经过一点点的挖掘后,我想出了这个。为了使差异备份还原起作用,需要在NoRecovery设置为true的情况下执行完全还原:

// before executing the SqlRestore command for myFullRestore...
myFullRestore.NoRecovery = true;

This specifies that another transaction log needs to be applied, which in this case is the differential backup. This page has some more information that I found useful: http://doc.ddart.net/mssql/sql70/ra-rz_9.htm

这指定需要应用另一个事务日志,在这种情况下是差异备份。这个页面有一些我发现有用的信息:http://doc.ddart.net/mssql/sql70/ra-rz_9.htm