简体   繁体   中英

How to make a copy of a database in SQL Server

I can't seem to find any SQL that will clone one database in SQL Server within the same server.

Let's say I have a database called MyDB . I simply want to make a copy of MyDB to MyDb2 . I thought that this would work:

BACKUP DATABASE MyDB TO MyDB2;

But I get this error when I try to execute it:

Backup device 'DbTestBack' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.

Does anyone know what the best way to do this is? I want an exact duplicate of the original including security permissions.

A simple way is taking a back up copy of current DB and restoring it. You Can do this in single step with a simple script

backup database MyDB 
to disk='D:\MyDB.bak';


restore database MyDB2
from disk='D:\MyDB.bak'
WITH move 'MyDB_Data' to  'D:\MyDB2_Data.mdf',
move 'MyDB_log' to  'D:\MyDB2_Data.ldf';
GO

Note : I made an assumption on your current data file and log file name ( MyDB_Data , MyDB_log ), you need to check them and make correct

DBAtools is your friend here.

Use Copy-DbaDatabase

ie.

Copy-DbaDatabase -Source SRV1 -Destination SRV1 -Database myDB -BackupRestore -SharedPath \\<<your temporary server location such as c:\temp>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM