AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 56319
Accepted
Mike Henderson
Mike Henderson
Asked: 2014-01-08 19:16:58 +0800 CST2014-01-08 19:16:58 +0800 CST 2014-01-08 19:16:58 +0800 CST

文件还原的意外结果

  • 772

我正在使用 AdventureWorks 数据库练习文件恢复,但没有得到我期望的恢复。我确信有一些非常简单的原因,但我还没有找到它。

这是我所做的

  1. 创建了一个名为USERTABLES_01.
  2. 将数据从表复制HumanResources.Employee到新的 . HumanResources.Employee_01在USERTABLES_01文件组中。
  3. 生成了完整备份。
  4. DELETE FROM HumanResources.Employee_01在没有 where 子句的情况下执行
  5. Shazbot,我需要恢复表格。好的,我会恢复文件组USERTABLES_01。
  6. 执行RESTORE DATABASE [AdventureWorks2012] FILE N'AdventureWorks2012_UserTables_01'...
  7. 生成尾日志备份
  8. 恢复尾日志备份

一切恢复无怨无悔。但是我HumanResources.Employee_01的没有任何数据。

查看MS Example,也许我没有正确遵循它,但我很确定我做到了。

查看了类似的问题,但不确定这是我正在寻找的答案: Restore .mdf data file for a filegroup in SQL Server。

sql-server filegroups
  • 2 2 个回答
  • 127 Views

2 个回答

  • Voted
  1. Best Answer
    Sebastian Meine
    2014-01-09T05:22:26+08:002014-01-09T05:22:26+08:00

    SQL Server 的最高优先级是遵守ACID属性。这意味着,无论您做什么,您的数据库始终在事务上保持一致。

    但是,您尝试执行的操作可能会使您陷入不一致的状态。想一想解释 ACID 属性的标准示例:银行帐户。假设您有两个帐户驻留在不同的文件组中(例如,两个帐户表或一个具有分区)。现在您在事务中将资金从文件组一转移到文件组二。然后你去恢复文件组一到传输之前。这导致钱现在同时存在于两个账户中,这显然不是理想的状态。

    那么,您的示例中发生了什么?

    6) 文件组成功恢复到灾难发生前的某个时间点。但是它处于“正在恢复”状态并且不可访问。

    8) 日志恢复使文件组与数据库的其余部分同步(在事务级别)。为此,日志备份中捕获的所有已提交事务都将重新应用于该文件组。但是,删除是一个已提交的事务,因此它也会重新执行。这使数据库保持一致,但表为空。

    您指向的 Technet 示例假设文件由于某些外部原因(例如驱动器故障)而脱机。在这种情况下,所描述的恢复是有意义的,因为您希望重新应用所有事务。

    在您的情况下,您需要在其他地方恢复数据库,然后将数据复制回原始表。为此,您可以只恢复包含相关数据的文件组和主文件组。您不能只恢复辅助文件组,主文件组也必须始终恢复。这是建议不要在主文件组中包含任何数据的原因之一。这样一来,必须恢复那个的影响也很小。

    • 1
  2. Thomas Stringer
    2014-01-09T06:08:34+08:002014-01-09T06:08:34+08:00

    在恢复序列之后,您的表中没有数据,因为您恢复了上次日志备份的所有事务,其中包括从表中删除的数据。

    您基本上要寻找的是时间点还原。这是一个工作示例(注意:正如 Sebastian 已经暗示的那样,在对文件执行时间点还原时需要还原 PRIMARY 文件组):

    use AdventureWorks2012RestoreTest;
    go
    
    -- create a new filegroup
    --
    alter database AdventureWorks2012RestoreTest
    add filegroup USERTABLES_01;
    go
    
    -- create a new data file in the new filegroup
    --
    alter database AdventureWorks2012RestoreTest
    add file
    (
        name = Aw2012Rt_data2,
        filename = '<backup dir>\Aw2012Rt_data2.ndf',
        size = 2 MB
    ) to filegroup USERTABLES_01;
    
    -- create the duplicate table on the new filegroup
    --
    create table [HumanResources].[Employee_01]
    (
        [BusinessEntityID] [int] NOT NULL,
        [NationalIDNumber] [nvarchar](15) NOT NULL,
        [LoginID] [nvarchar](256) NOT NULL,
        [OrganizationNode] [hierarchyid] NULL,
        [OrganizationLevel] [smallint] NULL,
        [JobTitle] [nvarchar](50) NOT NULL,
        [BirthDate] [date] NOT NULL,
        [MaritalStatus] [nchar](1) NOT NULL,
        [Gender] [nchar](1) NOT NULL,
        [HireDate] [date] NOT NULL,
        [SalariedFlag] [bit] NOT NULL,
        [VacationHours] [smallint] NOT NULL,
        [SickLeaveHours] [smallint] NOT NULL,
        [CurrentFlag] [bit] NOT NULL,
        [rowguid] [uniqueidentifier] NOT NULL,
        [ModifiedDate] [datetime] NOT NULL
    ) on USERTABLES_01;
    go
    
    -- populate the new table
    --
    insert into HumanResources.Employee_01
    select *
    from HumanResources.Employee;
    
    -- get initial count of table
    --
    select count(*)
    from HumanResources.Employee_01;
    -- 290
    
    -- initiate a full backup of the database
    --
    backup database AdventureWorks2012RestoreTest
    to disk = '<backup dir>\Aw2012RT_full.bak'
    with init;
    go
    
    -- because we need to do a point-in-time recovery we might as
    -- well get the datetime stamp right before the delete
    --
    select getdate();
    -- 2014-01-08 05:34:56.767
    
    -- "accidentally" delete all data
    --
    delete from HumanResources.Employee_01;
    
    -- verify all of the data is gone
    --
    select count(*)
    from HumanResources.Employee_01;
    -- 0
    
    -- start the restore sequence
    --
    use master;
    go
    
    -- you need to restore the PRIMARY filegroup in order for point-in-time recovery
    -- othewise you'll get the following error message:
    -- "Point-in-time recovery is not possible unless the primary filegroup is part 
    -- of the restore sequence. Omit the point-in-time clause or restore the primary filegroup."
    --
    backup log AdventureWorks2012RestoreTest
    to disk = '<backup dir>\Aw2012RT_log.trn'
    with init, norecovery;
    go
    
    restore database AdventureWorks2012RestoreTest
    file = 'Aw2012Rt_data2',
    filegroup = 'PRIMARY'
    from disk = '\\stringermain\VmShare\temp\Aw2012RT_full.bak'
    with norecovery;
    go
    
    -- now you can do point-in-time recovery with the STOPAT clause
    -- specifying a datetime stamp before the delete operation
    --
    restore log AdventureWorks2012RestoreTest
    from disk = '<backup dir>\Aw2012RT_log.trn'
    with 
        stopat = '2014-01-08 05:34:56.767',
        recovery;
    go
    
    use AdventureWorks2012RestoreTest;
    go
    
    -- verify that we now have data in the table
    --
    select count(*)
    from HumanResources.Employee_01;
    -- 290
    

    正如 Sebastian 还提到的那样,如果您希望取回已删除的数据,那么在其他地方恢复并复制数据可能本质上更容易。但我想展示你更正的测试用例只是为了说明你将如何进行时间点恢复。

    • 1

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve