dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

[Bug]: Get-DbaDbBackupHistory (Break in LSN chain)

Open saxet77 opened this issue 3 years ago β€’ 41 comments

Report

Host used

  • [ ] powershell.exe
  • [x] ISE
  • [ ] VS Code
  • [ ] Other (please specify)

Errors Received


WARNING: [21:10:15][Restore-DbaDatabase] Database TODELETE failed testing,  skipping

Steps to Reproduce

# Get backup history from the SourceInstance msdb database
$ListBackupFile = Get-DbaDbBackupHistory -SqlInstance Server1 -Database Database1 -last 
  
 # Generate the PIT restore script
 $RestoreScript = $ListBackupFile | Restore-DbaDatabase -SqlInstance Server2 -DatabaseName TODELETE -RestoreTime '2021-07-07 01:17:15' -WithReplace -OutputScriptOnly -ReplaceDbNameInFile -TrustDbBackupHistory;

$ListBackupFile
SqlInstance                 Database   Type TotalSize   DeviceType Start                   Duration End                    
-----------                 --------   ---- ---------   ---------- -----                   -------- ---                    

#### Expected Behavior

Restore Script provide by SSMS 'Restore/Database/Script'

Actual Behavior

Restore Script provide by DBATools (missing 1 log file)

Environmental information

🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 Please provide the output of the below script

Powershell Version = 5.1.14393.3866
dbatools lastest installed = 1.0.169
Culture of OS = 
LCID             Name             DisplayName                                                                                                                                                                                                                              
----             ----             -----------                                                                                                                                                                                                                              
1036             fr-FR            French (France)

SQL Server:

Microsoft SQL Server 2017 (RTM-CU23) (KB5000685) - 14.0.3381.3 (X64)   Feb  9 2021 12:08:50   Copyright (C) 2017 Microsoft Corporation  Developer Edition (64-bit) on Windows Server 2016 Datacenter 10.0 <X64> (Build 14393: ) (Hypervisor) 
us_english

saxet77 avatar Jul 08 '21 05:07 saxet77

Expected Behavior generate from the SSMS 'Database/Restore/Script'

Actual behavior generated by DBATools (seems missing 1 log file)

saxet77 avatar Jul 08 '21 05:07 saxet77

Capture d’écran 2021-07-08 aΜ€ 15 20 56

Include the msdb..backupset table extract for the database/log backup LSN. DatabaseBackupLSN are the same for the backup database and the missing backup log. Peharps it's the problem ...

saxet77 avatar Jul 08 '21 13:07 saxet77

Currently I can not reproduce this. I just had a look at the code: BackupDatabaseLSN is not used.

andreasjordan avatar Jul 08 '21 14:07 andreasjordan

Does it return the correct script if you don't use -RestoreTime?

andreasjordan avatar Jul 08 '21 14:07 andreasjordan

If you look at $ListBackupFile - the file is missing already there - correct?

andreasjordan avatar Jul 08 '21 15:07 andreasjordan

Sorry for all the messages - I think I found the problem...

andreasjordan avatar Jul 08 '21 15:07 andreasjordan

@saxet77 - Can you please verify that you get the correct result from Get-DbaDbBackupHistory if you add -IncludeCopyOnly, so using $ListBackupFile = Get-DbaDbBackupHistory -SqlInstance Server1 -Database Database1 -last -IncludeCopyOnly?

@Stuart-Moore - Can you tell me more about this part? https://github.com/sqlcollaborative/dbatools/blob/d81f07cc176102dda4c48ae620953d0f62ed3cb6/functions/Get-DbaDbBackupHistory.ps1#L340-L345 Especially the difference in the Where-Object: [bigint]$_.DatabaseBackupLSN -eq [bigint]$fullDb.CheckPointLSN.

andreasjordan avatar Jul 08 '21 15:07 andreasjordan

About your situation: The full backup finished after the log backup started, but before the log backup finished. That's why the missing log backup has a different DatabaseBackupLSN than the other log backup and then CheckpointLSN of the full backup. Because of that, the missing log backup is not been taken into account.

andreasjordan avatar Jul 08 '21 15:07 andreasjordan

@andreasjordan, I will use -IncludeCopyOnly in my script.

You think it's need a fix on the Get-DbaDbBackupHistory cmdlet ?

saxet77 avatar Jul 09 '21 05:07 saxet77

Yes, we have to fix Get-DbaDbBackupHistory. Your case (first log backup started before full finishes but finishes after full) is a rare case and thus has not seen in tests until now. I don't understand the code difference between having IncludeCopyOnly set and not set yet. Buit I see using -IncludeCopyOnly as a workaround for you and I will try to find time to dig deep into that or maybe @Stuart-Moore has time as he is the author of that command.

andreasjordan avatar Jul 09 '21 05:07 andreasjordan

Can you add -StopAfterGetBackupInformation -GetBackupInformation backupInformation to the command? Then the variable $backupInformation is filled with the backups that would be restored. I would like to know if the "missing log backup" is included there or not. So to have an idea where it gets filtered out.

andreasjordan avatar Jul 09 '21 08:07 andreasjordan

So here the $FilteredBackupHistory (that is what is in your $backupinformation) is still including the "missing log backup": https://github.com/sqlcollaborative/dbatools/blob/879021234ae8730f4550d75f1ee43397b6b673a7/functions/Restore-DbaDatabase.ps1#L705

Test-DbaBackupInformation is then building an internal $DbHistory which is passed to Test-DbaLsnChain and failes there because of the missing log backup.

Ok, I'll have a deeper look into Test-DbaBackupInformation...

andreasjordan avatar Jul 09 '21 12:07 andreasjordan

Can you please run the command that fails (edit: that writes the warning about the brocken chain) with -Verbose - maybe that output helps us.

andreasjordan avatar Jul 09 '21 12:07 andreasjordan

Thanks for the verbose output - but I still don't see where the log backup gets filtered out. We have to wait for @Stuart-Moore - maybe he has an idea.

andreasjordan avatar Jul 09 '21 15:07 andreasjordan

@andreasjordan
DatabaseBackupLSN is the LSN of the last 'real' full backup taken before the current backup was taken.

CheckpointLSN is usuallly the same as FirstLSN and is the LSN to which the backup would be restored to. It's rare to not have them be the same but it can happen

Reading those 2 lines I can't remember why they're different. Again, i expect it was another edge case that cropped up

@saxet77 is it possible for you to share you backup history? Then we can wrap it into the tests to make it easier to play with/replicate

If you could just run:

| Export-CliXml -Path c:\somewhere\file.xml

and upload it to here, then we can rehydrate it and work with. If you want to obfuscate server and db names that's fine. Really just want the LSNs and the other metadata.

Stuart-Moore avatar Jul 10 '21 16:07 Stuart-Moore

sorry, mucked up the Markdown in the last comment. Pleae could you upload: $ListBackupFile | Export-CliXml -Depth 5 -Path c:\somewhere\file.xml

I can fake something if needed, but having 'real' data will make sure we have a proper test to stop it coming back again

Stuart-Moore avatar Jul 11 '21 10:07 Stuart-Moore

Going go need a bit more info please, was concentrating on the restore side to much. Could you dump out the raw backup history for me please? Will need something like: Get-DbaDbBackupHistory -sqlinstance x -database y -since '10/07/2021 21:10' | Export-CliXml -path -depth 5 ./some.xml

Sorry for the delay, time is being eaten up by a large migration atm.

Stuart-Moore avatar Jul 14 '21 15:07 Stuart-Moore

Think i now have a fix for this, but it's failing a single test. Need to rebuild my local test rig to work out why.

Stuart-Moore avatar Jul 18 '21 16:07 Stuart-Moore

Ok, so this is getting annoying. Passes fine locally: image

But fails in Appveyor.

Trying to rdp in and it appears to not be liking the password. Is it not just the one in appveyor.yml anymore? (@andreasjordan or @potatoqualitee can you help here?)

Stuart-Moore avatar Jul 21 '21 15:07 Stuart-Moore

oh sorry, ill msg you on twitter, stuart

potatoqualitee avatar Jul 21 '21 16:07 potatoqualitee

Gah, it's a bug with 2008r2! That's why it works fine for me.

For some reason 2008r2 returns 3 rows of history, others return 4. Both result sets are restoreable though, so that's all that really matters. will fix up tests and push it back up

Stuart-Moore avatar Jul 22 '21 12:07 Stuart-Moore

haha awesome work! πŸš’

potatoqualitee avatar Jul 22 '21 13:07 potatoqualitee

Yeah, that was confusing the heck out of me. The good news is that the filtering of all back up history is done by select-dbabackupinformation, so we should have consistency between get-dbadbbackuphistory and the restore stack.

@saxet77 Really sorry for the delay on this one. Would you be able to take this new branch for a spin and see if it fixes your issue. I think it should, but would be great to know it does.

Stuart-Moore avatar Jul 22 '21 13:07 Stuart-Moore

Thanks, @Stuart-Moore ! I'll wait for your PR to release 1.1.0 πŸ’―

potatoqualitee avatar Jul 22 '21 13:07 potatoqualitee

Hi @Stuart-Moore, no problem for the delay. Not very familiar with GitHub "how can I take this new branch" to validate the fix on my side ? Thks

saxet77 avatar Jul 22 '21 13:07 saxet77

I can walk you through it. Do you have any git experience? You won't need much but just means i know where to target things

Stuart-Moore avatar Jul 22 '21 14:07 Stuart-Moore

Is the fix in the dev branch? if so, it can be downloaded from https://dbatools.io/devzip

potatoqualitee avatar Jul 22 '21 19:07 potatoqualitee

Not yet. Worst case I can share a one drive link to a zip of the lsnbreak branch.

Sent from a mobile device so apologies for any spolling mistakes

On 22 Jul 2021, at 20:13, Chrissy LeMaire @.***> wrote:

ο»Ώ Is the fix in the dev branch? if so, it can be downloaded from https://dbatools.io/devzip

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Stuart-Moore avatar Jul 22 '21 20:07 Stuart-Moore

Ahh there's the branch! Here's the zip

https://github.com/sqlcollaborative/dbatools/archive/refs/heads/lsnbreak.zip

potatoqualitee avatar Jul 22 '21 20:07 potatoqualitee

Thks, got the zip file. tests in progress ... ;)

saxet77 avatar Jul 23 '21 04:07 saxet77

sweet! looking forward to hearing back

potatoqualitee avatar Jul 23 '21 05:07 potatoqualitee

Great, and I’ve learnt a new Github feature as well πŸ‘

Cheers Stuart On 23 Jul 2021, 06:49 +0100, Chrissy LeMaire @.***>, wrote:

sweet! looking forward to hearing back β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Stuart-Moore avatar Jul 23 '21 07:07 Stuart-Moore

Through the website, you can select the branch, then click on the green button and copy the link from Download zip πŸ‘πŸΌ

potatoqualitee avatar Jul 23 '21 07:07 potatoqualitee

hey @saxet77 - please close out powershell entirely then Import-module C:\wherever\to\the\dir\dbatools.psd1 -Force

without a force, it can look like the new module files are imported, but they arent.

potatoqualitee avatar Jul 23 '21 17:07 potatoqualitee

bummer. thank you for the files, tho. they may be able to help stuart πŸ’―

potatoqualitee avatar Jul 24 '21 14:07 potatoqualitee

Take 2. Would you mind taking this new version for a spin. It seems to work on the 'msdb' I've hacked together from the files, but need to know it works with a proper one please

https://github.com/sqlcollaborative/dbatools/archive/refs/heads/lsnbreak.zip

Stuart-Moore avatar Jul 27 '21 12:07 Stuart-Moore

Sorry ;( Same error message with this new version.

saxet77 avatar Jul 27 '21 15:07 saxet77

@Stuart-Moore Do you have an eye on this?

andreasjordan avatar Oct 30 '21 15:10 andreasjordan