Ant*_*lev 4 sql-server backup restore
我想要做的是备份我们的生产数据库之一并将它们恢复为我们的开发数据库(覆盖那里的内容)。
这就是我备份的方式:
backup database [authfx] to disk = N'f:\db_backups\authfx\authfx-latest.bak'
with
noformat,
init,
name = N'authfx Latest Full Database Backup',
skip,
norewind,
nounload,
stats = 1;
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试恢复其他使用数据库
restore database [dev-authfx] from disk = N'f:\db_backups\authfx\authfx-latest.bak'
with
file = 1,
nounload,
replace,
stats = 1;
Run Code Online (Sandbox Code Playgroud)
它到处吠叫:
Msg 1834, Level 16, State 1, Line 10
The file 'E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\authfx.mdf' cannot be overwritten. It is being used by database 'authfx'.
Msg 3156, Level 16, State 4, Line 10
File 'authfx' cannot be restored to 'E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\authfx.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 10
The file 'F:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\authfx_log.ldf' cannot be overwritten. It is being used by database 'authfx'.
Msg 3156, Level 16, State 4, Line 10
File 'authfx_log' cannot be restored to 'F:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\authfx_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 10
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 10
RESTORE DATABASE is terminating abnormally.
Run Code Online (Sandbox Code Playgroud)
我知道我需要添加with move并指定dev数据库的原始数据和日志文件的位置,但有什么办法可以省略这一点,只是说:从这个其他备份恢复这个数据库,不要接触任何其他东西并覆盖已经存在的内容以前在这里。
Stu*_*ore 10
不幸的是没有。SQL Server将始终尝试恢复到究竟是什么被记录在备份文件,除非你明确指定,否则(与MOVE在这种情况下)。
该REPLACE选项仅在您替换正在恢复的数据库拥有的文件时才有效(这是有道理的,您可能已经说过通过恢复可以杀死 DB_A,但 SQL 并不真正了解您对杀死数据库的感受DB_B 也是如此)。
如果这是您将经常做的事情,那么编写一个包含所有内容的小型 sql 脚本,保存它并在每次您想要进行恢复时使用它。这就是我从 prod 对 dev/UAT/Training 进行夜间恢复的方式。