Question

I'm trying to get a copy of an MDF file but coming across the standard "file is in use" message which I believe is the SQL service holding a lock on it. Is it possible to make a copy of the mdf file without having to stop the service or in any way affect users/applications?

If not, is it possible to create a once-off full backup mdf to a different location than the existing one so that it wouldn't be locked by SQL service?

(Variations on this have been asked on this site already, but I don't think this is a duplicate. I am not trying to relocate the database, simply attempting to take a copy of the mdf - without interrupting Live operations - so I can place it on my dev machine at home and play around with the database)

Was it helpful?

Solution

this will let u make a backup :

BACKUP DATABASE MyDatabase TO DISK = 'W:\DBs\MyDatabase.bak' WITH INIT;

and if u want to backup the log file as well then just add this command before the previous one :

ALTER DATABASE MyDatabase SET RECOVERY FULL;

Then if you want to recover the database from the backup file try this command:

USE [master]
ALTER DATABASE MyDatabase 
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
-- The previous command will used to close all connections to the database
-- until you recover it

Restore Database MyDatabase From Disk = 'W:\DBs\MyDatabase.bak' WITH REPLACE;

ALTER DATABASE MyDatabase SET MULTI_USER
-- This will reopen the database for users connections
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top