Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Backup-DbaDatabase use Database and ExcludeDatabase when piping #5069

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions functions/Backup-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,19 @@ function Backup-DbaDatabase {
Write-Message -Message 'Setting Default timestampformat' -Level Verbose
$TimeStampFormat = "yyyyMMddHHmm"
}
if ($SqlInstance.length -ne 0) {

if ($SqlInstance) {
try {
$Server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AzureUnsupported
} catch {
Stop-Function -Message "Cannot connect to $SqlInstance" -ErrorRecord $_
return
}

$InputObject = $server.Databases | Where-Object Name -ne 'tempdb'

if ($Database) {
$InputObject = $server.Databases | Where-Object Name -in $Database
} else {
$InputObject = $server.Databases | Where-Object Name -ne 'tempdb'
$InputObject = $InputObject | Where-Object Name -in $Database
}

if ($ExcludeDatabase) {
Expand Down Expand Up @@ -280,13 +281,21 @@ function Backup-DbaDatabase {
}

process {
if (!$SqlInstance -and !$InputObject) {
if (-not $SqlInstance -and -not $InputObject) {
Stop-Function -Message "You must specify a server and database or pipe some databases"
return
}

Write-Message -Level Verbose -Message "$($InputObject.Count) database to backup"

if ($Database) {
$InputObject = $InputObject | Where-Object Name -in $Database
}

if ($ExcludeDatabase) {
$InputObject = $InputObject | Where-Object Name -notin $ExcludeDatabase
}

foreach ($db in $InputObject) {
$ProgressId = Get-Random
$failures = @()
Expand Down
22 changes: 21 additions & 1 deletion tests/Backup-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','SqlCredential','Database','ExcludeDatabase','BackupDirectory','BackupFileName','ReplaceInName','CopyOnly','Type','InputObject','CreateFolder','FileCount','CompressBackup','Checksum','Verify','MaxTransferSize','BlockSize','BufferCount','AzureBaseUrl','AzureCredential','NoRecovery','BuildPath','WithFormat','Initialize','SkipTapeHeader','TimeStampFormat','IgnoreFileChecks','OutputScriptOnly','EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'BackupDirectory', 'BackupFileName', 'ReplaceInName', 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', 'OutputScriptOnly', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down Expand Up @@ -75,6 +75,26 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}
}

Context "Database parameter works when using pipes (fixes #5044)" {
$results = Get-DbaDatabase -SqlInstance $script:instance1 | Backup-DbaDatabase -Database master -BackupFileName PesterTest.bak -BackupDirectory $DestBackupDir
It "Should report it has backed up to the path with the correct name" {
$results.Fullname | Should -BeLike "$DestBackupDir*PesterTest.bak"
}
It "Should have backed up to the path with the correct name" {
Test-Path "$DestBackupDir\PesterTest.bak" | Should -Be $true
}
}

Context "ExcludeDatabase parameter works when using pipes (fixes #5044)" {
$results = Get-DbaDatabase -SqlInstance $script:instance1 | Backup-DbaDatabase -ExcludeDatabase master, tempdb, msdb, model
It "Should report it has backed up to the path with the correct name" {
$results.DatabaseName | Should -Not -Contain master
$results.DatabaseName | Should -Not -Contain tempdb
$results.DatabaseName | Should -Not -Contain msdb
$results.DatabaseName | Should -Not -Contain model
}
}

Context "Handling backup paths that don't exist" {
$MissingPathTrailing = "$DestBackupDir\Missing1\Awol2\"
$MissingPath = "$DestBackupDir\Missing1\Awol2"
Expand Down