如何查询SQL服务器备份文件

How to query a SQL Server backup file

我正在使用 SQL Server 2008 R2,我有一个备份文件 B:\backups\full_backup.bak 但我不知道这个文件有哪些备份或每个备份有哪些文件。我如何编写查询以从此备份文件恢复数据库?

我试图恢复数据库,但它抛出了一个错误

Msg 3156, Level 16, State 4, Line 1
File 'Application_Primary' cannot be restored to 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Application_Primary.mdf'. Use WITH MOVE to identify a valid location for the file.

Msg 3156, Level 16, State 4, Line 1
File 'Application_FTS' cannot be restored to 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Application_FTS.mdf'. Use WITH MOVE to identify a valid location for the file.

Msg 3156, Level 16, State 4, Line 1
File 'Application_Log' cannot be restored to 'L:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Application_Log.ldf'. Use WITH MOVE to identify a valid location for the file.

Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.

Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

代码:

restore database Application_db
From disk = 'B:\backups\full_backup.bak'

在我看来,您正在尝试恢复一个备份,该备份具有来自另一台服务器的数据库备份,其中存储了一些数据文件(.mdf、ldf)的路径。

您将需要使用 MOVE 子句恢复数据库。只是 RESTORE 命令将尝试将数据库还原到进行此备份的数据库的路径。

查看备份文件中有哪些备份

USE master;
GO

RESTORE HEADERONLY 
FROM DISK = N'B:\backups\full_backup.bak'
GO

这将 return 此备份文件保存的备份。您需要确定 Position 您的备份文件所在的位置。

检查备份文件中有哪些文件

USE master;
GO

RESTORE FILELISTONLY 
FROM DISK = N'B:\backups\full_backup.bak'
 WITH FILE = 1    --<-- Position of backup in the backup file 
GO

这将 return 特定备份的文件。您将需要这些文件名用于恢复语句中的 MOVE 子句。

恢复数据库

USE master;
GO

RESTORE DATABASE [Application_DB]
 FROM DISK = N'B:\backups\full_backup.bak'
WITH FILE = 1             --<-- position of database backup in the backup file 
,MOVE N'Application_Primary' TO N'D:\<some valid path>\Application_Primary.mdf'
,MOVE N'Application_FTS'     TO N'D:\<some valid path>\Application_FTS.ndf'
,MOVE N'Application_Logy'    TO N'D:\<some valid path>\Application_Log.ldf'
,RECOVERY;

开始

您可以还原文件的 HEADERONLY 以准确查看它包含哪些备份:

DECLARE @HeaderInfo table
  (
        BackupName  nvarchar(128),
        BackupDescription  nvarchar(255) ,
        BackupType  smallint ,
        ExpirationDate  datetime ,
        Compressed  bit ,
        Position  smallint ,
        DeviceType  tinyint ,
        UserName  nvarchar(128) ,
        ServerName  nvarchar(128) ,
        DatabaseName  nvarchar(128) ,
        DatabaseVersion  int ,
        DatabaseCreationDate  datetime ,
        BackupSize  numeric(20,0) ,
        FirstLSN  numeric(25,0) ,
        LastLSN  numeric(25,0) ,
        CheckpointLSN  numeric(25,0) ,
        DatabaseBackupLSN  numeric(25,0) ,
        BackupStartDate  datetime ,
        BackupFinishDate  datetime ,
        SortOrder  smallint ,
        CodePage  smallint ,
        UnicodeLocaleId  int ,
        UnicodeComparisonStyle  int ,
        CompatibilityLevel  tinyint ,
        SoftwareVendorId  int ,
        SoftwareVersionMajor  int ,
        SoftwareVersionMinor  int ,
        SoftwareVersionBuild  int ,
        MachineName  nvarchar(128) ,
        Flags  int ,
        BindingID  uniqueidentifier ,
        RecoveryForkID  uniqueidentifier ,
        Collation  nvarchar(128) ,
        FamilyGUID  uniqueidentifier ,
        HasBulkLoggedData  bit ,
        IsSnapshot  bit ,
        IsReadOnly  bit ,
        IsSingleUser  bit ,
        HasBackupChecksums  bit ,
        IsDamaged  bit ,
        BeginsLogChain  bit ,
        HasIncompleteMetaData  bit ,
        IsForceOffline  bit ,
        IsCopyOnly  bit ,
        FirstRecoveryForkID  uniqueidentifier ,
        ForkPointLSN  numeric(25,0) NULL,
        RecoveryModel  nvarchar(60) ,
        DifferentialBaseLSN  numeric(25,0) NULL,
        DifferentialBaseGUID  uniqueidentifier ,
        BackupTypeDescription  nvarchar(60) ,
        BackupSetGUID  uniqueidentifier NULL,
        CompressedBackupSize  numeric(20,0)
  )


 INSERT INTO @HeaderInfo EXEC('RESTORE HEADERONLY 
 FROM DISK = N''B:\backups\full_backup.bak''
 WITH NOUNLOAD')

 SELECT * FROM @HeaderInfo

是错误的情况:MSSQLServer 在已备份的主数据库文件路径中创建数据库物理文件,除非您使用 move 命令为数据库文件指定新路径:

RESTORE DATABASE Application
   FROM DISK=N'B:\backups\full_backup.bak'
   WITH FILE 1,--use currect file number
   MOVE 'Application_Primary' TO 'C:\Application_Primary.mdf', 
   MOVE 'Application_FTS' TO 'C:\Application_FTS.mdf', 
   MOVE 'Application_Log' TO 'C:\Application_Log.ldf'