在网页中的还原代码失败后,数据库将处于“还原”模式和/或脱机状态。在退出代码之前,如何恢复以便有一个可操作的在线数据库?
失败后的任何页面加载都会尝试检查用户是否已登录,并要求数据库在线和可用,因此用户不再登录。我倾向于不改变用户使用我正在还原的数据库登录的要求。
详细信息:
不知何故,我得到了一个损坏的SQL备份。在这种情况下,这是一件好事,因为它允许我检查错误捕获代码。完整备份的还原已完成,但以下差异备份的还原失败。然而,我的问题涉及到任何失败--而不仅仅是我目前的问题。错误是:由于数据库尚未恢复到正确的早期状态,因此无法还原此差异备份。我不需要知道原因-只是如何恢复之后和其他恢复失败。这些选项中的任何一个都可以,但在失败之后我什么都不能做,而且……在我离开密码之前。
更多详情:
任何建议都会非常感谢--或者更好的是,告诉我一些从恢复失败中恢复的代码示例。我发现的大多数例子甚至都没有捕捉错误。
丹尼斯
protected void Restore_Click(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
string RestoreDestinationPath = "C:\\SF_Access\\SciFairMasterWebsite-rev562-InWork\\App_Data\\backup\\SFMASPNETDB_BUSET_Started_2015-10.bak";
int SelPosNum = 14; // The Postion Number of Differential backup. 21 works
int fullBackupPosition = 13; // The Position number of the previous Full Backup 20 works
string databaseName = "SFMASPNETDB";
string bufilePath = RestoreDestinationPath ;
string dataFilePath = "C:\\SF_Access\\SciFairMasterWebsite-rev562-InWork\\App_Data\\";
string logFilePath = "C:\\SF_Access\\SciFairMasterWebsite-rev562-InWork\\App_Data\\";
SqlConnection sqlCon = new SqlConnection(ConnectionString);
ServerConnection connection = new ServerConnection(sqlCon);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(bufilePath, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
sqlRestore.ContinueAfterError = false;
sqlRestore.Action = RestoreActionType.Database;
string dataFileLocation = dataFilePath + databaseName + ".mdf";
string logFileLocation = logFilePath + databaseName + "_Log.ldf";
RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);
sqlRestore.ReplaceDatabase = true;
// WE MUST Take the current database offline to do the restore.
// Alternate... Some examples just set it to single user then back to multi user after restore
try
{
db.SetOffline();
}
catch (Exception yx)
{
sqlRestore.Abort(); // If set offline fails, This does NOT WORK - leaves the db in restoring status
connection.Cancel();
connection.Disconnect();
sqlCon.Close();
return;
}
sqlRestore.NoRecovery = true; // Set this for the Full Backup recovery
sqlRestore.Checksum = true; // Backups made with checksums.
sqlRestore.FileNumber = fullBackupPosition; // the previous full backup postition
try
{
sqlRestore.SqlRestore(sqlServer); // Restore Full Backup with NoRecovery
}
catch (Exception ex1)
{
sqlRestore.Abort(); // This does not take the database out of "Restoring" status
db.SetOnline(); // This actually fails so would need a try / catch to handle
connection.Cancel();
connection.Disconnect();
sqlCon.Close();
return;
}
sqlRestore.NoRecovery = false; // now get ready to do the incremental and let it Recover after restore
sqlRestore.FileNumber = SelPosNum; // position number of the incremental backup done after the full Backup
sqlRestore.Checksum = true;
try
{
sqlRestore.SqlRestore(sqlServer); // Restore incremental with RESTORE ***THIS FAILS***
}
catch (Exception ex)
{
// *** THIS IS THE AREA IN QUESTION ******
// This code leaves the database in "Restoring" status.
// Since the database is the membership database and administration is logged on, if the database is not
// restored, all other pages on the website requiring login fail.
// I need to:
// Take the database out of 'restoring' and start over with a different backup
// (I could make a Full Backup before I start for that purpose and it would probably restore without error.)
// Put it back online the way it was before I started this code (Delete it and attach a copy I could make before I start)
// or any other way to recover with an 'on line' database - before I exit this page.
string msg = ex.Message;
sqlRestore.Abort(); // This does not stop the restore state.
sqlServer.KillAllProcesses(databaseName); // ?? this does not help
sqlServer.Refresh();
connection.Cancel();
connection.Disconnect();
sqlCon.Close();
return;
}
try
{
db = sqlServer.Databases[databaseName];
db.SetOnline(); // some examples do not do these two lines. not using seems to cause problems
sqlServer.Refresh();
}
catch (Exception ez)
{
connection.Cancel(); // Important
connection.Disconnect();
sqlCon.Close(); //
return;
}
// The Full and Incremental Restores both completed without error and the database is online and operational
connection.Cancel();
connection.Disconnect();
sqlCon.Close();
}发布于 2015-10-16 01:49:45
一旦恢复启动,目标数据库就会被覆盖,并且无法恢复到它的原始状态(sans还原)。考虑还原到不同的数据库名,如果成功,则删除另一个数据库,并将还原的数据库重命名为所需的名称。
发布于 2018-12-07 11:31:30
是的,我百分之百同意丹。由于您的完整数据库备份已损坏,并且每天都进行差异备份,因此,在这种情况下:如果有差异备份,请不要在还原时选择覆盖选项。在还原数据库窗口中,在恢复状态下选择使用NORECOVERY选项进行还原。如果选择使用具有差异备份的恢复进行还原,则数据库将进入还原状态。
https://dba.stackexchange.com/questions/118216
复制相似问题