diff --git a/public/Backup-DbaDatabase.ps1 b/public/Backup-DbaDatabase.ps1 index f7038b29a2..bf8a2d5d2f 100644 --- a/public/Backup-DbaDatabase.ps1 +++ b/public/Backup-DbaDatabase.ps1 @@ -54,6 +54,9 @@ function Backup-DbaDatabase { timestamp - will be replaced with the timestamp (either the default, or the format provided) backuptype - will be replaced with Full, Log or Differential as appropriate + .PARAMETER NoAppendDbNameInPath + A switch that will prevent to systematically appended dbname to the path when creating the backup file path + .PARAMETER CopyOnly If this switch is enabled, CopyOnly backups will be taken. By default function performs a normal backup, these backups interfere with the restore chain of the database. CopyOnly backups will not interfere with the restore chain of the database. @@ -217,6 +220,7 @@ function Backup-DbaDatabase { [string]$FilePath, [switch]$IncrementPrefix, [switch]$ReplaceInName, + [switch]$NoAppendDbNameInPath, [switch]$CopyOnly, [ValidateSet('Full', 'Log', 'Differential', 'Diff', 'Database')] [string]$Type = 'Database', @@ -661,7 +665,11 @@ function Backup-DbaDatabase { for ($i = 0; $i -lt $FinalBackupPath.Count; $i++) { $parent = [IO.Path]::GetDirectoryName($FinalBackupPath[$i]) $leaf = [IO.Path]::GetFileName($FinalBackupPath[$i]) - $FinalBackupPath[$i] = [IO.Path]::Combine($parent, $dbName, $leaf) + if ($NoAppendDbNameInPath) { + $FinalBackupPath[$i] = [IO.Path]::Combine($parent, $leaf) + } else { + $FinalBackupPath[$i] = [IO.Path]::Combine($parent, $dbName, $leaf) + } } } diff --git a/tests/Backup-DbaDatabase.Tests.ps1 b/tests/Backup-DbaDatabase.Tests.ps1 index e4b736b283..26889a9db6 100644 --- a/tests/Backup-DbaDatabase.Tests.ps1 +++ b/tests/Backup-DbaDatabase.Tests.ps1 @@ -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', 'Path', 'FilePath', 'ReplaceInName', 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', 'OutputScriptOnly', 'EnableException', 'EncryptionAlgorithm', 'EncryptionCertificate', 'IncrementPrefix', 'Description' + [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'Path', 'FilePath', 'ReplaceInName', 'NoAppendDbNameInPath', 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', 'OutputScriptOnly', 'EnableException', 'EncryptionAlgorithm', 'EncryptionCertificate', 'IncrementPrefix', 'Description' $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