How to Backup and Restore a Database as a Copy on the Same Server

How to create copy of database using backup and restore

When you restore a database from a backup it will use the same file names as the original database.
You need to change these file names during the restore.

On the restore window go to the Files tab. On this window you have a column called Restore As.
Change the file names at the end of the path in the column Restore As for each of the files you see.

How do you backup and restore a database as a copy on the same server?

RESTORE FILELISTONLY is an informational command and is not required to perform a restore. A user can use this to figure out what the logical names are for the data files, that can be used with the MOVE commands to restore the database to a new location.

As suggested by the error message you need to use RESTORE FILELISTONLY to see what the logical names for the database are. Your restore command has these wrong.

Here is a working example of what you need to do:

--backup the database
backup database test1 to disk='c:\test1_full.bak'

-- use the filelistonly command to work out what the logical names
-- are to use in the MOVE commands. the logical name needs to
-- stay the same, the physical name can change
restore filelistonly from disk='c:\test1_full.bak'
--------------------------------------------------
| LogicalName | PhysicalName |
--------------------------------------------------
| test1 | C:\mssql\data\test1.mdf |
| test1_log | C:\mssql\data\test1_log.ldf |
-------------------------------------------------

restore database test2 from disk='c:\test1_full.bak'
with move 'test1' to 'C:\mssql\data\test2.mdf',
move 'test1_log' to 'C:\mssql\data\test2.ldf'

Creating new database from a backup of another Database on the same server?

What I should to do:

  • Click on 'Restore Database ...' float menu that appears right clicking the "Databases" node on SQL Server Management Studio.
  • Fill wizard with the database to restore and the new name.
  • Important If database still exists change the "Restore As" file names in the "Files" tab to avoid "files already in use, cannot overwrite" error message.

What I do

IDk why I prefer to do this:

  • I create a blank target database with my favorite params.
  • Then, in "SQL Server Management Studio" restore wizard, I look for the option to overwrite target database. It is in the 'Options' tab and is called 'Overwrite the existing database (WITH REPLACE)'. Check it.
  • Remember to select target files in 'Files' page.

You can change 'tabs' at left side of the wizard (General, Files, Options)

What is the best way to copy a database?

Backup and Restore is the most straight-forward way I know. You have to be careful between servers as security credentials don't come with the restored database.

How to restore to a different database in SQL Server?

You can create a new db then use the "Restore Wizard" enabling the Overwrite option or:

View the contents of the backup file:

RESTORE FILELISTONLY FROM DISK='c:\your.bak'

note the logical names of the .mdf & .ldf from the results, then:

RESTORE DATABASE MyTempCopy FROM DISK='c:\your.bak'
WITH
MOVE 'LogicalNameForTheMDF' TO 'c:\MyTempCopy.mdf',
MOVE 'LogicalNameForTheLDF' TO 'c:\MyTempCopy_log.ldf'

This will create the database MyTempCopy with the contents of your.bak.

(Don't create the MyTempCopy, it's created during the restore)


Example (restores a backup of a db called 'creditline' to 'MyTempCopy'):

RESTORE FILELISTONLY FROM DISK='e:\mssql\backup\creditline.bak'

>LogicalName
>--------------
>CreditLine
>CreditLine_log

RESTORE DATABASE MyTempCopy FROM DISK='e:\mssql\backup\creditline.bak'
WITH
MOVE 'CreditLine' TO 'e:\mssql\MyTempCopy.mdf',
MOVE 'CreditLine_log' TO 'e:\mssql\MyTempCopy_log.ldf'

>RESTORE DATABASE successfully processed 186 pages in 0.010 seconds (144.970 MB/sec).


Related Topics



Leave a reply



Submit