从批处理文件恢复 .bak 文件

Restoring .bak file from batch file

我正在自动化测试环境设置过程,以下是涉及的手动步骤的一部分:

  1. 从网络驱动器复制一个 .bak 文件到我的 D: 驱动器
  2. 启动 SQL Management Studio,使用还原文件和文件组选项还原 .bak 文件

对于第 1 点,我使用了:

robocopy \10.xx.xxx.x\dirA\Build\%myVar%\DB\ D:\ myDB_V%myVar%.bak

复制的很好!

现在对于第 2 点,我正在命令提示符下使用 sqlcmd 尝试以下命令:

sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE myDB_V2.2.2.10 FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

它说

Msg 102, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near '.2'.
Msg 319, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

我无法理解它在说什么。

有人能帮帮我吗?

试试这个:

sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE 'myDB_V2.2.2.10' FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

看起来错误与您的数据库名称有关,"RESTORE DATABASE myDB_V2**.2**.2.10 [...]"。我建议不要在数据库名称中使用句点,因为句点用于分隔查询中的数据库对象。如果您已经在使用句点,请尝试将数据库名称括在方括号 [].

试试这个
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE [myDB_V2.2.2.10] FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"