• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

5 Second Rule Doesn’t Apply to Dropped Databases or Does It?

June 22, 2012 11:45 am / 1 Comment / SQLSoldier

5 Second Rule Doesn’t Apply to Dropped Databases or Does It?

5 Second Rule Doesn't Apply to Dropped Databases or Does It?

5 Second Rule Doesn't Apply to Dropped Databases or Does It?


Imagine you get a frantic call from someone, we’ll call him George, who just dropped a busy production database, and if he can’t get it back up ASAP, heads are going to roll (namely, his). The conversation might go something like this:

George: Oh crap, oh crap, oh crap! I’m going to get fired.

Me: What happened?

George: I dropped our busy production database by mistake. I thought I was on the test server.

Me: Do you have current backups?

George: Yes, but it has to be pulled from tape. It’s going to take at least 6 hours to restore from backup. I have to get it back up ASAP. Is there anyway to recover the dropped database?

Me: Maybe. How did you manage to drop a busy database? Dropping a database requires locking the database first.

George: I set it offline with rollback immediate first.

Me: Okay, good. We may be able to salvage this yet.

We had George’s production database back online in less than half an hour without restoring from backup. Intrigued? This is what we did.

Normally, when you drop a database, the database files are deleted as well. However, if the database is offline, the database files are not deleted. So even though the databae had been dropped, the files were still completely in tact in the file system. The one big question in my mind was whether or not the files would be attachable. In order to attach database files to SQL Server, the database has to be cleanly shut down first. Hopefully, setting the database offline shut it down cleanly.

Turns out that the database had been shut down cleanly. We found the database files and attached them to SQL. Nothing was lost (save for the active transactions that were rolled back when George set it offline.

Here some code to demonstrate

Use master;

-- Drop database if it exists
If DB_ID('Testdrop') Is Not Null
    Drop Database TestDrop;

-- Create a new database
Create Database TestDrop
    On (Name = N'TestDrop',
        FileName = N'C:\bak\TestDrop.mdf')
    Log On
        (Name = N'TestDrop_log',
        FileName = N'C:\bak\TestDrop_log.ldf');
Go

-- Add a table and some data
Use TestDrop;

Select *
Into dbo.AllDatabases
From sys.databases;

-- Set offline
Use master;

Alter Database TestDrop Set Offline With Rollback Immediate;
Go

-- Drop database
Drop Database TestDrop;
Go

-- Verify database files still exist
Exec xp_fileexist N'C:\bak\TestDrop.mdf';
Exec xp_fileexist N'C:\bak\TestDrop_log.ldf';
Go

-- Try to attach previously dropped files
Create Database TestDrop
    On (FileName = N'C:\bak\TestDrop.mdf')
    Log On
        (FileName = N'C:\bak\TestDrop_log.ldf')
 For Attach
 
 -- Validate data still exists
Select *
From TestDrop.dbo.AllDatabases;
Go

Disclaimer: Names were changed to protect the guilty.

Posted in: SQL Server / Tagged: Disaster Recovery

One Thought on “5 Second Rule Doesn’t Apply to Dropped Databases or Does It?”

  1. Pingback: Something for the Weekend - SQL Server Links 29/06/12

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this