如何使用SQL 日志恢复数据
2010-06-10 11:05
369 查看
How do I recover data from SQL Server's log files?
There are several tools available for reviewing SQL Server log files, and restoring data that was accidentally lost or corrupted, say, after your last backup. Three such products are:Apex SQL Log
Log Explorer
SQL Log Rescue
What follows is the reason this article was written in the first place... a big OOPS by yours truly, and a surprisingly easy procedure for turning that OOPS into a complete recovery (using Lumigent's Log Explorer).
Let's say I create a database called foobar, create a table called foo1, and populate the table with some fictitious data:
SET NOCOUNT ON CREATE DATABASE foobar GO USE foobar GO CREATE TABLE foo1 ( id INT PRIMARY KEY, relID INT ) SET NOCOUNT ON INSERT foo1 VALUES(1, 1) INSERT foo1 VALUES(2, 5) INSERT foo1 VALUES(3, 3) INSERT foo1 VALUES(4, 2) INSERT foo1 VALUES(5, 7) INSERT foo1 VALUES(6, 9) INSERT foo1 VALUES(7, 4) INSERT foo1 VALUES(8, 5) |
So now, I want to UPDATE one row, and I open a new Query Analyzer window and say:
UPDATE foo1 SET relID = 5 |
Lumigent's Log Explorer that will allow you to easily parse SQL Server's logs and recover data after an irreparable event, such as a DELETE or UPDATE without a WHERE clause, not within a transaction, and before a single backup has been performed.
So, I can open Log Explorer, select Attach Log File:
Then I can Connect, and then I can select my database and Attach:
Next, I want to move to Filter Log Records so I can only show MODIFY ROW statements:
If I move to Browse / View Log, you can see that I can scroll through my six update statements (remember, two of them did not apply because the data didn't change):
If I right-click and choose Undo Transaction, I will be presented with a set of reverse operations in a file called Recovery.sql. In this case, the output is as follows:
UPDATE [dbo].[foo1] SET [relID] = 4 WHERE [id] = 7 AND [relID] = 5 UPDATE [dbo].[foo1] SET [relID] = 7 WHERE [id] = 5 AND [relID] = 5 UPDATE [dbo].[foo1] SET [relID] = 2 WHERE [id] = 4 AND [relID] = 5 UPDATE [dbo].[foo1] SET [relID] = 3 WHERE [id] = 3 AND [relID] = 5 UPDATE [dbo].[foo1] SET [relID] = 1 WHERE [id] = 1 AND [relID] = 5 UPDATE [dbo].[foo1] SET [relID] = 9 WHERE [id] = 6 AND [relID] = 5 |
Some further information about Log Explorer:
Log Explorer can read SQL Server's online transaction log and transaction log backup files on disk. There is no limit to the size or length of history in the logs, but the log backups must be SQL Server format on a disk Log Explorer can read.
Log Explorer can read the transaction log regardless of the Recovery Model (Simple, Bulk, or Full). However, if the recovery model is set to Simple (Truncate Log On Checkpoint ON for SQL Server 7) then SQL Server can overwrite transactions in the log. If transactions are overwritten by SQL or for any other reason (e.g. a TRUNCATE operation) they do not exist, then Log Explorer obviously cannot find them. So, Bulk and Full Recovery models are recommended to ensure coverage by Log Explorer.
As you can see from some of the screenshots, there is a lot of functionality buried in Log Explorer. I highly recommend downloading the evaluation version and playing with it against Pubs, to see how powerful it really is.
Related Articles
Are there tools available for auditing changes to SQL Server data?
Can I create an index on a BIT column?
Can I have optional parameters to my stored procedures?
Can I implement an input mask in SQL Server?
Can I make SQL Server format dates and times for me?
Can I start IDENTITY values at a new seed?
Can SQL Server tell me which row was inserted most recently?
How can I learn more about undocumented SQL Server stored procedures?
How can I make my SQL queries case sensitive?
How do I audit changes to SQL Server data?
How do I connect to a non-default instance of SQL Server?
How do I connect to SQL Server on a port other than 1433?
How do I create a cross-tab (or "pivot") query?
How do I determine if a table exists in a SQL Server database?
How do I drop a SQL Server database?
How do I find all the available SQL Servers on my network?
How do I get a list of SQL Server tables and their row counts?
How do I get rid of Named Pipes / DBNMPNTW errors?
How do I get the correct date/time from the msdb.sysjob* tables?
How do I get the nth row in a SQL Server table?
How do I get the result of dynamic SQL into a variable?
How do I handle REPLACE() within an NTEXT column in SQL Server?
How do I hide system tables in SQL Server's Enterprise Manager?
How do I know which version of SQL Server I'm running?
How do I limit the number of rows returned in my resultset?
How do I load text or csv file data into SQL Server?
How do I manage changes in SQL Server objects?
How do I monitor SQL Server performance?
How do I prevent linked server errors?
How do I reclaim space in SQL Server?
How do I search for special characters (e.g. %) in SQL Server?
How do I start SQL Server Agent from ASP?
How do I time my T-SQL code?
How do I upsize from Access to SQL Server?
How do I upsize my MSDE database to full-blown SQL Server 2000?
How do I use a variable in a TOP clause in SQL Server?
How do I use GETDATE() within a User-Defined Function (UDF)?
How should I store an IP address in SQL Server?
Schema: How do I find all the foreign keys in a database?
SQL Server & MSDE
What are reserved Access, ODBC and SQL Server keywords?
What are the capacities of Access, SQL Server, and MSDE?
What are the main differences between Access and SQL Server?
What do I need to know about SQL Server 2000 SP4?
Where else can I learn about SQL Server?
Where is SP4 for SQL Server 2000?
Why am I having problems with SQL Server 2000 SP3 / SP3a?
Why can't I install SQL Server on Windows Server 2003?
Why can't I install SQL Server on Windows XP?
Why can't I use LIKE '%datepart%' queries?
Why do I get "Login failed for user '/'."?
Why do I get 'object could not be found' or 'invalid object name'?
Why do I get errors about master..spt_values?
Why do I get script errors in Enterprise Manager's 'taskpad' view?
Why do I get SQLSetConnectAttr Failed errors?
Why do I have problems with views after altering the base table?
Why does EM crash when I get an error in a stored procedure?
Why does Enterprise Manager return 'Invalid cursor state'?
Why does my DELETE query not work?
Why does sp_spaceused return inaccurate values?
Why is Enterprise Manager slow at expanding my database list?
Why is my app slow after upgrading from SQL Server 7 to 2000?
Why is tempdb full, and how can I prevent this from happening?
Why should I consider using an auxiliary calendar table?
Why should I consider using an auxiliary numbers table?
相关文章推荐
- 使用binlog2sql做数据恢复的简单示例
- SQLServer2005数据库日志文件损坏的情况下如何恢复数据库
- SQL数据修复,SQL数据库修复,SQL数据库修复软件,SQL数据库恢复,SQL数据库日志恢复软件
- 使用日志恢复数据库
- 人工误删除InnoDB ibdata数据文件与ib_logile重做日志文件如何恢复详细过程
- 使用SQL产生BCP命令快速备份/恢复你所有数据
- 数据误删后如何使用Flashback恢复数据
- 没有备份、只有归档日志,如何恢复数据文件?
- SQL2005 如何在没有日志文件的情况下如何恢复MDF数据库文件
- 如何让Gridview在没有数据的时候显示表头[没有使用SqlDataSource控件时]
- 没有备份只有归档日志如何恢复数据…
- 如何使用sql语句备份和恢复mysql数据库
- LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据[转]
- MYSQL--如何用日志文件恢复数据
- 在 Linux 下使用 RAID(八):当软件 RAID 故障时如何恢复和重建数据
- 没有备份、只有归档日志,如何恢复数据文件?
- (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法
- 如何通过Mysql的二进制日志恢复数据库数据
- pl/sql,Oracle数据库中,不小心delete数据并提交后如何恢复被删数据
- 使用日志恢复误删除的数据文件