RESTORE DATABASE Tes
FROM DISK = '{7522204E-0256-47B3-9864-137D6D1FD449}6'
WITH MOVE 'Test' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.mdf',
MOVE 'Test_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.ldf'
The above restore command is working fine for new dbs, But few old databases it showing a errors like
The file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf' cannot be overwritten. It is being used by database 'windb'.
Msg 3156, Level 16, State 4, Line 1
File 'windb' cannot be restored to 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf'. Use WITH MOVE to identify a valid location for the file
I found that only dbs having more than one ndf files having this issues ..
Any solutions this issue ?
Try to restore the database from the Restore Database Wizard. For that, in the Object Explorer, right-click on Databases -> select Restore Databases.... In the window that appears, set the necessary details for the Source and the Destination. And in the left pane, either select Options or Files, depending on the SSMS version you have.
In the table that appears, find the column Restore As. In each row, give a new file name, instead of the name that shows. For example, in your case it might be showing the file names as something similar to <Folder Path>\DATA\Tes.mdf and <Folder Path>\DATA\Tes.ldf. Give a different name other than Tes. (A name that does not already exist in the DATA folder).
Note: The file names do not have anything to do with the database name, but it will easy referencing later on to give a meaningful name, in case you have to backup or look for the files.
If you are trying to move your database to a new location, you should better detach the source database or take it offline first
I faced this issue when working on an MS SQL Server 2012.
I was trying to restore a database using the script below
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db.dev
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.mydb.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
ALTER DATABASE com.mydb.dev SET MULTI_USER;
GO
And then this was throwing the error below when I tried executing the restore task:
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf' cannot be overwritten. It is being used by database restdb
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf' cannot be overwritten. It is being used by database restdb
Here's how I fixed it:
The issue was that the database backup file that I was using for this database restore operation had been previously used to restore data to a different database on the same server.
All I had to do was to change the Physical names of the Logical files to that of the database that I was to restore to. That is from com.mydb.dev.mdf to my_db.mdf and from com.mydb.dev_log.ldf to my_db_log.ldf:
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.my_db.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db_log.ldf'
GO
ALTER DATABASE my_db SET MULTI_USER;
GO
And this time, the database restore task ran successfully.
That's all.
I hope this helps
Related
I have created a backup of database A1234 as file A1234.bak.
I want to restore this A1234.bak in 2 different databases say D1 and D2 in same sql server instance.
I want same data in both the databases.
I've tried
RESTORE DATABASE D1
FROM disk='C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\A1234.bak'
This works for D1. When I try for D2 It gives error that A1234 is already created and cannot be overwritten.
when I see in C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\ location, there is database A1234.mdf and A1234_log.ldf files and no D1.
while in SSMS it shows D1 and no A1234. So I guess the database A1234 is just pointed to D1.
How can I just restore the schema and data in any database I want using .bak file.
I've seen some WITH MOVE commands but can't understand it
Edit : Also Tried like this
RESTORE DATABASE D1 FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\A1234.bak'
WITH MOVE 'A1234' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\D1.mdf',
MOVE 'A1234_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\D1_log.ldf',
GO
Compared to SSMS, Datagrip doesn't really seem to have the "Restore Database" option, so apparently the only way is through script.
I tried the following:
RESTORE FILELISTONLY
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
GO
RESTORE DATABASE Northwind
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH
MOVE 'Northwind' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind.mdf',
MOVE 'Northwind_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind_log.ldf',
GO
[2019-02-22 15:29:41] completed in 1 s 117 ms
The console shows that the task has been completed with no problems, however the database does not appear on the side explorer, I also cannot select any tables or do anything, I can only drop the database.
What could be the reason for this?
Just try,
USE [master]
RESTORE DATABASE Northwind
FROM DISK = N'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
GO
Right click on the hostname / datasource name in the explorer panel
Go to database tools
you'll find an option called manage shown schemas , just tick the
mark next to databases you want to see
I have a probleme while restoring my database from a .bak file. If I use sql query line I'll find the database but with no table in it, and if I use restoring wizard with the same file and the default configuration i'll get my database and my tables. I think that I'm missing something in this query:
first query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
second query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
WITH REPLACE,
MOVE 'ColisageTest' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.COLISAGE\MSSQL\DATA\ColisageTest.mdf',
MOVE 'ColisageTest_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.COLISAGE\MSSQL\DATA\ColisageTest_log.ldf'
GO
third query
RESTORE DATABASE ColisageTest
FROM DISK = 'e:\colisageTest.bak'
with RECOVERY;
GO
No one of them is working all of them it just restore my database but no tables
Are you sure the database is not empty on tables?
One way to test this is to make another .bak file (that you know have tables) and try to restore it for validation.
No problem with empty tables I'm saying you might have an empty database, ie there is no table there at all.
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?
I need to make clean database template backup and then generate a new one database with another name, but with backuped content.
Backup script is:
sqlcmd -S %DB_HOST% -Q "BACKUP DATABASE %DB_NAME% TO DISK = '%BACKUP_FILE%'"
Restore script is:
sqlcmd -S %DB_HOST% -Q "RESTORE DATABASE %DB_NAME% FROM DISK = '%BACKUP_FILE%' WITH REPLACE"
And it works on the same database name.
But if i backup DB 'ORIGINAL_DB_NAME' and then restore it to 'NEW_DB_NAME' it will raise exception:
Msg 1834, Level 16, State 1, Server <HOST>, Line 1
The file '.....\MSSQL\Data\ORIGINAL_DB_NAME.mdf' cannot be overwritten. It is being used by database 'ORIGINAL_DB_NAME'.
....
You need to specify a new location for the data/log files.
See this MSDN article, Example D
RESTORE DATABASE AdventureWorks2012
FROM AdventureWorksBackups
WITH NORECOVERY,
MOVE 'AdventureWorks2012_Data' TO
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'AdventureWorks2012_Log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH RECOVERY;
You can also avoid that conundrum using SSMS. Under the Options tab, after you have selected a source for your restore direct the data and log files to either the files you want to overwrite, or point them to the directory you need and name them appropriately.
If you go to Files on the Restore Configuration, make sure that the Original File Name is not the same as Restore As, that should solve your issue.