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
Related
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
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
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 have a file with .bak extension.
How can I import this date to a database in SQL Server?
On SQL Server Management Studio
Right click Databases on left pane (Object Explorer)
Click Restore Database...
Choose Device, click ..., and add your .bak file
Click OK, then OK again
Done.
This will show you a list of database files contained in DB.bak:
RESTORE FILELISTONLY
FROM DISK = 'D:\3.0 Databases\DB.bak'
You will need the logical names from that list for the MOVE operation in the second step:
RESTORE DATABASE YourDB
FROM DISK = 'D:\3.0 Databases\DB.bak'
and you have to move appropriate mdf,ndf & ldf files using
With Move 'primarydatafilename' To 'D:\DB\data.mdf',
Move 'secondarydatafile' To 'D:\DB\data1.ndf',
Move 'logfilename' To 'D:\DB\log.ldf'
You can simply restore these database backup files using native SQL Server methods, or you can use ApexSQL Restore tool to quickly virtually attach the files and access them as fully restored databases.
Disclaimer: I work as a Product Support Engineer at ApexSQL
Instead of choosing Restore Database..., select Restore Files and Filegroups...
Then enter a database name, select your .bak file path as the source, check the restore checkbox, and click Ok. If the .bak file is valid, it will work.
(The SQL Server restore option names are not intuitive for what should a very simple task.)
On Microsoft SQL Server Management Studio 2019:
On Restore Database window:
Choose Device
Choose Add and pick target file
OK to confirm
OK to confirm restore
Connect to a server you want to store your DB
Right-click Database
Click Restore
Choose the Device radio button under the source section
Click Add.
Navigate to the path where your .bak file is stored, select it and click OK
Enter the destination of your DB
Enter the name by which you want to store your DB
Click OK
Done
Although it is much easier to restore database using SSMS as stated in many answers.
You can also restore Database using .bak with SQL server query, for example
RESTORE DATABASE AdventureWorks2012 FROM DISK = 'D:\AdventureWorks2012.BAK'
GO
In above Query you need to keep in mind about .mdf/.ldf file location.
You might get error
System.Data.SqlClient.SqlError: Directory lookup for the file "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\AdventureWorks.MDF" failed with the operating system error 3(The system cannot find the path specified.). (Microsoft.SqlServer.SmoExtended)
So you need to run Query as below
RESTORE FILELISTONLY
FROM DISK = 'D:\AdventureWorks2012.BAK'
Once you will run above Query you will get location of mdf/ldf use it Restore database using query
USE MASTER
GO
RESTORE DATABASE DBASE
FROM DISK = 'D:\AdventureWorks2012.BAK'
WITH
MOVE 'DBASE' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE.MDF',
MOVE 'DBASE_LOG' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DBASE\MSSQL\DATA\DBASE_1.LDF',
NOUNLOAD, REPLACE, NOUNLOAD, STATS = 5
GO
Source:Restore database from .bak file in SQL server (With & without scripts)
Simply use
sp_restoredb 'Your Database Name' ,'Location From you want to restore'
Example: sp_restoredb 'omDB','D:\abc.bak'
You can use node package, if you often need to restore databases in development process.
Install:
npm install -g sql-bak-restore
Usage:
sql-bak-restore <bakPath> <dbName> <oldDbName> <owner>
Arguments:
bakpath, relative or absolute path to file
dbName, to which database to restore (!! database with this name will be deleted if exists !!)
oldDbName, database name (if you don't know, specify something and run, you will see available databases after run.)
owner, userName to make and give him db_owner privileges (password "1")
!! sqlcmd command line utility should be in your PATH variable.
https://github.com/vladimirbuskin/sql-bak-restore/