Get-DbaLastBackup
View SourceSynopsis
Retrieves last backup dates and times for database backup compliance monitoring
Description
Queries msdb backup history to retrieve the most recent full, differential, and transaction log backup dates for each database. This function helps DBAs quickly identify backup gaps and verify compliance with backup policies by showing when each backup type was last performed. The function also calculates elapsed time since each backup and provides status indicators to highlight potential issues, such as databases with no recent backups or transaction log backups that are overdue in full recovery model databases. Default output includes Server, Database, LastFullBackup, LastDiffBackup, and LastLogBackup columns.
Syntax
Get-DbaLastBackup
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[[-Database] <Object[]>]
[[-ExcludeDatabase] <Object[]>]
[-EnableException]
[<CommonParameters>]
Examples
Example: 1
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987
Returns a custom object with Server name, Database name, and the date the last time backups were performed.
Example: 2
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Select-Object *
Returns a custom object with Server name, Database name, and the date the last time backups were performed, and also recoverymodel and calculations on how long ago backups were taken and what the
status is.
Example: 3
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Select-Object * | Out-Gridview
Returns a gridview displaying ComputerName, InstanceName, SqlInstance, Database, RecoveryModel, LastFullBackup, LastDiffBackup, LastLogBackup, SinceFull, SinceDiff, SinceLog,
LastFullBackupIsCopyOnly, LastDiffBackupIsCopyOnly, LastLogBackupIsCopyOnly, DatabaseCreated, DaysSinceDbCreated, Status
Example: 4
PS C:\> $MyInstances | Get-DbaLastBackup | Where-Object -FilterScript { $_.LastFullBackup.Date -lt (Get-Date).AddDays(-3) } | Format-Table -Property SqlInstance, Database, LastFullBackup
Returns all databases on the given instances without a full backup in the last three days.
Note that the property LastFullBackup is a custom object, with the subproperty Date of type datetime and therefore suitable for comparison with dates.
Example: 5
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Where-Object { $_.LastFullBackupIsCopyOnly -eq $true }
Filters for the databases that had a copy_only full backup done as the last backup.
Required Parameters
-SqlInstance
The target SQL Server instance or instances.
| Property | Value |
|---|---|
| Alias | |
| Required | True |
| Pipeline | true (ByValue) |
| Default Value |
Optional Parameters
-SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Database
Specifies which databases to check for backup history. Accepts wildcards for pattern matching.
Use this when you need to focus on specific databases rather than scanning all databases on the instance.
Helpful for monitoring critical production databases or troubleshooting backup issues on particular databases.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-ExcludeDatabase
Excludes specific databases from the backup history check. Commonly used to skip system databases or test databases.
Use this when you want to check most databases but exclude certain ones like tempdb, development databases, or databases with known backup exemptions.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
Outputs
PSCustomObject
Returns one object per database processed, containing backup status information and compliance metrics.
Default display properties (via Select-DefaultView):
- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Database: The name of the database
- LastFullBackup: DateTime of the most recent full backup (DbaDateTime object with Date subproperty)
- LastDiffBackup: DateTime of the most recent differential backup (DbaDateTime object with Date subproperty)
- LastLogBackup: DateTime of the most recent transaction log backup (DbaDateTime object with Date subproperty)
Additional properties available (use Select-Object * to display):
- RecoveryModel: Database recovery model (Simple, Full, or BulkLogged)
- SinceFull: DbaTimeSpan representing time elapsed since last full backup; null if never backed up
- SinceDiff: DbaTimeSpan representing time elapsed since last differential backup; null if never backed up
- SinceLog: DbaTimeSpan representing time elapsed since last transaction log backup; null if never backed up
- LastFullBackupIsCopyOnly: Boolean indicating if the last full backup was copy-only
- LastDiffBackupIsCopyOnly: Boolean indicating if the last differential backup was copy-only (always false per SQL Server rules)
- LastLogBackupIsCopyOnly: Boolean indicating if the last transaction log backup was copy-only
- DatabaseCreated: DateTime when the database was created
- DaysSinceDbCreated: Integer number of days since database creation
- Status: String status indicator - “OK”, “New database, not backed up yet”, “No Full or Diff Back Up in the last day”, or “No Log Back Up in the last hour” The LastFullBackup, LastDiffBackup, and LastLogBackup properties are DbaDateTime objects that can be compared with .Date subproperty.
dbatools