首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从失败的SQL还原中恢复?

问如何从失败的SQL还原中恢复?
EN

Database Administration用户
提问于 2015-10-15 21:20:18
回答 2查看 963关注 0票数 0

在网页中的还原代码失败后,数据库将处于“还原”模式和/或脱机状态。在退出代码之前,如何恢复以便有一个可操作的在线数据库?

失败后的任何页面加载都会尝试检查用户是否已登录,并要求数据库在线和可用,因此用户不再登录。我倾向于不改变用户使用我正在还原的数据库登录的要求。

详细信息:

不知何故,我得到了一个损坏的SQL备份。在这种情况下,这是一件好事,因为它允许我检查错误捕获代码。完整备份的还原已完成,但以下差异备份的还原失败。然而,我的问题涉及到任何失败--而不仅仅是我目前的问题。错误是:由于数据库尚未恢复到正确的早期状态,因此无法还原此差异备份。我不需要知道原因-只是如何恢复之后和其他恢复失败。这些选项中的任何一个都可以,但在失败之后我什么都不能做,而且……在我离开密码之前。

  • 中止当前还原,然后使用已知的良好备份重新启动--而不留下代码--从而导致页面加载事件。我似乎不能放弃一切而重新开始。
  • 只需将数据库恢复到恢复之前的状态--在线而不是恢复模式。是否有一些我不知道这样做的SMO选项?
  • 等。我还有其他选择吗?

更多详情:

  • 我遗漏了许多需要简化的代码--工作代码附在一起。
  • 当备份良好时,代码可以正常工作。
  • 数据库很小-3M字节左右。
  • 数据库备份处于简单模式。我们不需要日志备份/还原或事务备份/还原
  • 我们每月做一次完整的备份,并且每天对同一个文件进行差异备份。除了恢复到选定的完整备份或差异备份之外,我们永远不需要进行还原。

任何建议都会非常感谢--或者更好的是,告诉我一些从恢复失败中恢复的代码示例。我发现的大多数例子甚至都没有捕捉错误。

丹尼斯

代码语言:javascript
复制
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();     
}
EN

回答 2

Database Administration用户

发布于 2015-10-16 01:49:45

一旦恢复启动,目标数据库就会被覆盖,并且无法恢复到它的原始状态(sans还原)。考虑还原到不同的数据库名,如果成功,则删除另一个数据库,并将还原的数据库重命名为所需的名称。

票数 2
EN

Database Administration用户

发布于 2018-12-07 11:31:30

是的,我百分之百同意丹。由于您的完整数据库备份已损坏,并且每天都进行差异备份,因此,在这种情况下:如果有差异备份,请不要在还原时选择覆盖选项。在还原数据库窗口中,在恢复状态下选择使用NORECOVERY选项进行还原。如果选择使用具有差异备份的恢复进行还原,则数据库将进入还原状态。

票数 0
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/118216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档