Create Duplicate SQL Database for Testing - sql-server

I have created a database on SQL server and a front end user application in winforms c#.
It's up and running and working fine, but I've now been asked to set up a test version by the client for training new employees, so they can put in dummy data whilst they are trained without effecting the 'real' live database.
When I installed the database I had it all scripted, but things have changed since, and I don't particularly want to script all the tables again.
Is there a fast, convenient way of duplicating the database (and its data) on the same server, but under a different name?
EDIT (subsequent to Tony Hopkinson post)
I've got as far as this
exec sp_addumpdevice 'Disk','MyDatabaseBackup',N'D:\MyDatabaseBackup'
Restore Database MyDatabase From MyDatabaseBackup With File = 1,
Move 'MyDatabase' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST.mdf',
Move 'MyDatabase_Log' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST_log.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [MyDatabaseTEST] FROM [MyDatabaseBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice MyDatabaseBackup
But I'm getting the following error message
Msg 3234, Level 16, State 2, Line 2
Logical file 'MyDatabase' is not part of database 'MyDatabase'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
Msg 3154, Level 16, State 4, Line 6
The backup set holds a backup of a database other than the existing 'MyDatabaseTEST' database.
Msg 3013, Level 16, State 1, Line 6
RESTORE LOG is terminating abnormally.
Device dropped.

use copy database option in SQL server management studio

I found this method to be most effective on SQL Server 2005 and 2008, Express Editions:
From the Microsoft Docs:
Right click on the database you want to duplicate and choose Tasks->"Back Up..."
Save the back up to a .bak file
Right click on the "Databases" folder in the Object Explorer in SQL Server Management Studio
Choose "Restore Database"
As the source, select "File" and point to the .bak file you created earlier.
Change the name of the database to restore to (this was the key step for me - you are not constrained to the options in the dropdown.)
SSMS will restore your .bak file to a new database, according to the name that you give it.

First do a full backup your current database, which of course you have :)
The you restore it to another one
e.g. something like
exec sp_addumpdevice 'Disk','LiveDataBackup',N'Insert backup file name here including path'
Restore Database TestData From LiveDataBackup With File = 1,
Move 'LiveData' To N'Path to where sqlserver expects the mdfs to be\TestData.mdf',
Move 'LiveData_Log' To N'Path to where sqlserver expects the ldf to be\TaxData1.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [TestData] FROM [LiveDataBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice LiveDataBackup
Above assume your live database is cunningly named LiveData and test, TestData.
The path to where the mdf and ldf will be depends on the version of sql server and the instance name
It should be something like
C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\
MSSQL11 because it's sql 2012, and DENALI is my instance name and it was installed by default in C: \Program Files
Also there's no with replace, so if you wanted to run it again, you'd need to Drop your test database.
There's probably some way to do this from the GUI, but I found it a massive PIA trying to relate the UI, to what I wanted to do.

using MS SQLServer 2012, you need to perform 3 basic steps
first, generate .sql file containing only the structure of the source DB
=> right click on the source DB and then Tasks then Generate Scripts
=> follow the wizard and u can save the .sql file locally
Second, replace in .sql file the source db with the destination one
=> right click on the destination file, open the .sql file and press New Query and Ctrl-H or (edit - find and replace - Quack replace)
finally, populate with data
=> right click on the detination DB, then Tasks and then Import Data
=> Data source drop dow set to ".net framework data procider for sql server" + set connection string text field under DATA ex: Data Source=Mehdi\SQLEXPRESS;Initial Catalog=db_test;User ID=sa;Password=sqlrpwrd15
=> Same thing to do with the destination
=> check the table you want to transfer or check box besides "source :....." to check all of them
you are done.

You want to copy one Database_Production to Database_Testing in the same server. I would take database_production database as an example. I tested it in my server successfully.
Firstly, backup the database Database_Production.
BACKUP DATABASE Database_Production TO DISK ='H:\test\Database_Production.bark';
Secondly, restore Database_Production and this could rename the database name to Database_Testing.
RESTORE DATABASE Database_Testing
FROM DISK='H:\test\Database_Production.bark'
WITH
MOVE 'Database_Production_Data' TO 'H:\test\Database_Testing_Data.mdf',
MOVE 'Database_Production_log' to 'H:\test\Database_Testing_Data.ldf';
GO
Then the database Database_Production is copied to database Database_Testing. The MOVE statement causes the data and log file to be restored to the specified locations. You do not need to create database Database_Testing and the script would create it.

Dump your database into a backup file
Re-create your database from your dump - that is a script which you can run - with different name (that you have to change into the script)
You can follow (this)

If you need to create a database on the same server just create the empty database. Right Click on it and Select Restore-> Choose the database you want to make a copy of and click okay.

Related

Import database backup (.bak file) into another database

I'm using microsoft SQL server. I have a database that we can call database1 and its backup in .bak format.
I need to restore the backup to another, blank database that we can call database_temp, so both database must exist. I need to get and confirm some data from the database_temp archive and then delete it when I'll end my job.
what I did:
1) created new database "database_blank"
2) tasks - restore database. In source I choose "device" and then my .bak file, in destination I select database_temp.
3) in option I choose Overwrite the existing database with replace.
I got the error "Restore of database failed the file cannot be overwritten it is being used by database1 database"
You have to move the database files.
e.g.
RESTORE DATABASE [AdventureWorks] FROM DISK = 'c:\backup\Adv.bak'
WITH CHECKSUM,
MOVE 'AdventureWorks_Data' TO 'c:\mssql\data\AdventureWorksCopy_Data.mdf',
MOVE 'AdventureWorks_Log' TO 'c:\mssql\log\AdventureWorksCopy_Log.ldf',
RECOVERY, REPLACE, STATS = 10;
The first part of the move is the logical file name of the database file. Right click on the original database - Properties - Files to get the right names.
The full documented syntax can be found here

Database restore is missing tables

I am using SSMS 2017 and attempting to restore a SQL Server 2016 backup (.bak) file to a new database (the data file and log file names in the original backup are different from the new database I am restoring to), so for example, db1.bak is being used to create a new database called db2 through a restore operation.
After the restore completes, all stored procedures and views are present, but none of the tables are there.
These are the steps I've taken in SSMS to perform the restore:
Right click the Databases folder
Select "Restore Database"
Under the General Page: For "Source" I choose Device and select the db1.bak file, and for "Destination" I enter the name of the new database to be created from the backup (db2)
Under the Files Page I select Relocate all files to folder (both Data and Log folder paths have been pre-populated with the correct names) as have the Logical File Name and the Restore As paths.
Under the Options Page I either select nothing or select Overwrite the existing database (WITH REPLACE).
Once the restore completes "successfully" I expand the newly created db2 and click to expand the tables and receive this error message:
What am I doing wrong?
Here is the script:
USE [master]
RESTORE DATABASE [PdsClone] FROM DISK = N'F:\SQLBackups\PDS_backup_2017_12_12_001015_0199267.bak' WITH FILE = 1, MOVE N'PDS_Data' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone.mdf', MOVE N'PDS_Log' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone_log.ldf', NOUNLOAD, REPLACE, STATS = 5 GO
UPDATE
I had the server group add another disk to that server and moved the SQL backups there, and I was able to successfully complete the restore this morning. I had previously cleaned off most of the backup files, but could it have been a space issue that prevented the tables from showing up in the restore? Every try it indicated success but no tables. They are there now, and I am thankful for your suggestions and help!
Not really an answer to what happened, but perhaps SSMS was doing something a little buggy as #JeroenMostert suggested.

restoring database sql server

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.

restore .bak mssql 2005 to a new a server of mssql 2008r2

I created a .bak file (backup) from server A with the with following specs: Windows server 2003, MSSQL 2005. Now I would like to restore this backup on a new Server B with the following specs: Windows 8, MSSQL 2008 R2. I did the following to try and do the restore
Copy files to the new server(Server B)
Clicked on Microsoft SQL Server Management Studio 2008R2(Server B)
Right click on Databases to create a new Database called Boom (Server B)
After creating the new Database(Boom), right clicked on Tasks->Restore->Database and
On the source for restore area, Clicked From device and located the .bak file, select it and cliked ok.
Instead of getting the success message, I get the following error:
Restore failed for Server 'Server B'.(Microsft.SqlServer.SmoExtended)
Additional information: System.Data.SqlError: The backup set holds a backup of a database other than the existing 'Boom' database(Microsoft.SqlServer.Smo).
Please assist, Im am new to MSSQL
Right click on Databases to create a new Database called Boom (Server B)
After creating the new Database(Boom), right clicked on Tasks->Restore->Database and
Well now you're making a new database and trying to overwrite it with a different database's backup. Hence:
The backup set holds a backup of a database other than the existing 'Boom' database
There's a WITH REPLACE option that allows you to proceed, but just avoid the indirection to begin with: restore the database, don't make a new one.
You'll probably need to delete the redundant Boom database you made, first. If, for whatever reason, you couldn't delete the database you'd have to use WITH REPLACE.
"Restore failed for Server 'Server B'.(Microsft.SqlServer.SmoExtended) Additional information: System.Data.SqlError: The backup set holds a backup of a database other than the existing 'Boom' database(Microsoft.SqlServer.Smo)."
I encountered this error when the logical name of the files do not match. Check on the logical name of the database you backed up and you will need the same logical name for the new database you created.
Or you can surely use the With Replace option too as specified by ta

Import .bak file to a database in SQL server

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/

Resources