使用 SMO 恢复对象恢复差异备份

发布于 2024-07-25 08:04:10 字数 1273 浏览 10 评论 0原文

我尝试通过首先还原完整备份,然后使用 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

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

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);

但是,此代码将抛出 Microsoft.SqlServer.Management.Smo.FailedOperationException,其中消息“服务器‘servername’恢复失败”。 我是否需要明确声明我正在恢复差异备份,如果需要,我该如何执行此操作? 或者问题没有比这更明显? 任何关于我做错了什么(或忽略做什么)的建议将不胜感激。

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

fullRestore.AddDevice(...);

应该是

fullRestore.Devices.AddDevice(...)

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:

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.

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

fullRestore.AddDevice(...);

should be

fullRestore.Devices.AddDevice(...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-08-01 08:04:10

经过一番挖掘后,我弄清楚了这一点。 为了使差异备份恢复工作,需要在 NoRecovery 设置为 true 的情况下执行完整恢复:

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

这指定需要应用另一个事务日志,在本例中是差异备份。 此页面包含一些我认为有用的更多信息:
http://doc.ddart.net/mssql/sql70/ra-rz_9。嗯

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:

// 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文