Differential backup recovery using SMO recovery object

I am trying to restore a database by first restoring a full backup and then restoring a differential backup using the Microsoft.SqlServer.Management.Smo.Restore class. A full backup is restored using 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 a full backup (which succeeds), my code for restoring a 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 Microsoft.SqlServer.Management.Smo.FailedOperationException, with the message "Restore failed for server" server name. Do I need to explicitly indicate that I am restoring a differential backup, and if so, how should I do it? Or a problem less obvious? Any suggestions regarding what I am doing wrong (or neglect to do) would be greatly appreciated.

Refresh . Not sure if this was originally a typo or whether there were previous versions of this form of the API, but for later versions this line

 fullRestore.AddDevice(...); 

it should be

 fullRestore.Devices.AddDevice(...) 
+4
source share
1 answer

After a bit more digging, I realized this. In order for differential backup recovery to work, you must perform a full recovery with NoRecovery set to true:

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

This indicates the need to use a different transaction log, which in this case is a differential backup. There is additional information on this page that I found useful: http://doc.ddart.net/mssql/sql70/ra-rz_9.htm

+4
source

Source: https://habr.com/ru/post/1286714/


All Articles