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
Related
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.
I am facing an issue while restoring the databases from differential backups. Here are the steps I performed
DROP DATABASE DBName_delta
GO
BACKUP DATABASE DBName TO DISK = 'E:\Fullbak1.bak'
GO
RESTORE FILELISTONLY FROM DISK = 'E:\Fullbak.bak'
GO
RESTORE DATABASE DBName_delta
FROM DISK='E:\Fullbak.bak'
WITH MOVE 'DBName_Data' TO 'E:\DBData\DBName_delta.mdf',
MOVE 'DBName_Image_Data' TO 'E:\DBData\DBName_delta_Image_Data.mdf',
MOVE 'DBName_Log' TO 'D:\DBLog\DBName_delta.ldf',
NORECOVERY
--Made Some changes in the database
BACKUP DATABASE DBName
TO DISK = 'E:\DiffBak1.TRN'
WITH DIFFERENTIAL
GO
--Made Some more changes in the database
BACKUP DATABASE DBName
TO DISK = 'E:\DiffBak2.TRN'
WITH DIFFERENTIAL
GO
RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak1.TRN'
GO
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
GO
Msg 4305, Level 16, State 1, Line 2
The log in this backup set begins at LSN 81125000000059600297, which is too recent to apply to the database. An earlier log backup that includes LSN 81121000000116200001 can be restored.
Msg 3013, Level 16, State 1, Line 2
RESTORE LOG is terminating abnormally.
RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak2.TRN'
GO
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak2.TRN' WITH STANDBY = 'c:\undo.ldf'
GO
But when I tried to restore the same E:\DiffBak1.TRN through SSMS by using the WITH NORECOVERY option, it restored the database and again I was able to perform the restore of the same file using the Transact SQL. Am I missing something here? Is this something to do with the RESTORE DATABASE? I am sure that we are not missing any of the logs in between. Any help will be much appreciated.
I am able to figure this out. As I mentioned, I was able to restore the differential database through the SSMS wizard, I took the script from the wizard and found out the differences in the query.
I was using the below code
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
GO
I have changed the code to
RESTORE DATABASE [DBrel02t_delta] FROM DISK = N'E:\DiffBak1.TRN' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
GO
This resolved the issue. Use the below query to find out the lsn details of your backup.
RESTORE HEADERONLY FROM DISK = Nā<backup file>ā;
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 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.
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?