Tales of a corrupt SQL log

Posted by guybarrette on ASP.net Weblogs See other posts from ASP.net Weblogs or by guybarrette
Published on Mon, 31 May 2010 20:20:32 GMT Indexed on 2010/05/31 20:23 UTC
Read the original article Hit count: 348

Filed under:

Warning: I’m a simple dev, not an all powerful DBA with godly powers.

This morning, one of my sites was down and DNN reported a problem with the database.  A quick series of tests revealed that the culprit was a corrupted log file.

Easy fix I said, I have daily backups so it’s just a mater of restoring a good copy of the database and log files.  Well, I found out that’s not exactly true.  You see, for this database, I have daily file backups and these are not database backups created by SQL Server.

So I restored a set of files from a couple of days ago, stopped the SQL service, copied the files over the bad ones, restarted the service only to find out that SQL doesn’t like when you do that.  It suspects something fishy and marks the database as suspect.  A database marked as suspect can’t be accessed at all.  So now what?

I searched throughout the tubes of the InterWeb and found that you can restore from a corrupted log file by creating a new database with the same name as the defective one, then copy the restored database file (the one with data) over the newly created one.  Sweet!  But you still end up with SQL marking the database as suspect but at least, the newly created log is OK.  Well not true, it’s not corrupted but the lack of data makes it not OK for SQL so you need to rebuild the log.  How can you do that when SQL blocks any action the database? 

First, you need to change the database status from suspect to emergency.  Then you need to set the database for single access only.  After that, you need to repair the log with DBCC and do the DBA dance.  If you dance long enough, SQL should repair the log file.  Now you need to set the access back to multi user.  Here’s the T-SQL script:

use master
GO
EXEC sp_resetstatus 'MyDatabase'
ALTER DATABASE MyDatabase SET EMERGENCY
Alter database MyDatabase set Single_User
DBCC checkdb('MyDatabase')
ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('MyDatabase', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE MyDatabase SET MULTI_USER

So I guess that I would have been a lot easier to restore a SQL backup.  I can’t really say but the InterWeb seems to say so.  Anyway, lessons learned:

  • Vive la différence: File backups are different then SQL backups.
  • Don’t touch me: SQL doesn’t like when you restore a file over a corrupted one.
  • The more the merrier: You should do both SQL and file backups.
  • WTF?: The InterWeb provides you with dozens of way to deal with the problem but many are SQL 2000 or SQL 2005 only, many are confusing and many are written in strange dialects only DBAs understand.
Bookmark and Share

© ASP.net Weblogs or respective owner

Tales of a corrupt SQL log

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Mon, 31 May 2010 00:00:00 GMT Indexed on 2010/05/31 21:54 UTC
Read the original article Hit count: 348

Filed under:
Warning: Im a simple dev, not an all powerful DBA with godly powers. This morning, one of my sites was down and DNN reported a problem with the database.  A quick series of tests revealed that the culprit was a corrupted log file. Easy fix I said, I have daily backups so its just a mater of restoring a good copy of the database and log files.  Well, I found out thats not exactly true.  You see, for this database, I have daily file backups and these are not database backups created...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

Related posts about .NET