我正在研究如何Restore Database Backup using SQL Script (T-SQL)的步骤。以下是步骤:
数据库
YourDB
具有完整备份YourBackUpFile.bak
。可以使用以下两个步骤恢复它:步骤1:从备份中检索数据库的逻辑文件名。
RESTORE FILELISTONLY FROM DISK = 'D:BackUpYourBackUpFile.bak' GO
第2步:使用以下步骤中
LogicalName
列中的值。----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'
我遇到了如何获取YourMDFLogicalName
和YourLDFLogicalName
的问题。
任何人都可以帮助我吗?
答案 0 :(得分:39)
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
<强>更新强>
SQL Server文件有两个名称:
logical_file_name
logical_file_name是用于引用物理文件的名称 在所有Transact-SQL语句中。逻辑文件名必须符合 SQL Server标识符的规则,在逻辑上必须是唯一的 数据库中的文件名。
则os_file_name
os_file_name是包含的物理文件的名称 目录路径。它必须遵循操作系统文件的规则 名。
答案 1 :(得分:14)
logical_file_name:是引用该文件时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'
答案 2 :(得分:2)
如果您有原始数据库(从中获取备份文件),则获取其逻辑名称的最简单方法是:
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命令中使用的logical_name相同。
答案 3 :(得分:1)
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
答案 4 :(得分:1)
更新了脚本版本:
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
答案 5 :(得分:-1)
如果您手动进行更改,则可以通过以下查询找到相应的名称:
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;