Closed kmramsey closed 1 year ago
I've created a better script that helps uncover the problem.
$MyDatabase = "YourDatabase"
Get-Dbadatabase -SqlInstance $Myinstance -Database $MyDatabase
Get-DbaBackupHistory -SqlInstance $Myinstance -Database $MyDatabase -Verbose:$false -WarningAction SilentlyContinue -LastFull -IncludeCopyOnly | Select Database, Type, START, END, path | ft
Get-DbaBackupHistory -SqlInstance $Myinstance -Database $MyDatabase -Verbose:$false -WarningAction SilentlyContinue -LastLog | Select Database, Type, START, END, path | ft
Get-DbaBackupHistory -SqlInstance $Myinstance -Database $MyDatabase -Verbose:$false -WarningAction SilentlyContinue -LastDiff | Select Database, Type, START, END, path | ft
$queryDatabaseBackupInfo1=
"---------------------------------------------------------------------------------
--FULL Database Backups for all databases For Previous Week
---------------------------------------------------------------------------------
SELECT top 1
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date,
msdb.dbo.backupmediafamily.physical_device_name
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7)
and database_name = '$MyDatabase'
and msdb.dbo.backupset.name = '$MyDatabase - Full Backup'
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date desc
"
$queryDatabaseBackupInfo2 =
"
SELECT top 1
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date,
msdb.dbo.backupmediafamily.physical_device_name
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7)
and database_name = '$MyDatabase'
and msdb.dbo.backupset.name = '$MyDatabase - Diff Backup'
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date desc
"
$queryDatabaseBackupInfo3 =
"
SELECT top 1
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date,
msdb.dbo.backupmediafamily.physical_device_name
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7)
and database_name = '$MyDatabase'
and msdb.dbo.backupset.name = '$MyDatabase - Log Backup'
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date desc
"
Invoke-DbaQuery -SqlInstance $Myinstance -Database msdb -Query $queryDatabaseBackupInfo1
Invoke-DbaQuery -SqlInstance $Myinstance -Database msdb -Query $queryDatabaseBackupInfo2
Invoke-DbaQuery -SqlInstance $Myinstance -Database msdb -Query $queryDatabaseBackupInfo3
we order by lsns, not finish date .... sure those "inconsistent result" are not coming from forks ?
Forked: I don't use forked versions of dbatools.
Oder by: With respect, I'm not sure what your order by comment. When I ask for the last log, or last diff, or last full, it should get you the actual LAST full, diff or log, right?
forks as in "backup forks". We do have a "long standing" issue that seems to come up over and over with the way we order things..... as for "I ask for the last", be aware that is not always that easy to go for finish_date, so we use a more "strict" approach ordering by LSNs .... Problem is that LSNs unconveniently are not reset for backup forks... which in turn makes the order "screwed" for those databases which have a recorded history of a backup fork ;-(
Do you have any TSQL or Powershell that would help me know if that scenario is happening?
I don't think this should be stale.
Pretty please keep this issue alive.
We need to see the LSNs of those backups because as stated, we do not go by dates on the backups in our commands. We look at whether forks occurred in the backups of any given database:
You guys understand this better than I do. Are you saying this ticket is not needed? Do you need me to give you something to investigate?
Run the query below on a database you have that is showing "bad" information. If you get any results back that is why the results from our command are going to be inconsistent.
SELECT
database_name,
MIN(database_backup_lsn) as 'FirstLsn',
MAX(database_backup_lsn) as 'FinalLsn',
MIN(backup_start_date) as 'MinDate',
MAX(backup_finish_date) as 'MaxDate',
last_recovery_fork_guid 'RecFork',
count(1) as 'backupcount'
FROM msdb.dbo.backupset
WHERE database_name='<your database name here>'
GROUP by database_name, last_recovery_fork_guid
ORDER by MaxDate Asc
keep alive
This is not going to be something we can resolve without access to a system that shows the issue.
If someone wants to provide a data dump of all the backup history for thier environment, or a backup of the msdb database itself would be appreciated.
mayyyybe what we can do, since a number of people have a concern, is offer an -OrderBy
parameter. We default to LSN but then maybe if we encounter the chain issue X times, we write in Verbose "try OrderBy LastDate
if you continue to encounter this issue.
I get that fork problem so often during demos, i'd probably just set OrderBy LastDate in my PSDefaultParameterValues
It would be useful if we could select which LSN it is looking at. We currently have it reporting a wrong backup as being the most recent as it is ordering by the FirstLSN field, if it sorted on either the DatabaseBackupLSN or LastLSN it would be correct in our situation.
Let's link #8604 here as this pr should help so solve this issue.
But the linked pr only changes Get-DbaAgBackupHistory - so do we want this for Get-DbaBackupHistory as well?
LSNSort in the backup history? Yes, that'd be awesome, @andreasjordan
dope, this is closed as fixed! will be releasd in 2.0
There has been no fix to this placed in the Get-DbaDbBackupHistory
command, the linked PR is for the AG backup history command.
oh, thanks Shawn. I'm pretty much down to skipping many of these as a requirement for 2.0 and docs will be the final requirement.
i dont understand how to fix it but if anyone else wants to, i imagine dbatools 2.0 will be GA within 30 days.
Wait, this was fixed? I truncated the code a lil bit to show
https://github.com/dataplat/dbatools/blob/development/public/Get-DbaDbBackupHistory.ps1#L179
Environmental data
SQL Server:
Steps to Reproduce
Expected Behavior
Correct date of last backups
Actual Behavior
Inconsistent results. When I run these commands for 150 databases, I get 4-6 databases with incorrect backup information. When I isolate the ones with incorrect info, its consistently incorrrect. When pick good databases, its consistently correct. In other words, its not randomly inconsistent.