Skip to content

Commit 56bf8d7

Browse files
Restore-DbaDatabase - Add -StopAtLsn parameter for LSN-based restore
Add -StopAtLsn parameter to Restore-DbaDatabase and Invoke-DbaAdvancedRestore to support restoring to a specific Log Sequence Number using SQL Server's STOPATMARK = 'lsn:<value>' syntax. Works with -StopBefore to stop just before the specified LSN. (do Restore-DbaDatabase) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent aa4e253 commit 56bf8d7

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

public/Invoke-DbaAdvancedRestore.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,20 @@ function Invoke-DbaAdvancedRestore {
117117
Provides more granular control than timestamp-based recovery for critical business operations.
118118
119119
.PARAMETER StopBefore
120-
Stops the restore operation just before the specified StopMark rather than after it.
121-
Use this when you need to exclude a particular marked transaction from the restored database.
122-
Only effective when used in combination with the StopMark parameter for mark-based recovery scenarios.
120+
Stops the restore operation just before the specified StopMark or StopAtLsn rather than after it.
121+
Use this when you need to exclude a particular marked transaction or LSN from the restored database.
122+
Only effective when used in combination with the StopMark or StopAtLsn parameter.
123123
124124
.PARAMETER StopAfterDate
125125
DateTime value specifying that only StopMark occurrences after this date should be considered for restore termination.
126126
Use this when the same mark name appears multiple times in your transaction log backups.
127127
Ensures the restore stops at the correct instance of the mark when identical mark names exist at different times.
128128
129+
.PARAMETER StopAtLsn
130+
Log Sequence Number (LSN) in the transaction log at which to stop the restore operation.
131+
Use this for precise point-in-time recovery to an exact LSN, which provides more granular control than timestamp-based recovery.
132+
The LSN value can be obtained from sys.fn_dblog, backup headers, or error logs. Combine with -StopBefore to stop just before the specified LSN.
133+
129134
.PARAMETER Checksum
130135
Enables backup checksum verification during restore operations. Forces the restore to verify backup checksums and fail if checksums are not present.
131136
Use this to ensure backup files contain checksums and validate them during restore, following backup best practices.
@@ -239,6 +244,7 @@ function Invoke-DbaAdvancedRestore {
239244
[switch]$StopBefore,
240245
[string]$StopMark,
241246
[datetime]$StopAfterDate,
247+
[string]$StopAtLsn,
242248
[switch]$Checksum,
243249
[switch]$Restart,
244250
[switch]$EnableException
@@ -323,7 +329,13 @@ function Invoke-DbaAdvancedRestore {
323329
} else {
324330
$restore.NoRecovery = $False
325331
}
326-
if (-not [string]::IsNullOrEmpty($StopMark)) {
332+
if (-not [string]::IsNullOrEmpty($StopAtLsn)) {
333+
if ($StopBefore -eq $True) {
334+
$restore.StopBeforeMarkName = "lsn:$StopAtLsn"
335+
} else {
336+
$restore.StopAtMarkName = "lsn:$StopAtLsn"
337+
}
338+
} elseif (-not [string]::IsNullOrEmpty($StopMark)) {
327339
if ($StopBefore -eq $True) {
328340
$restore.StopBeforeMarkName = $StopMark
329341
if ($null -ne $StopAfterDate) {

public/Restore-DbaDatabase.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,16 @@ function Restore-DbaDatabase {
235235
Marked point in the transaction log to stop the restore at (Mark is created via BEGIN TRANSACTION (https://docs.microsoft.com/en-us/sql/t-sql/language-elements/begin-transaction-transact-sql?view=sql-server-ver15)).
236236
237237
.PARAMETER StopBefore
238-
Switch to indicate the restore should stop before StopMark occurs, default is to stop when mark is created.
238+
Switch to indicate the restore should stop before StopMark or StopAtLsn occurs, default is to stop when mark/LSN is reached.
239239
240240
.PARAMETER StopAfterDate
241241
By default the restore will stop at the first occurence of StopMark found in the chain, passing a datetime where will cause it to stop the first StopMark atfer that datetime.
242242
243+
.PARAMETER StopAtLsn
244+
Log Sequence Number (LSN) in the transaction log at which to stop the restore operation.
245+
Use this for precise point-in-time recovery to an exact LSN, which provides more granular control than timestamp-based recovery.
246+
The LSN value can be obtained from sys.fn_dblog, backup headers, or error logs. Combine with -StopBefore to stop just before the specified LSN.
247+
243248
.PARAMETER Checksum
244249
Enables backup checksum verification during restore operations. Forces the restore to verify backup checksums and fail if checksums are not present.
245250
Use this to ensure backup files contain checksums and validate them during restore, following backup best practices.
@@ -430,6 +435,16 @@ function Restore-DbaDatabase {
430435
431436
Restores the backups from \\ServerName\ShareName\File as database, stops before the first 'OvernightStart' mark that occurs after '21:00 10/05/2020'.
432437
438+
.EXAMPLE
439+
PS C:\> Restore-DbaDatabase -SqlInstance server1 -Path \\ServerName\ShareName\File -DatabaseName database -StopAtLsn '00000030:00000f28:0001'
440+
441+
Restores the backups from \\ServerName\ShareName\File as database, stopping when the specified LSN is reached.
442+
443+
.EXAMPLE
444+
PS C:\> Restore-DbaDatabase -SqlInstance server1 -Path \\ServerName\ShareName\File -DatabaseName database -StopAtLsn '00000030:00000f28:0001' -StopBefore
445+
446+
Restores the backups from \\ServerName\ShareName\File as database, stopping just before the specified LSN is reached.
447+
433448
Note that Date time needs to be specified in your local SQL Server culture
434449
435450
.EXAMPLE
@@ -498,6 +513,7 @@ function Restore-DbaDatabase {
498513
[switch]$StopBefore,
499514
[string]$StopMark,
500515
[datetime]$StopAfterDate = (Get-Date '01/01/1971'),
516+
[string]$StopAtLsn,
501517
[int]$StatementTimeout = 0,
502518
[parameter(ParameterSetName = "Restore")][parameter(ParameterSetName = "RestorePage")][switch]$Checksum,
503519
[parameter(ParameterSetName = "Restore")][parameter(ParameterSetName = "RestorePage")][switch]$Restart
@@ -891,6 +907,7 @@ function Restore-DbaDatabase {
891907
StopMark = $StopMark
892908
StopAfterDate = $StopAfterDate
893909
StopBefore = $StopBefore
910+
StopAtLsn = $StopAtLsn
894911
ExecuteAs = $ExecuteAs
895912
Checksum = $Checksum
896913
Restart = $Restart

0 commit comments

Comments
 (0)