search
HomeDatabaseMysql TutorialSQLSERVER拯救某个时间点被误删除的数据

SQLSERVER 拯救 某个 时间 点被误 删除 的 数据 http://blog.csdn.net/dba_huangzj/article/details/8491327 要 拯救 某个 时间 点被误 删除 的 数据 ,需要你在那个 时间 点之前有一个完整 数据 库备份。 而且,需要你的 数据 库恢复模式为:完整恢复模式

SQLSERVER拯救某个时间点被误删除数据

http://blog.csdn.net/dba_huangzj/article/details/8491327

拯救某个时间点被误删除数据,需要你在那个时间点之前有一个完整数据库备份。

而且,需要你的数据库恢复模式为:“完整恢复模式”

如何查看你的数据库恢复模式为完整恢复模式,运行下面SQL

<span>1</span> <span>SELECT</span><span>  recovery_model, recovery_model_desc
</span><span>2</span> <span>FROM</span><span>    sys.databases
</span><span>3</span> <span>WHERE</span>   name <span>=</span> <span>'</span><span>GPOSDB</span><span>'</span>

SQLSERVER拯救某个时间点被误删除的数据

在创建完一个新数据库之后,强烈建议做一次数据库完整备份

-------------------------------------------------------------------华丽的分割线-----------------------------------------------------------------------

先创建测试表

<span> 1</span> <span>/*</span>
<span> 2</span> <span>由于tempdb永远为简单恢复模式,所以不适合做案例。
</span><span> 3</span> <span>这里使用我的测试机器里的<strong>数据</strong>库GPOSDB
</span><span> 4</span> <span>*/</span>
<span> 5</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span>
<span> 6</span> <span>GO</span>
<span> 7</span> <span>IF</span> <span>OBJECT_ID</span>(<span>'</span><span>testRestore</span><span>'</span>) <span>IS</span> <span>NOT</span> <span>NULL</span> 
<span> 8</span>     <span>DROP</span> <span>TABLE</span><span> testRestore
</span><span> 9</span> <span>GO</span>
<span>10</span> <span>CREATE</span> <span>TABLE</span><span> testRestore
</span><span>11</span> <span>    (
</span><span>12</span>       id <span>INT</span> <span>IDENTITY</span>(<span>1</span>, <span>1</span><span>) ,
</span><span>13</span>       NAME <span>VARCHAR</span>(<span>50</span><span>)
</span><span>14</span>     );

插入测试数据

<span> 1</span> <span>--</span><span>插入测试<strong>数据</strong>:   </span>
<span> 2</span> <span>INSERT</span> <span>INTO</span><span> testRestore(Name)
</span><span> 3</span> <span>SELECT</span> <span>'</span><span>test1</span><span>'</span>
<span> 4</span> <span>UNION</span> <span>ALL</span> 
<span> 5</span> <span>SELECT</span> <span>'</span><span>test2</span><span>'</span>
<span> 6</span> <span>UNION</span> <span>ALL</span> 
<span> 7</span> <span>SELECT</span> <span>'</span><span>test3</span><span>'</span>
<span> 8</span> <span>UNION</span> <span>ALL</span> 
<span> 9</span> <span>SELECT</span> <span>'</span><span>test4</span><span>'</span>
<span>10</span> <span>UNION</span> <span>ALL</span> 
<span>11</span> <span>SELECT</span> <span>'</span><span>test5</span><span>'</span>
<span>12</span> <span>UNION</span> <span>ALL</span> 
<span>13</span> <span>SELECT</span> <span>'</span><span>test6</span><span>'</span>
<span>14</span> <span>UNION</span> <span>ALL</span> 
<span>15</span> <span>SELECT</span> <span>'</span><span>test7</span><span>'</span>
<span>16</span> <span>UNION</span> <span>ALL</span> 
<span>17</span> <span>SELECT</span> <span>'</span><span>test8</span><span>'</span>

 

<span>1</span> <span>SELECT</span> <span>*</span> <span>FROM</span> testRestore

SQLSERVER拯救某个时间点被误删除的数据

先来一个数据库完整备份,在22:32的时候完整备份数据

<span>1</span> <span>BACKUP</span> <span>DATABASE</span> <span>[</span><span>GPOSDB</span><span>]</span>
<span>2</span> <span>TO</span> <span>DISK</span><span>=</span><span>'</span><span>D:\GPOSDB_FULLBACKUP2013-07-23.bak</span><span>'</span>
<span>3</span> <span>WITH</span><span> INIT
</span><span>4</span> <span>go</span>

模拟删除数据,这里使用waitfor time,目的是使删除数据时间准确一点,这里选择22:34的时候删除数据方便后面还原某个时间点之前的数据

<span>1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span>
<span>2</span> <span>GO</span>
<span>3</span> <span>WAITFOR</span> TIME <span>'</span><span>22:34</span><span>'</span>
<span>4</span> <span>DELETE</span> <span>FROM</span> dbo.testRestore


查询一下数据是否删除

<span>1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span>
<span>2</span> <span>GO</span>
<span>3</span> <span>SELECT</span> <span>*</span> <span>FROM</span> dbo.testRestore

SQLSERVER拯救某个时间点被误删除的数据

断开与GPOSDB数据库的连接,如果还有连接就kill掉连接,然后执行事务日志备份,在22:36的时候执行事务日志备份

<span> 1</span> <span>--</span><span><strong>删除</strong>了表中的<strong>数据</strong>之后最少要等3分钟才执行下面SQL语句,不然的话等一下还原事务日志的时候会报错</span>
<span> 2</span> <span>--</span><span>必须断开所有与GPOSDB<strong>数据</strong>库的连接</span>
<span> 3</span> <span>USE</span><span> master
</span><span> 4</span> <span>GO</span>
<span> 5</span> <span>BACKUP</span> <span>LOG</span> <span>[</span><span>GPOSDB</span><span>]</span> <span>TO</span>  <span>DISK</span> <span>=</span> N<span>'</span><span>D:\GPOSDB_LOGBACKUP2013-07-23.bak</span><span>'</span> <span>WITH</span>  NO_TRUNCATE , NOFORMAT,  NAME <span>=</span> N<span>'</span><span>GPOSDB-事务日志备份</span><span>'</span>, SKIP,  NORECOVERY ,  STATS <span>=</span> <span>10</span><span>, CHECKSUM
</span><span> 6</span> <span>GO</span>
<span> 7</span> <span>DECLARE</span> <span>@backupSetId</span> <span>AS</span> <span>INT</span>
<span> 8</span> <span>SELECT</span>  <span>@backupSetId</span> <span>=</span><span> position
</span><span> 9</span> <span>FROM</span><span>    msdb..backupset
</span><span>10</span> <span>WHERE</span>   database_name <span>=</span> N<span>'</span><span>GPOSDB</span><span>'</span> <span>AND</span> backup_set_id <span>=</span> ( <span>SELECT</span>
<span>11</span>                                                               <span>MAX</span><span>(backup_set_id)
</span><span>12</span>                                                         <span>FROM</span><span>  msdb..backupset
</span><span>13</span>                                                         <span>WHERE</span> database_name <span>=</span> N<span>'</span><span>GPOSDB</span><span>'</span>
<span>14</span> <span>                                                      )
</span><span>15</span> <span>IF</span> <span>@backupSetId</span> <span>IS</span> <span>NULL</span>  <span>--</span><span>这里是验证事务日志备份是否成功,如果没有断开与GPOSDB<strong>数据</strong>库的连接备份事务日志可能会失败</span>
<span>16</span>     <span>BEGIN</span>
<span>17</span>         <span>RAISERROR</span>(N<span>'</span><span>验证失败。找不到<strong>数据</strong>库“GPOSDB”的备份信息。</span><span>'</span>, <span>16</span>, <span>1</span><span>)
</span><span>18</span>     <span>END</span>
<span>19</span> <span>RESTORE</span> VERIFYONLY <span>FROM</span>  <span>DISK</span> <span>=</span> N<span>'</span><span>D:\GPOSDB_LOGBACKUP2013-07-23.bak</span><span>'</span> <span>WITH</span>  <span>FILE</span> <span>=</span> <span>@backupSetId</span>
<span>20</span> <span>GO</span>

SQLSERVER拯救某个时间点被误删除的数据

<span>1</span> <span>--</span><span>查询是否备份成功</span>
<span>2</span> <span>SELECT</span> <span>TOP</span> <span>10</span> <span>*</span> <span>FROM</span>    msdb..backupset <span>ORDER</span> <span>BY</span> <span>[</span><span>backup_set_id</span><span>]</span> <span>DESC</span> 

SQLSERVER拯救某个时间点被误删除的数据

还原数据库完整备份

<span>1</span> <span>--</span><span><strong>数据</strong>库处于正在还原状态</span>
<span>2</span> <span>RESTORE</span> <span>DATABASE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span>FROM</span> <span>DISK</span><span>=</span><span>'</span><span>D:\GPOSDB_FULLBACKUP2013-07-23.bak</span><span>'</span> <span>WITH</span> <span>replace</span>, NORECOVERY 

SQLSERVER拯救某个时间点被误删除的数据

 还原事务日志

<span>1</span> <span>--</span><span>因为<strong>删除</strong><strong>数据</strong>是在'22:34' 所以还原到22:33</span>
<span>2</span> <span>RESTORE</span> <span>LOG</span> <span>[</span><span>GPOSDB</span><span>]</span> <span>FROM</span> <span>DISK</span><span>=</span><span>'</span><span>D:\GPOSDB_LOGBACKUP2013-07-23.bak</span><span>'</span> <span>WITH</span> RECOVERY ,STOPAT<span>=</span><span>'</span><span>2013-7-23 22:33</span><span>'</span>

如果事务日志还原失败的话,可以使用GUI界面

SQLSERVER拯救某个时间点被误删除的数据

SQLSERVER拯救某个时间点被误删除的数据

查看表中的数据

<span>1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span>
<span>2</span> <span>GO</span>
<span>3</span> <span>SELECT</span> <span>*</span> <span>FROM</span> dbo.testRestore

SQLSERVER拯救某个时间点被误删除的数据

 -----------------------------------------------------------华丽的分割线--------------------------------------------

要注意的两个地方

(1)如果你发现误操作以后还有很多人做了操作,那么你还原成功后,别人的操作就会冲掉,所以发生误操作后,要马上停止别人对数据库的操作。

(2)这个方法要对数据库独占

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)