dataplat / dbatools

🚀 SQL Server automation and instance migrations have never been safer, faster or freer
https://dbatools.io
MIT License
2.39k stars 787 forks source link

Backup-DbaDatabase : add a switch that will prevent to always append dbname in the directory backup path and strictly respect the given path variable. #9346

Open rferraton opened 1 month ago

rferraton commented 1 month ago

Summarize Functionality

For the command Backup-DbaDatabase, add a switch that will prevent to always append dbname in the directory backup path and strictly respect the given path variable.

Cause : Currently, Backup-DbaDatabase always appended the DbName in the backup path. This can be problematic in some cases where the backup path try to respect Ola Hallengren default path model and when the database name is long. In this case the current dbname appending lead to repeat the dbname twice in the backup path and can cause a "backup path too long" error.

Suggested switch name -NoAppendDbNameInPath Default value would remain false to avoid impact on the existing backups scripts

Is there a command that is similiar or close to what you are looking for?

Yes

Technical Details

line 664 in Backup-DbaDatabase.ps1 :

if ($CreateFolder -and $FinalBackupPath[0] -ne 'NUL:') {
                for ($i = 0; $i -lt $FinalBackupPath.Count; $i++) {
                    $parent = [IO.Path]::GetDirectoryName($FinalBackupPath[$i])
                    $leaf = [IO.Path]::GetFileName($FinalBackupPath[$i])
                    if ($NoAppendDbNameInPath) {
                        $FinalBackupPath[$i] = [IO.Path]::Combine($parent, $leaf)
                    } else {
                    $FinalBackupPath[$i] = [IO.Path]::Combine($parent, $dbName, $leaf)
                    }
                }
            }