如何从备份文件中检索数据库的逻辑文件名

发布于 2024-11-30 08:59:19 字数 795 浏览 3 评论 0原文

我正在研究如何使用 SQL 脚本 (T-SQL) 恢复数据库备份。步骤如下:

数据库 YourDB 具有完整备份 YourBackUpFile.bak。可以通过以下两个步骤恢复:

第 1 步:从备份中检索数据库的逻辑文件名。

仅恢复文件列表
从磁盘 = 'D:BackUpYourBackUpFile.bak'
去

第 2 步:在后续步骤中使用 LogicalName 列中的值。

----使数据库成为单用户模式
更改数据库 YourDB
设置单个用户为
立即回滚

----恢复数据库
恢复数据库 YourDB
从磁盘 = 'D:BackUpYourBackUpFile.bak'
将“YourMDFLogicalName”移动到“D:DataYourMDFFile.mdf”,
将“YourLDFLogicalName”移动到“D:DataYourLDFFile.ldf”

我只是在如何获取 YourMDFLogicalNameYourLDFLogicalName 方面遇到问题。 有人可以帮我吗?

I was looking into the steps of how to Restore Database Backup using SQL Script (T-SQL). Here are the steps:

Database YourDB has full backup YourBackUpFile.bak. It can be restored using following two steps:

Step 1: Retrieve the logical file name of the database from the backup.

RESTORE FILELISTONLY
FROM DISK = 'D:BackUpYourBackUpFile.bak'
GO

Step 2: Use the values in the LogicalName column in the following step.

----Make Database to single user Mode
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE

----Restore Database
RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'

I am just having problem on how to get the YourMDFLogicalName and YourLDFLogicalName.
Can any one help me with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜警司 2024-12-07 08:59:19
DECLARE @Table TABLE (LogicalName varchar(128),[PhysicalName] varchar(128), [Type] varchar, [FileGroupName] varchar(128), [Size] varchar(128), 
            [MaxSize] varchar(128), [FileId]varchar(128), [CreateLSN]varchar(128), [DropLSN]varchar(128), [UniqueId]varchar(128), [ReadOnlyLSN]varchar(128), [ReadWriteLSN]varchar(128), 
            [BackupSizeInBytes]varchar(128), [SourceBlockSize]varchar(128), [FileGroupId]varchar(128), [LogGroupGUID]varchar(128), [DifferentialBaseLSN]varchar(128), [DifferentialBaseGUID]varchar(128), [IsReadOnly]varchar(128), [IsPresent]varchar(128), [TDEThumbprint]varchar(128)
)
DECLARE @Path varchar(1000)='C:\SomePath\Base.bak'
DECLARE @LogicalNameData varchar(128),@LogicalNameLog varchar(128)
INSERT INTO @table
EXEC('
RESTORE FILELISTONLY 
   FROM DISK=''' +@Path+ '''
   ')

   SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
   SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')

SELECT @LogicalNameData,@LogicalNameLog

更新

根据Microsoft 网站

SQL Server 文件有两个名称:

逻辑文件名

逻辑文件名是用于引用物理文件的名称
在所有 Transact-SQL 语句中。逻辑文件名必须符合
SQL Server 标识符的规则,并且在逻辑中必须是唯一的
数据库中的文件名。

操作系统文件名

os_file_name 是物理文件的名称,包括
目录路径。它必须遵循操作系统文件的规则
名称。

DECLARE @Table TABLE (LogicalName varchar(128),[PhysicalName] varchar(128), [Type] varchar, [FileGroupName] varchar(128), [Size] varchar(128), 
            [MaxSize] varchar(128), [FileId]varchar(128), [CreateLSN]varchar(128), [DropLSN]varchar(128), [UniqueId]varchar(128), [ReadOnlyLSN]varchar(128), [ReadWriteLSN]varchar(128), 
            [BackupSizeInBytes]varchar(128), [SourceBlockSize]varchar(128), [FileGroupId]varchar(128), [LogGroupGUID]varchar(128), [DifferentialBaseLSN]varchar(128), [DifferentialBaseGUID]varchar(128), [IsReadOnly]varchar(128), [IsPresent]varchar(128), [TDEThumbprint]varchar(128)
)
DECLARE @Path varchar(1000)='C:\SomePath\Base.bak'
DECLARE @LogicalNameData varchar(128),@LogicalNameLog varchar(128)
INSERT INTO @table
EXEC('
RESTORE FILELISTONLY 
   FROM DISK=''' +@Path+ '''
   ')

   SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
   SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')

SELECT @LogicalNameData,@LogicalNameLog

UPDATE

According to Microsoft site:

SQL Server files have two names:

logical_file_name

The logical_file_name is the name used to refer to the physical file
in all Transact-SQL statements. The logical file name must comply with
the rules for SQL Server identifiers and must be unique among logical
file names in the database.

os_file_name

The os_file_name is the name of the physical file including the
directory path. It must follow the rules for the operating system file
names.

温柔一刀 2024-12-07 08:59:19

逻辑文件名:是 Microsoft SQL Server 引用文件时使用的名称。该名称在数据库中必须是唯一的,并且符合标识符规则。该名称可以是字符或 Unicode 常量、常规标识符或分隔标识符。

来自:http://msdn.microsoft.com/en-us/ library/aa275464(v=sql.80).aspx

另外,从 Dalex 的脚本中,您可以只运行(无需所有表逻辑):

RESTORE FILELISTONLY FROM DISK = 'D:\MyBackups\Backup.bak'

logical_file_name: is the name used in Microsoft SQL Server when referencing the file. The name must be unique within the database and conform to the rules for identifiers. The name can be a character or Unicode constant, a regular identifier, or a delimited identifier.

From: http://msdn.microsoft.com/en-us/library/aa275464(v=sql.80).aspx

Also, from Dalex's script, you can just run (without all the table logic):

RESTORE FILELISTONLY FROM DISK = 'D:\MyBackups\Backup.bak'
感悟人生的甜 2024-12-07 08:59:19

脚本的更新版本:

DECLARE @Table TABLE (
    LogicalName varchar(128),
    [PhysicalName] varchar(128), 
    [Type] varchar, 
    [FileGroupName] varchar(128), 
    [Size] varchar(128),
    [MaxSize] varchar(128), 
    [FileId]varchar(128), 
    [CreateLSN]varchar(128), 
    [DropLSN]varchar(128), 
    [UniqueId]varchar(128), 
    [ReadOnlyLSN]varchar(128), 
    [ReadWriteLSN]varchar(128),
    [BackupSizeInBytes]varchar(128), 
    [SourceBlockSize]varchar(128), 
    [FileGroupId]varchar(128), 
    [LogGroupGUID]varchar(128), 
    [DifferentialBaseLSN]varchar(128), 
    [DifferentialBaseGUID]varchar(128), 
    [IsReadOnly]varchar(128), 
    [IsPresent]varchar(128), 
    [TDEThumbprint]varchar(128),
    [SnapshotUrl]varchar(128)
)
DECLARE @Path varchar(1000)='/path/to/backup.bak'
DECLARE @LogicalNameData varchar(128),@LogicalNameLog varchar(128)
INSERT INTO @table
EXEC('
RESTORE FILELISTONLY
   FROM DISK=''' +@Path+ '''
   ')

   SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
   SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')

SELECT @LogicalNameData,@LogicalNameLog

Updated version of script:

DECLARE @Table TABLE (
    LogicalName varchar(128),
    [PhysicalName] varchar(128), 
    [Type] varchar, 
    [FileGroupName] varchar(128), 
    [Size] varchar(128),
    [MaxSize] varchar(128), 
    [FileId]varchar(128), 
    [CreateLSN]varchar(128), 
    [DropLSN]varchar(128), 
    [UniqueId]varchar(128), 
    [ReadOnlyLSN]varchar(128), 
    [ReadWriteLSN]varchar(128),
    [BackupSizeInBytes]varchar(128), 
    [SourceBlockSize]varchar(128), 
    [FileGroupId]varchar(128), 
    [LogGroupGUID]varchar(128), 
    [DifferentialBaseLSN]varchar(128), 
    [DifferentialBaseGUID]varchar(128), 
    [IsReadOnly]varchar(128), 
    [IsPresent]varchar(128), 
    [TDEThumbprint]varchar(128),
    [SnapshotUrl]varchar(128)
)
DECLARE @Path varchar(1000)='/path/to/backup.bak'
DECLARE @LogicalNameData varchar(128),@LogicalNameLog varchar(128)
INSERT INTO @table
EXEC('
RESTORE FILELISTONLY
   FROM DISK=''' +@Path+ '''
   ')

   SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
   SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')

SELECT @LogicalNameData,@LogicalNameLog
不打扰别人 2024-12-07 08:59:19

如果您有原始数据库(从中获取备份文件),那么获取其逻辑名称的最简单方法是通过 :

use [original_db]
go
select file_name(1)
go

或使用一个 T-SQL

> sqlcmd [connection parameters and credentials] -d [original_db] -Q "set nocount on; select file_name(1)" -h-1 -W 

这将与您在 RESTORE 命令中使用的逻辑名称相同。

If you have the original DB (from which the backup file was taken) then the easiest way to obtain its logical name is via :

use [original_db]
go
select file_name(1)
go

or with one T-SQL

> sqlcmd [connection parameters and credentials] -d [original_db] -Q "set nocount on; select file_name(1)" -h-1 -W 

This would be the same logical_name you would use in the RESTORE command.

肩上的翅膀 2024-12-07 08:59:19
DECLARE @Path VARCHAR(1000)= N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\aaa.bak',
        @RestorePath NVARCHAR(max)='C:\'

DECLARE @Table TABLE
    (
      LogicalName VARCHAR(128) ,
      [PhysicalName] VARCHAR(128) ,
      [Type] VARCHAR ,
      [FileGroupName] VARCHAR(128) ,
      [Size] VARCHAR(128) ,
      [MaxSize] VARCHAR(128) ,
      [FileId] VARCHAR(128) ,
      [CreateLSN] VARCHAR(128) ,
      [DropLSN] VARCHAR(128) ,
      [UniqueId] VARCHAR(128) ,
      [ReadOnlyLSN] VARCHAR(128) ,
      [ReadWriteLSN] VARCHAR(128) ,
      [BackupSizeInBytes] VARCHAR(128) ,
      [SourceBlockSize] VARCHAR(128) ,
      [FileGroupId] VARCHAR(128) ,
      [LogGroupGUID] VARCHAR(128) ,
      [DifferentialBaseLSN] VARCHAR(128) ,
      [DifferentialBaseGUID] VARCHAR(128) ,
      [IsReadOnly] VARCHAR(128) ,
      [IsPresent] VARCHAR(128) ,
      [TDEThumbprint] VARCHAR(128)
    )

DECLARE @LogicalNameData VARCHAR(128) ,
    @LogicalNameLog VARCHAR(128)
INSERT  INTO @table
        EXEC ( '
RESTORE FILELISTONLY 
   FROM DISK=''' + @Path + '''
   '
            )

DECLARE @restoreScript NVARCHAR(max)='RESTORE DATABASE [aaa] FROM DISK =''' + @Path + ''' WITH FILE = 1 '

SELECT  @restoreScript +=CHAR(10) + ' ,MOVE  ''' +  LogicalName + ''' TO ''' + 
        @RestorePath  + LogicalName + RIGHT(PhysicalName,4) + ''''
                         FROM   @Table
                         WHERE  Type = 'D'

 SELECT  @restoreScript += ' ,MOVE  ''' +  LogicalName + ''' TO ''' + @RestorePath  + LogicalName + '.ldf'''
                        FROM    @Table
                        WHERE   Type = 'L'

SET @restoreScript += ' , NOUNLOAD, REPLACE, STATS = 10 '
SELECT  @restoreScript
DECLARE @Path VARCHAR(1000)= N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\aaa.bak',
        @RestorePath NVARCHAR(max)='C:\'

DECLARE @Table TABLE
    (
      LogicalName VARCHAR(128) ,
      [PhysicalName] VARCHAR(128) ,
      [Type] VARCHAR ,
      [FileGroupName] VARCHAR(128) ,
      [Size] VARCHAR(128) ,
      [MaxSize] VARCHAR(128) ,
      [FileId] VARCHAR(128) ,
      [CreateLSN] VARCHAR(128) ,
      [DropLSN] VARCHAR(128) ,
      [UniqueId] VARCHAR(128) ,
      [ReadOnlyLSN] VARCHAR(128) ,
      [ReadWriteLSN] VARCHAR(128) ,
      [BackupSizeInBytes] VARCHAR(128) ,
      [SourceBlockSize] VARCHAR(128) ,
      [FileGroupId] VARCHAR(128) ,
      [LogGroupGUID] VARCHAR(128) ,
      [DifferentialBaseLSN] VARCHAR(128) ,
      [DifferentialBaseGUID] VARCHAR(128) ,
      [IsReadOnly] VARCHAR(128) ,
      [IsPresent] VARCHAR(128) ,
      [TDEThumbprint] VARCHAR(128)
    )

DECLARE @LogicalNameData VARCHAR(128) ,
    @LogicalNameLog VARCHAR(128)
INSERT  INTO @table
        EXEC ( '
RESTORE FILELISTONLY 
   FROM DISK=''' + @Path + '''
   '
            )

DECLARE @restoreScript NVARCHAR(max)='RESTORE DATABASE [aaa] FROM DISK =''' + @Path + ''' WITH FILE = 1 '

SELECT  @restoreScript +=CHAR(10) + ' ,MOVE  ''' +  LogicalName + ''' TO ''' + 
        @RestorePath  + LogicalName + RIGHT(PhysicalName,4) + ''''
                         FROM   @Table
                         WHERE  Type = 'D'

 SELECT  @restoreScript += ' ,MOVE  ''' +  LogicalName + ''' TO ''' + @RestorePath  + LogicalName + '.ldf'''
                        FROM    @Table
                        WHERE   Type = 'L'

SET @restoreScript += ' , NOUNLOAD, REPLACE, STATS = 10 '
SELECT  @restoreScript
甜中书 2024-12-07 08:59:19

如果您手动进行更改,则可以使用以下查询找到相应的名称:

SELECT db.name AS [DB Name], mf.name AS [Full logical name],
CASE
  WHEN CHARINDEX('_', mf.name) > 0 THEN LEFT (mf.name, CHARINDEX('_', mf.name)-1)
  ELSE mf.name
END AS [Shortened logical name]
FROM sys.master_files mf
INNER JOIN sys.databases db ON db.database_id = mf.database_id
WHERE mf.type_desc != 'LOG'
ORDER BY db.name;

If you are making your changes manually, then you can find the corresponding names with the following query:

SELECT db.name AS [DB Name], mf.name AS [Full logical name],
CASE
  WHEN CHARINDEX('_', mf.name) > 0 THEN LEFT (mf.name, CHARINDEX('_', mf.name)-1)
  ELSE mf.name
END AS [Shortened logical name]
FROM sys.master_files mf
INNER JOIN sys.databases db ON db.database_id = mf.database_id
WHERE mf.type_desc != 'LOG'
ORDER BY db.name;
十雾 2024-12-07 08:59:19

尝试以下代码:

DECLARE @BackUpPath nvarchar(2000)='D:\BackUpYourBackUpFile.bak'
EXEC('RESTORE FILELISTONLY  FROM DISK=''' +@BackUpPath+ '''  ')

Try following code:

DECLARE @BackUpPath nvarchar(2000)='D:\BackUpYourBackUpFile.bak'
EXEC('RESTORE FILELISTONLY  FROM DISK=''' +@BackUpPath+ '''  ')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文