上述腳本正常工作,但不會更改複製服務器的邏輯文件名。因此,如果您嘗試再次運行它以反轉該過程,則它將在MOVE語句中失敗。
我稍微修改了腳本,想出了以下這些似乎適用於我的工作。我是新手,所以要小心!
DECLARE @SOURCEDB nvarchar(100)
DECLARE @SOURCEDBLOG nvarchar(100)
DECLARE @DESTINATIONDB nvarchar(100)
DECLARE @DESTINATIONDBLOG nvarchar(100)
DECLARE @BACKUPDIR nvarchar(100)
DECLARE @BACKUPFILE nvarchar(100)
DECLARE @BACKUPNAME nvarchar(100)
DECLARE @SQLDATADIR nvarchar(100)
DECLARE @SQLDATABACKUPFILE nvarchar(100)
DECLARE @SQLDATABACKUPLOGFILE nvarchar(100)
--CHANGE THESE VALUES TO MATCH YOUR SYSTEM
SET @SOURCEDB = N'test'
SET @DESTINATIONDB = N'test-backup'
SET @BACKUPDIR = N'C:\SHARED\'
SET @SQLDATADIR = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\'
--CALCULATED VALUES
SET @SOURCEDBLOG = @SOURCEDB + N'_log'
SET @DESTINATIONDBLOG = @DESTINATIONDB + N'_log'
SET @BACKUPFILE = @BACKUPDIR + @SOURCEDB + N'-to-' + @DESTINATIONDB + N'.bak'
SET @BACKUPNAME = @SOURCEDB + N'-Full Backup'
SET @SQLDATABACKUPFILE = @SQLDATADIR + @DESTINATIONDB + N'.mdf'
SET @SQLDATABACKUPLOGFILE = @SQLDATADIR + @DESTINATIONDBLOG + N'.ldf'
--BACKUP THE DATABASE
BACKUP DATABASE @SOURCEDB
TO DISK = @BACKUPFILE
WITH
COPY_ONLY,
NOFORMAT,
INIT,
NAME = @BACKUPNAME,
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10
--RESTORE THE BACKUP TO THE NEW DATABASE NAME
RESTORE DATABASE @DESTINATIONDB
FROM DISK = @BACKUPFILE
WITH
FILE = 1,
MOVE @SOURCEDB TO @SQLDATABACKUPFILE,
MOVE @SOURCEDBLOG TO @SQLDATABACKUPLOGFILE,
NOUNLOAD,
REPLACE,
STATS = 10
--UPDATE THE LOGICAL FILE NAMES
DECLARE @TEMPLATE varchar(500)
DECLARE @SCRIPT varchar(500)
SET @TEMPLATE = N'ALTER DATABASE [{DBNAME}] MODIFY FILE (NAME = [{OLD}], NEWNAME = [{NEW}])'
SET @SCRIPT = REPLACE(REPLACE(REPLACE(@TEMPLATE, '{DBNAME}', @DESTINATIONDB),'{OLD}',@SOURCEDB),'{NEW}',@DESTINATIONDB)
EXEC(@SCRIPT)
SET @SCRIPT = REPLACE(REPLACE(REPLACE(@TEMPLATE, '{DBNAME}', @DESTINATIONDB),'{OLD}',@SOURCEDBLOG),'{NEW}',@DESTINATIONDBLOG)
EXEC(@SCRIPT)
你碰巧知道SQL代碼做同樣的事嗎? – 2009-09-17 17:11:53
嗯......此外,這似乎仍然從數據庫備份中恢復,而不是數據庫的「實時運行版本」......也就是說,選擇數據庫看起來只是列出執行備份的便捷方式在那個DB上。 – 2009-09-17 17:21:38
哇,你說得對。我沒有注意到,因爲我有我所有數據庫的備份。我想你必須通過文件系統。 – 2009-09-17 17:38:17