gpt4 book ai didi

sql - 如何在同一服务器上将数据库备份和恢复为副本?

转载 作者:行者123 更新时间:2023-12-02 00:05:43 24 4
gpt4 key购买 nike

我有一个 SQL2005 Express 数据库,我想在同一实例上创建它的副本。您如何使用脚本来做到这一点?

我已经有一个用于生成备份的脚本,但恢复失败...

错误:

Msg 3234, Level 16, State 2, Line 2 Logical file 'MyDB_data' is not part of database 'MyDB_Test'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 2 RESTORE DATABASE is terminating abnormally.

解决方案:

RESTORE DATABASE [MyDB_Test]
FROM DISK = 'C:\temp\SQL\MyDB.bak'
WITH
MOVE 'MyDB' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDB_Test.mdf'
, MOVE 'MyDB_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDB_Test_log.ldf'
, REPLACE;

原因:
我第一次尝试时没有正确识别逻辑路径。

最佳答案

RESTORE FILELISTONLY 是一条信息性命令,不需要执行恢复。用户可以使用它来找出数据文件的逻辑名称,这些逻辑名称可以与 MOVE 命令一起使用,将数据库恢复到新位置。

根据错误消息的建议,您需要使用RESTORE FILELISTONLY来查看数据库的逻辑名称是什么。您的恢复命令有这些错误。

以下是您需要执行的操作示例:

--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'

关于sql - 如何在同一服务器上将数据库备份和恢复为副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1360529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com