This question was migrated from Stack Overflow because it can be answered on Database Administrators Stack Exchange.
Migrated 22 days ago.
I tried to run this SQL statement:
RESTORE DATABASE test1
FROM DISK = N'D:\Projects\Billing DB\Test1.bak'
WITH REPLACE;
but all I get are these errors:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf" failed with the operating system error 3(The system cannot find the path specified.).
Msg 3156, Level 16, State 3, Line 1
File 'ChickenDB' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf" failed with the operating system error 3(The system cannot find the path specified.).
Msg 3156, Level 16, State 3, Line 1
File 'ChickenDB_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Like the error says you need to use WITH MOVE to specify valid file locations for the database files. By default the file locations on the source system are used.
An easy way to do this is to restore the database with the restore wizard in SSMS. It will write the SQL for you, and even execute it if you want.
A SQL Server backup file contains information on where the database files were located when the (full) backup (set) was created.
In your case the backup file contains the following information:
Logical Name | Physical Name
---------------+--------------------------------------------------------------------------------
ChickenDB | C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf
ChickenDB_log | C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf
When restoring a database with the RESTORE DATABASE .... command, then the restore process will read the backup file and extract this information and use it as a default value for the restore process.
If you want to determine the file locations stored in the backup file then use the RESTORE FILIELISTONLY... command.
Returns a result set containing a list of the database and log files contained in the backup set in SQL Server.
You can use the RESTORE FILIELISTONLY... command like this:
RESTORE FILELISTONLY FROM DISK = `D:\Projects\Billing DB\Test1.bak`;
In order to restore the database to a new location you will have to use the MOVE parameter of the RESTORE DATABASE .... command.
MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' [ ...n ]
Specifies that the data or log file whose logical name is specified by logical_file_name_in_backup should be moved by restoring it to the location specified by operating_system_file_name. The logical file name of a data or log file in a backup set matches its logical name in the database when the backup set was created.
Solution
For your backup you would probably be best of with something like this:
RESTORE DATABASE test1
FROM DISK = N'D:\Projects\Billing DB\Test1.bak'
WITH
MOVE 'ChickenDB' TO 'D:\SomeDirectory\Test1.mdf',
MOVE 'ChickenDB_log' TO 'D:\SomeDirectory\Test1.ldf',
REPLACE,
RECOVERY;
Example code can also be found in the online documentation for the RESTORE DATABASE .... command in the example D. Restore a database and move files.
Related
I want to attach a DB to SQL Server. I have the mdf files for it but those are in read-only state. Also the LDF file is not present. I use the following command to attach the DB.
Create database TrackData on (Filename = N'F:\Data\Bill_Data.mdf') FOR ATTACH_REBUILD_LOG
but i get the following error.
Msg 3415, Level 16, State 3, Line 1
Database 'Bill_Data' cannot be upgraded because it is read-only or has read-only files. Make the database or files writeable, and rerun recovery.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'Bill_Data'. CREATE DATABASE is aborted.
I know that it might not be possible to recover the Database in this case as MDF file is in read-only state. Also I do not have a backup of database. But still expecting any wild ideas that could help.
Paste your .mdf file and ldf file in this directory - C:\Program Files\Microsoft SQL Server\MSSQL11.SS2012\MSSQL\DATA
and make sure that the folder in which your .mdf file resides is not read only.
please see this and this.
I had a an instance of SQL Server 2014 with filesteam on. Recently the machine was infected with a virus that basically decrypted all the files that were possible to modify, so SQL Server was broken. The administrator of the machine gave us a backup in form of .mdf and .ldf files of our database, no .bak file, he was basically just saving what was on disk instead of doing SQL Server backups. Anyways, I have installed a new instance of SQL Server 2014 on that machine (witch filestream on) and I'm trying to attach the recovered .mdf file. If I use this script:
CREATE DATABASE [ABC] ON PRIMARY
( NAME = N'ABC', FILENAME = N'D:\SQL Server 2014 2\MSSQL12.SQLEXPRESS2\MSSQL\DATA\ABC.mdf'),
( NAME = N'ABC_log', FILENAME = N'D:\SQL Server 2014 2\MSSQL12.SQLEXPRESS2\MSSQL\DATA\ABC_log.ldf')
FOR ATTACH
I get the following error message:
Msg 3634, Level 16, State 1, Line 5
The operating system returned the error '2(The system cannot find the file specified.)' while attempting 'FsFileHeader::Open' on 'D:\Filestream\ABC.FS'.
Msg 5105, Level 16, State 14, Line 5
A file activation error occurred. The physical file name 'D:\Filestream\ABC.FS' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 1813, Level 16, State 2, Line 5
Could not open new database 'ABC'. CREATE DATABASE is aborted.
So it basically tries to refer to a location of a filestream that is broken (the data there was decrypted by the virus). However, the ABC.FS folder exists, so I don't know why such a message is shown. Maybe it refers to some filestream files that this folder contains? If I delete everything and leave this folder empty, the same error is thrown.
I have also tried another script, where I try to create a new Filestream directory (the rest of the script is same as above):
FILEGROUP [ABC.FS] CONTAINS FILESTREAM DEFAULT
( NAME = N'ABC', FILENAME = N'd:\Filestream2\ABC.FS' )
LOG ON
This gives me a following error:
Unable to open the physical file "d:\Filestream2\ABC.FS". Operating system error 5: "5(Access is denied.)".
I think I have added every possible user, including the user on which SQL Server runs, to have full control over this folder, but it would not help.
I will also point out that I don't want to restore any data the filestream containded, this is pretty much gone. I just want to be able to attach this database from the .mdf file.
I need to create a script that when I execute it. It will backup a production database (keep in mind this is a live database) and restore it as development database(if exists overwrite)
I have tried this query but getting error message. I need to find a way to do this without taking the database offline or single user mode
USE [master]
RESTORE DATABASE [Development] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\Production.bak'
WITH FILE = 6, MOVE N'Producation' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development.mdf',
MOVE N'Production_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development_log.ldf', NOUNLOAD, REPLACE, STATS = 5
Msg 3101, Level 16, State 1, Line 67
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 67
RESTORE DATABASE is terminating abnormally.
I realized I do not need to keep the development online and can kill connections.
so I have used the ALTER DATABASE [ALI] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO to resolve this error
Hello I have database on which user work.
I want to create copy of this database for testing purpose.
Can anyone tell how can this be done?
I tried everything from copy a database within SQL Server Express?
But Copying files and attaching it destroys first database and trying to restore backup into new database throws error that database is not the same.
Can anyone suggest me solution?
as #Nadeem_MK suggested I created backup and now I'm trying to restore with this script:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\ExistingDb.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.ldf' --logical name and physical name
and this throws error:
Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf' cannot be overwritten. It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf' cannot be overwritten. It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
As you can see restores is trying to alter files from backup-ed database instead of new database
#Update:
BackUp Script:
--To backup Database (Delete the previous backup first)
BACKUP DATABASE Equipment --Database Name
TO DISK = 'C:\Backup\Equipment.bak' WITH INIT --Path of backup location
file locations:
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test_log.mdf
Restore function:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\Equipment.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name
You can create a backup and restore it using the following script;
--To backup Database (Delete the previous backup first)
BACKUP DATABASE ExistingDb --Database Name
TO DISK = 'C:\DBBackup\ExistingDb.bak' WITH INIT --Path of backup location
Then restore it;
RESTORE DATABASE NewDbName -- use your new db name
FROM DISK = N'C:\DBBackup\ExistingDb.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Reporting' TO N'C:\Databases\MDF\NewDbName.mdf', --logical name and physical name
MOVE N'Reporting_log' TO N'C:\Databases\LDF\NewDbName.ldf' --logical name and physical name
Assuming this are the paths of the original database:
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
and the filename of your backup is called
Equipment.bak
Make sure that the filenames for the new database aren't the same as the old one. That is what the errors you are getting means:
The file 'C:\...' cannot be overwritten. It is being used by database 'Equipment'.
Explanation: Don't use filenames of files you already have!
Instead, give these a new name. Like this:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\Equipment.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name
This is basically Nadeem_MK's answer, but instead of your original names Equipment.mdf and Equipment_log.ldf, now you should have Equipment2.mdf and Equipment_log.ldf
If this works, please also opvote Nadeem_MK's answer, as this is basically what he was saying. I just don't have enough reputation yet to comment on his answer.
Simple, Do these steps:
Take backup of your primary database.
Create new database with whatever name you want to give it for testing.
Restore backup of your primary database to your newly created testing database.
Done!!
Note: The functionality below is not available in SQL Express
I have left the answer here for completeness for future readers only.
The simplest method I can think of is to let Management Studio do all the work!
Right-click the database you wish to copy and look for the Copy Database... option under the Tasks menu.
Why don't you script the database and run the script?
Any idea why my Restore command works fine when run in Management Studio 2008 but not when run from the dos prompt?
Shown below is the error when running from the dos prompt.
C:\>SQLCMD -s local\SQL2008 -d master -Q "RESTORE DATABASE [Sample.Db] FROM DISK = N'C:\Sample.Db.bak' WITH FILE = 1, MOVE N'Sample.Db' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf', MOVE N'Sample.Db_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf', NOUNLOAD, REPLACE, STATS = 10"
Msg 3634, Level 16, State 1, Server GAUTAM, Line 1
The operating system returned the error '32(The process cannot access the file because it is being used by another process.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf'.
Msg 3156, Level 16, State 8, Server GAUTAM, Line 1
File 'Sample.Db' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 3634, Level 16, State 1, Server GAUTAM, Line 1
The operating system returned the error '32(The process cannot access the file because it is being used by another process.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf'.
Msg 3156, Level 16, State 8, Server GAUTAM, Line 1
File 'Sample.Db_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Server GAUTAM, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Server GAUTAM, Line 1
RESTORE DATABASE is terminating abnormally.
However if I execute this directly in Management Studio 2008, it works fine:
RESTORE DATABASE [Sample.Db] FROM DISK = N'C:\Sample.Db.bak' WITH FILE = 1, MOVE N'Sample.Db' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf', MOVE N'Sample.Db_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf', NOUNLOAD, REPLACE, STATS = 10
There is no lock or security issues, the data base doesn't exist on the server.
I can't figure it out.
Any ideas?
I found my problem. I was using the command line which is wrong:
SQLCMD -s local\SQL2008 ...
It was the -s. This is not the switch for server name but rather the switch for colseparator
The correct way would be (note the capital S as well as localhost):
SQLCMD -S localhost\SQL2008 ...
Problem solved guys, thanks for taking the time to look. :)
Can you try creating a test database, backing it up, deleting it, and restoring it in a SQLCMD session. i.e.
SQLCMD
Create database junk
go
Backup database junk to disk='c:\junk.bak'
go
Drop database junk
go
Restore database junk from disk='c:\junk.bak'....
If that works, what's different? Is the other database backup large? Do you have anti-virus software that could be scanning it? If in doubt, run Sysinternals Filemon while running the SQLCMD restore.
SQLAcid: This seemed to work:
C:\>SQLCMD -s local\SQL2008 -d master -Q "CREATE DATABASE [junk];"
C:\>SQLCMD -s local\SQL2008 -d master -Q "BACKUP DATABASE [junk] TO DISK='C:\junk.bak'"
Processed 152 pages for database 'junk', file 'junk' on file 1.
Processed 2 pages for database 'junk', file 'junk_log' on file 1.
BACKUP DATABASE successfully processed 154 pages in 0.182 seconds (6.892 MB/sec).
C:\>SQLCMD -s local\SQL2008 -d master -Q "RESTORE DATABASE [junk] FROM DISK='C:\junk.bak' WITH FILE = 1, MOVE N'junk' TO N'C:\Pr
ogram Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\junk.mdf', MOVE N'junk_log' TO N'C:\Program Files\Microsoft SQL Serve
r\MSSQL10.SQL2008\MSSQL\DATA\junk_log.ldf', NOUNLOAD, REPLACE, STATS = 100"
100 percent processed.
Processed 152 pages for database 'junk', file 'junk' on file 1.
Processed 2 pages for database 'junk', file 'junk_log' on file 1.
RESTORE DATABASE successfully processed 154 pages in 0.070 seconds (17.920 MB/sec).
Mind you my database was originally from SQL2005 but I don't think that should be an issue. Something has to be up.
Shot in the dark, but are you running the CMD prompt as Administrator? If not, can you try?