您的位置:首页 > 数据库

如何使用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)
Now let's say I've thrown away the table or file that showed me those relationships of id <-> relID and allowed me to create the above INSERT statements. I've also closed this Query Analyzer window, so I can't even recover the script I used to populate the table. (Pretend, for a moment, that the script was too long to be memorized.)

So now, I want to UPDATE one row, and I open a new Query Analyzer window and say:

UPDATE foo1 SET relID = 5
After I hit F5, I realize: oh dear, I forgot to add a WHERE clause. I just updated all of the data in my table! I wasn't in a transaction, so I can't roll back. I don't have a backup, so I can't restore. What do I do?

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
Now you can restore your data as it was, before the fat-fingered UPDATE statement!

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?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐