I wrote my own little database library that fits our needs. Every steps that we defined necessary while restoring a database is coded to nothing needs to be executed manually. But the last two restores failed at 70 percent.
IMPORTANT: The same code below worked just fine on one of the fastest sql-servers that we have. It is bare-metal compared to the others that are virtualized! The database was over 100 gigabyte in size!
FOR SMALL DATABASES THE RESTORE WORKS ON EVERY SERVER!
To me it seems like it's an timeout problem or something like that. It loses connection and breaks.
The code for restoring the database:
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;
namespace BSH.Internal
{
internal class DBRestoreBase
{
public void Restore(string databaseName, string databaseServer, string backupLocation, IList<RelocateFile> relocateFiles)
{
try
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(backupLocation, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(databaseServer);
Server sqlServer = new Server(connection);
Microsoft.SqlServer.Management.Smo.Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
db = sqlServer.Databases[databaseName];
foreach (RelocateFile relocateFile in relocateFiles)
{
sqlRestore.RelocateFiles.Add(relocateFile);
}
sqlRestore.ReplaceDatabase = true;
sqlRestore.Complete += (obj, e) => {
Console.WriteLine("Restore completed");
Console.WriteLine("-----------------------------------------------");
};
sqlRestore.PercentCompleteNotification = 10;
sqlRestore.PercentComplete += (obj, e) => { Console.WriteLine("Percent completed: {0}%.", e.Percent); };
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
catch (Exception ex)
{
Console.WriteLine(ex);
};
}
}
}
I am totally clueless what the problem could be. I have to say that I'm new to C# with little knowledge!