SQL 服务器还原分离数据库

SQL Server restore a detach database

是否可以恢复分离的数据库(无需重新附加)?

有一个大数据库,但没有磁盘空间可用于创建备份。 所以,我想分离数据库,将其复制到另一台服务器。

这台服务器使用 powershell 脚本来恢复数据库。 这适用于分离的数据库吗?我不能用这个脚本重新附加它。只是恢复。

Restore-SqlDatabase -ServerInstance $serverInstance -Database $myDb -BackupFile "detachedFile" -Credential $Cred

数据库的备份格式不同 (.bak)。你在detach操作中得到的文件:mdf、ndf数据文件格式、ldf日志文件格式是不同的格式。

You can read about the formats。为了便于参考,我把网站上的内容放在下面。

Usually, database backup files with .bak extension contain data in the standard Microsoft Tape Format (MTF) used by Microsoft itself as well as many backup tools for the Microsoft Windows platform.

Microsoft Tape Format is used while writing and reading data to and from removable storage devices during storage management or data protection operations such as data transfers, copies, backup and restore. In the case of SQL Server, MTF files contain data and log information (MDF and LDF files) necessary for restoring the database. The MDF file is the Main Data File or primary database data file, that binds all other files in that database together. The LDF is the Log Data File and it contains all log information and is crucial for a database restore process.

您不能对 mdf、ldf 文件使用还原数据库命令。您需要使用附加、分离方法使它们在另一个环境中可用。正如@Peter Smith 建议的那样,您可以使用 powershell 脚本来执行相同的操作。