Sql2008.Database Restore fails with 'RestoreContainer::ValidateTargetForCreation'

Dec 27, 2012 at 10:24 AM


The following tasks work OK:

<MSBuild.ExtensionPack.Sql2008.Database TaskAction="Backup" DatabaseItem="Assassin" DataFilePath="..\..\DBBackups\Assassin.bak"/>    

<MSBuild.ExtensionPack.Sql2008.Database TaskAction="VerifyBackup" DataFilePath="..\..\DBBackups\Assassin.bak"/>


BUT I couldn't Restore using:

<MSBuild.ExtensionPack.Sql2008.Database TaskAction="Restore" DatabaseItem="Assassin" DataFilePath="..\..\DBBackups\Assassin.bak" />

and even with

<MSBuild.ExtensionPack.Sql2008.Database TaskAction="Restore" DatabaseItem="Assassin" DataFilePath="..\..\DBBackups\Assassin.bak" ReplaceDatabase="True" LogFilePath="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\Assassin_log.LDF"/>

I have the following error lines:

Restoring SQL Database: Assassin from E:\Sources\Assassin\Base\Dev\DBBackups\Assassin.bakE:\Sources\Assassin\Base\Dev\\BuildScripts\RestoreDataBase_SqlServer.msbuild(12,9): error : FailedOperationException: Restore failed for Server 'PC-ALEX'. E:\Sources\Assassin\Base\Dev\\BuildScripts\RestoreDataBase_SqlServer.msbuild(12,9): error : SmoException: System.Data.SqlClient.SqlError: The operating system returned the error '32(failed to retrieve text for this error. Reason: 15105)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'E:\Sources\Assassin\Base\Dev\DBBackups\Assassin.bak'.E:\Sources\Assassin\Base\Dev\\BuildScripts\RestoreDataBase_SqlServer.msbuild(12,9): error : 

MS Mngm studio SQL which works OK is:

RESTORE DATABASE [Assassin] FROM  DISK = N'E:\Sources\Assassin\Base\Dev\DBBackups\Assassin.bak' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10

What can I do to fix?

Jan 26, 2014 at 9:50 AM
Sorry I missed this. I'll try figure it out.

Jan 26, 2014 at 10:13 AM
Is this windows 7? may be an OS issue. http://serverfault.com/questions/133599/error-when-restoring-database-windows-7-test-environment

but the API should resolve anyway so something else is going on.... investigating.
Jan 26, 2014 at 11:48 AM
running the same code on windows 8 gives the correct error

j(23,9): error : SmoException: System.Data.SqlClient.SqlError: The operating system returned the error '32(The process cannot access the file because it is being used by another process.)' while attempting 'RestoreContainer::ValidateTargetForCreation'

I'll try figure out a fix.

Jan 26, 2014 at 12:27 PM
So it looks like SQL has the files locked. You'll need to restore to a different file location (SQL will delete the files in use automatically). If you like you can then script a move of the datafiles back to where you need them.


<MSBuild.ExtensionPack.Sql2008.Database TaskAction="Restore" DatabaseItem="Assassin" DataFilePath="....\DBBackups\Assassin.bak" ReplaceDatabase="True" NewDataFilePath="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\Assasin_Restore.mdf" LogFilePath="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\Assassin_Restore_log.LDF" LogName="Assassin_log"/>

I think I edited that right... heres my working repo:
    <MSBuild.ExtensionPack.Sql2008.Database TaskAction="Restore" DatabaseItem="Mike" DataFilePath="C:\Databases\Backup\Mike-b.bak" NewDataFilePath="c:\b\Mike.mdf" ReplaceDatabase="true" LogFilePath="c:\b\Mike_log.ndf" LogName="Mike_log"/>
Marked as answer by mikeFourie on 1/26/2014 at 5:27 AM
Aug 26, 2014 at 9:58 AM
Figure out how easily restore SQL database without facing any trouble. Go through this tutorial which suggests you right way to recover SQL database: - http://www.sqlrepairtool.org/sql-2008-database-restore.html