HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Attach ldf more current than mdf

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
mdfthanmoreattachldfcurrent

Problem

Is it possible to create a database where the ldf is more current then the mdf? Or to attach the older (more mature) ldf to the younger mdf (less mature)?

I am working on Steve Stedman's Week 3 Database Corruption Challenge

Scenario

At 2:53pm a full backup was performed. After that full backup, users continued to do work and the database continued to grow.

At 2:54pm a transaction log backup was performed. Followed by more changes to the database.

At 3:01pm another transaction log backup was performed. Followed by more changes to the database.

At 3:12 another transaction log backup was performed. Followed by more changes to the database.

At 3:19 a catastrophic failure occurred causing the operating system running SQL Server to reboot. Let’s blame it on a power surge, or maybe it was just a DBA who accidentally bumped the power cord. I like the power surge idea, so that I don’t get blamed for kicking the power cord.

Initially I restored the full and each t-log one at time, so I could check for corruption and extract the "good" data. This left me with a database current as of the last t-log backup as my most current mdf and an ldf that was a bit older. Then I tried to figure out how to attach the ldf and get the mdf current.

I tried detaching the database and creating it with younger mdf and the older ldf

USE [master]
GO
CREATE DATABASE [CorruptionChallenge3_T3] ON 
( FILENAME = N'E:\SQLServer\Data01\CorruptionChallenge3_T3.mdf' ),
( FILENAME = N'F:\SQLLogs\CorruptionChallenge3_T3_log.LDF' )
 FOR ATTACH
GO
--That did not work, try this
CREATE DATABASE [CorruptionChallenge3_T3]
ON ( FILENAME = N'E:\SQLServer\Data01\CorruptionChallenge3_T3.mdf' )
LOG ON ( FILENAME = N'F:\SQLLogs\CorruptionChallenge3_T3_log.LDF')
GO
--That did not work either


Error

```
Msg 5173, Level 16, State 2, Line 90
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this i

Solution

You should always produce a log backup before restoring a database. Call it a tail log backup if you wish. I call it "the last log backup before a restore".

If you can't start your SQL server, meaning the ldf file is isolated, you hack-attach that ldf file into a running SQL Server and do that tail log backup there. Nothing complicated, really, just remember to do this.

You can't in any way let SQL Server combine database files from different point in times (so to speak). It won't let you. It has the most recent LSN number in the header of each files and if those aren't the same, it is a big no-no.

Here's a blog post on the subject, I wrote a long time ago: http://sqlblog.karaszi.com/restore-database-to-the-point-of-disaster/ .

Context

StackExchange Database Administrators Q#227496, answer score: 4

Revisions (0)

No revisions yet.