Sql Server 2005從備份還原到最近的檔案位置

Sql Server 2005從備份還原到最近的檔案位置

我正在嘗試將資料庫從磁碟還原到最新的備份。備份中有四個備份集。我想恢復最近的一張(今天拍攝的)。我需要能夠使用 TSQL 來完成此操作。

下面的腳本有效:

RESTORE DATABASE DatabaseName 
FROM DISK = 'D:\Data\DatabaseName.bak' WITH FILE = 4, REPLACE

但是,隨著更多備份的進行,檔案位置將來將會發生變化。

有沒有辦法將備份還原到檔案位置而不指定確切的檔案編號?類似“WITH FILE = most_recent_backup”

答案1

RESTORE HEADERONLY命令將為您提供文件中所有備份集的資料清單。從那裡您可以選擇最大值Position並將其傳遞到FILE參數。

http://msdn.microsoft.com/en-us/library/ms178536.aspx

答案2

不要將所有備份放入一個檔案中。如果您這樣做並且該檔案已損壞,那麼您就丟失了所有備份。

當您使用 BACKUP DATABASE 和 RESTORE DATABASE 命令時,應使用動態檔案名稱將每個備份放入自己的檔案中。

答案3

馬特的回答讓我朝著正確的方向前進。我需要先儲存結果集,然後才能選擇最大位置。我的搜尋引導我找到了這個解決方案http://sqlforums.windowsitpro.com/web/forum/messageview.aspx?catid=74&threadid=93926&enterthread=y

set nocount on

Create Table #header (
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
)

insert #header
Exec ('restore headeronly from disk = ''\\pathToBackup\file.bak''')

select backupstartdate from #header
drop table #header 

相關內容