2009-01-14 185 views
16

我有我的開發數據庫,​​我想分離一個副本進行測試。我想經常這樣做。SQL 2005 - 快速複製數據庫+數據的快速方法

經常這樣做的最快和最簡單的方法是什麼?我可以通過生成包含數據的腳本來完成工作,但我覺得必須有更好的方法。我應該備份數據庫1,然後擰緊它,以便我可以恢復它作爲一個不同的名稱?

回答

14

分離數據庫,複製.MDF文件(和.ldf文件),您可以同時附加兩個文件。或者只有一個,以保留一個作爲快速複製的副本。

Mcirosoft SQL Server Management Studio中的「複製數據庫」命令也可以像這樣工作。

您可以使用PowerShell或調用osql的.Cmd文件輕鬆自動化/編寫腳本。

+1

嗯,這是快速 – 2012-10-21 05:11:01

2

而且您還可以使用SQL Management Studio用戶界面來創建文件備份,然後將其還原到同一個或任何其他服務器上的新數據庫中。

+0

只要記住看哪裏恢復操作是試圖把mdf和ldf文件(注意它試圖覆蓋你不想覆蓋的文件)... – 2009-01-14 16:59:31

+0

當我嘗試恢復到不同的數據庫,我得到一個錯誤:「備份集保留了現有'd'以外的數據庫的備份bname'database「 – Kip 2010-02-24 16:59:55

2

在SQL 2005,我會說,最快的方法就是使用複製數據庫,在數據庫 - >任務

源和目標可以是相同的,並且它可以讓你如果需要重命名數據庫。

0

我會使用腳本,因爲這有助於您正在做的事情的透明度。
(特別是如果你想經常這樣做,因爲變化是每個人都會有稍微不同的數據。)

但是,如果你不想去創建腳本的麻煩,最簡單的方法是備份,然後恢復到不同的名稱。

0

如果您想編寫腳本,可以使用以下命令。這也是最好的,如果你不能在數據庫脫機:

USE master 
GO 

-- the original database (use 'SET @DB = NULL' to disable backup) 
DECLARE @DB varchar(200) 
SET @DB = 'PcTopp' 

    -- the backup filename 
DECLARE @BackupFile varchar(2000) 
SET @BackupFile = 'c:\pctopp\sqlserver\backup.dat' 

-- the new database name 
DECLARE @TestDB varchar(200) 
SET @TestDB = 'TestDB' 

-- the new database files without .mdf/.ldf 
DECLARE @RestoreFile varchar(2000) 
SET @RestoreFile = 'c:\pctopp\sqlserver\backup' 


-- **************************************************************** 
--     no change below this line 
-- **************************************************************** 


DECLARE @query varchar(2000) 

DECLARE @DataFile varchar(2000) 
SET @DataFile = @RestoreFile + '.mdf' 

DECLARE @LogFile varchar(2000) 
SET @LogFile = @RestoreFile + '.ldf' 

IF @DB IS NOT NULL 
BEGIN 
    SET @query = 'BACKUP DATABASE ' + @DB + ' TO DISK = ' + QUOTENAME(@BackupFile, '''') 
    EXEC (@query) 
END 

-- RESTORE FILELISTONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE HEADERONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE LABELONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE VERIFYONLY FROM DISK = 'C:\temp\backup.dat' 

IF EXISTS(SELECT * FROM sysdatabases WHERE name = @TestDB) 
BEGIN 
    SET @query = 'DROP DATABASE ' + @TestDB 
    EXEC (@query) 
END 

RESTORE HEADERONLY FROM DISK = @BackupFile 
DECLARE @File int 
SET @File = @@ROWCOUNT 

DECLARE @Data varchar(500) 
DECLARE @Log varchar(500) 

SET @query = 'RESTORE FILELISTONLY FROM DISK = ' + QUOTENAME(@BackupFile , '''') 

CREATE TABLE #restoretemp 
(
LogicalName varchar(500), 
PhysicalName varchar(500), 
type varchar(10), 
FilegroupName varchar(200), 
size int, 
maxsize bigint 
) 
INSERT #restoretemp EXEC (@query) 

SELECT @Data = LogicalName FROM #restoretemp WHERE type = 'D' 
SELECT @Log = LogicalName FROM #restoretemp WHERE type = 'L' 

PRINT @Data 
PRINT @Log 

TRUNCATE TABLE #restoretemp 
DROP TABLE #restoretemp 

IF @File > 0 
BEGIN 
    SET @query = 'RESTORE DATABASE ' + @TestDB + ' FROM DISK = ' + QUOTENAME(@BackupFile, '''') + 
     ' WITH MOVE ' + QUOTENAME(@Data, '''') + ' TO ' + QUOTENAME(@DataFile, '''') + ', MOVE ' + 
     QUOTENAME(@Log, '''') + ' TO ' + QUOTENAME(@LogFile, '''') + ', FILE = ' + CONVERT(varchar, @File) 
    EXEC (@query) 
END 
GO 

從複製:http://weblogs.asp.net/mschwarz/archive/2004/08/26/220735.aspx

0

後上述SQL 2005 - quick way to quickly duplicate a database + data作品...

但如果烏拉圭回合使用SQL Server 2008 R2例如,U可能需要上,因爲返回了「創建表#restoretemp」部分略有模從「恢復filelistonly ...」已經改變,因爲...

後「最大範圍BIGINT」

maxsize bigint, 
fileid bigint, 
createlsn numeric(26), 
droplsn numeric(26), 
uniqueid uniqueidentifier, 
readonlylsn numeric(26), 
readwritelsn numeric(26), 
BackupSizeInBytes bigint, 
SourceBlockSize int, 
FileGroupId int, 
LogGroupGUID uniqueidentifier, 
DifferentialBaseLSN numeric(26), 
DifferentialBaseGUID uniqueidentifier, 
IsReadOnly bit, 
IsPresent bit, 
TDEThumbprint varbinary(40) 

基本上添加此和u將結束與下面的工作腳本...

USE master 
GO 

-- the original database (use 'SET @DB = NULL' to disable backup) 
DECLARE @DB varchar(200) 
SET @DB = 'source_db_name' 

-- the backup filename 
DECLARE @BackupFile varchar(2000) 
SET @BackupFile = 'D:\somewhere\DB\backup.dat' 

-- the new database name 
DECLARE @TestDB varchar(200) 
SET @TestDB = 'boon_db' 

-- the new database files without .mdf/.ldf 
DECLARE @RestoreFile varchar(2000) 
SET @RestoreFile = 'D:\somewhere\DB' 


-- **************************************************************** 
--     no change below this line 
-- **************************************************************** 


DECLARE @query varchar(2000) 

DECLARE @DataFile varchar(2000) 
SET @DataFile = @RestoreFile + '.mdf' 

DECLARE @LogFile varchar(2000) 
SET @LogFile = @RestoreFile + '.ldf' 

IF @DB IS NOT NULL 
BEGIN 
    SET @query = 'BACKUP DATABASE ' + @DB + ' TO DISK = ' + QUOTENAME(@BackupFile, '''') 
    EXEC (@query) 
END 

-- RESTORE FILELISTONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE HEADERONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE LABELONLY FROM DISK = 'C:\temp\backup.dat' 
-- RESTORE VERIFYONLY FROM DISK = 'C:\temp\backup.dat' 

IF EXISTS(SELECT * FROM sysdatabases WHERE name = @TestDB) 
BEGIN 
    SET @query = 'DROP DATABASE ' + @TestDB 
    EXEC (@query) 
END 

RESTORE HEADERONLY FROM DISK = @BackupFile 
DECLARE @File int 
SET @File = @@ROWCOUNT 

DECLARE @Data varchar(500) 
DECLARE @Log varchar(500) 

SET @query = 'RESTORE FILELISTONLY FROM DISK = ' + QUOTENAME(@BackupFile , '''') 

CREATE TABLE #restoretemp 
(
LogicalName varchar(500), 
PhysicalName varchar(500), 
type varchar(10), 
FilegroupName varchar(200), 
size int, 
maxsize bigint, 
fileid bigint, 
createlsn numeric(26), 
droplsn numeric(26), 
uniqueid uniqueidentifier, 
readonlylsn numeric(26), 
readwritelsn numeric(26), 
BackupSizeInBytes bigint, 
SourceBlockSize int, 
FileGroupId int, 
LogGroupGUID uniqueidentifier, 
DifferentialBaseLSN numeric(26), 
DifferentialBaseGUID uniqueidentifier, 
IsReadOnly bit, 
IsPresent bit, 
TDEThumbprint varbinary(40) 
) 
INSERT #restoretemp EXEC (@query) 

SELECT @Data = LogicalName FROM #restoretemp WHERE type = 'D' 
SELECT @Log = LogicalName FROM #restoretemp WHERE type = 'L' 

PRINT @Data 
PRINT @Log 

TRUNCATE TABLE #restoretemp 
DROP TABLE #restoretemp 

IF @File > 0 
BEGIN 
    SET @query = 'RESTORE DATABASE ' + @TestDB + ' FROM DISK = ' + QUOTENAME(@BackupFile, '''') + 
     ' WITH MOVE ' + QUOTENAME(@Data, '''') + ' TO ' + QUOTENAME(@DataFile, '''') + ', MOVE ' + 
     QUOTENAME(@Log, '''') + ' TO ' + QUOTENAME(@LogFile, '''') + ', FILE = ' + CONVERT(varchar, @File) 
    EXEC (@query) 
END 
GO