commands

^

Get-DbaBackupInformation

Author Chrissy LeMaire (@cl) , Stuart Moore (@napalmgram)
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Get-DbaBackupInformation on GitHub.
Want to see the Bill Of Health for this command? Check out Get-DbaBackupInformation.

Synopsis

Scan backup files and creates a set, compatible with Restore-DbaDatabase

Description

Upon being passed a list of potential backups files this command will scan the files, select those that contain SQL Server
backup sets. It will then filter those files down to a set

The function defaults to working on a remote instance. This means that all paths passed in must be relative to the remote instance.
XpDirTree will be used to perform the file scans

Various means can be used to pass in a list of files to be considered. The default is to non recursively scan the folder
passed in.

Syntax

Get-DbaBackupInformation -Path <Object[]> -SqlInstance <DbaInstanceParameter>
    [-SqlCredential <PSCredential>]
    [-DatabaseName <String[]>]
    [-SourceInstance <String[]>]
    [-NoXpDirTree]
    [-NoXpDirRecurse]
    [-DirectoryRecurse]
    [-EnableException]
    [-MaintenanceSolution]
    [-IgnoreLogBackup]
    [-IgnoreDiffBackup]
    [-ExportPath <String>]
    [-AzureCredential <String>]
    [-Anonymise]
    [-NoClobber]
    [-PassThru]
    [<CommonParameters>]

Get-DbaBackupInformation -Path <Object[]>
    [-DatabaseName <String[]>]
    [-SourceInstance <String[]>]
    [-EnableException]
    [-MaintenanceSolution]
    [-IgnoreLogBackup]
    [-IgnoreDiffBackup]
    [-ExportPath <String>]
    [-AzureCredential <String>]
    [-Import]
    [-Anonymise]
    [-NoClobber]
    [-PassThru]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaBackupInformation -SqlInstance Server1 -Path c:\backups\ -DirectoryRecurse

Will use the Server1 instance to recursively read all backup files under c:\backups, and return a dbatools BackupHistory object

Example: 2
PS C:\> Get-DbaBackupInformation -SqlInstance Server1 -Path c:\backups\ -DirectoryRecurse -ExportPath c:\store\BackupHistory.xml
PS C:\> robocopy c:\store\ \\remoteMachine\C$\store\ BackupHistory.xml
PS C:\> Get-DbaBackupInformation -Import -Path  c:\store\BackupHistory.xml | Restore-DbaDatabase -SqlInstance Server2 -TrustDbBackupHistory

This example creates backup history output from server1 and copies the file to the remote machine in order to preserve backup history. It is then used to restore the databases onto server2.

Example: 3
PS C:\> Get-DbaBackupInformation -SqlInstance Server1 -Path c:\backups\ -DirectoryRecurse -ExportPath C:\store\BackupHistory.xml -PassThru | Restore-DbaDatabase -SqlInstance Server2

-TrustDbBackupHistory
In this example we gather backup information, export it to an xml file, and then pass it on through to Restore-DbaDatabase.
This allows us to repeat the restore without having to scan all the backup files again

Example: 4
PS C:\> Get-ChildItem c:\backups\ -recurse -files | Where-Object {$_.extension -in ('.bak','.trn') -and $_.LastWriteTime -gt (get-date).AddMonths(-1)} | Get-DbaBackupInformation -SqlInstance Server1

-ExportPath C:\backupHistory.xml
This lets you keep a record of all backup history from the last month on hand to speed up refreshes

Example: 5
PS C:\> $Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups
PS C:\> $Backups += Get-DbaBackupInformation -SqlInstance Server2 -NoXpDirTree -Path c:\backups

Scan the unc folder \network\backups with Server1, and then scan the C:\backups folder on
Server2 not using xp_dirtree, adding the results to the first set.

Example: 6
PS C:\> $Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups -MaintenanceSolution

When MaintenanceSolution is indicated we know we are dealing with the output from Ola Hallengren backup scripts. So we make sure that a FULL folder exists in the first level of Path, if not we
shortcut scanning all the files as we have nothing to work with

Example: 7
PS C:\> $Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups -MaintenanceSolution -IgnoreLogBackup

As we know we are dealing with an Ola Hallengren style backup folder from the MaintenanceSolution switch, when IgnoreLogBackup is also included we can ignore the LOG folder to skip any scanning of
log backups. Note this also means they WON'T be restored

Required Parameters

-Path

Path to SQL Server backup files.
Paths passed in as strings will be scanned using the desired method, default is a non recursive folder scan
Accepts multiple paths separated by ','
Or it can consist of FileInfo objects, such as the output of Get-ChildItem or Get-Item. This allows you to work with
your own file structures as needed

Alias
Required True
Pipeline true (ByValue)
Default Value
-SqlInstance

The SQL Server instance to be used to read the headers of the backup files

Alias
Required True
Pipeline false
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.

Alias
Required False
Pipeline false
Default Value
-DatabaseName

An array of Database Names to filter by. If empty all databases are returned.

Alias
Required False
Pipeline false
Default Value
-SourceInstance

If provided only backup originating from this destination will be returned. This SQL instance will not be connected to or involved in this work

Alias
Required False
Pipeline false
Default Value
-NoXpDirTree

If specified, this switch will cause the files to be parsed as local files to the SQL Server Instance provided. Errors may be observed when the SQL Server Instance cannot access the files being
parsed.

Alias
Required False
Pipeline false
Default Value False
-NoXpDirRecurse

If specified, this switch changes xp_dirtree behavior to not recurse the folder structure.

Alias
Required False
Pipeline false
Default Value False
-DirectoryRecurse

If specified the provided path/directory will be traversed (only applies if not using XpDirTree)

Alias
Required False
Pipeline false
Default Value False
-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.

Alias
Required False
Pipeline false
Default Value False
-MaintenanceSolution

This switch tells the function that the folder is the root of a Ola Hallengren backup folder

Alias
Required False
Pipeline false
Default Value False
-IgnoreLogBackup

This switch only works with the MaintenanceSolution switch. With an Ola Hallengren style backup we can be sure that the LOG folder contains only log backups and skip it.
For all other scenarios we need to read the file headers to be sure.

Alias
Required False
Pipeline false
Default Value False
-IgnoreDiffBackup

This switch only works with the MaintenanceSolution switch. With an Ola Hallengren style backup we can be sure that the DIFF folder contains only differential backups and skip it.
For all other scenarios we need to read the file headers to be sure.

Alias
Required False
Pipeline false
Default Value False
-ExportPath

If specified the output will export via CliXml format to the specified file. This allows you to store the backup history object for later usage, or move it between computers

Alias
Required False
Pipeline false
Default Value
-AzureCredential

The name of the SQL Server credential to be used if restoring from an Azure hosted backup

Alias
Required False
Pipeline false
Default Value
-Import

When specified along with a path the command will import a previously exported BackupHistory object from an xml file.

Alias
Required False
Pipeline false
Default Value False
-Anonymise

If specified we will output the results with ComputerName, InstanceName, Database, UserName, Paths, and Logical and Physical Names hashed out
This options is mainly for use if we need you to submit details for fault finding to the dbatools team

Alias Anonymize
Required False
Pipeline false
Default Value False
-NoClobber

If specified will stop Export from overwriting an existing file, the default is to overwrite

Alias
Required False
Pipeline false
Default Value False
-PassThru

When data is exported the cmdlet will return no other output, this switch means it will also return the normal output which can be then piped into another command

Alias
Required False
Pipeline false
Default Value False