dataplat / dbatools

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

Backup-DbaDatabase add database name in backup path even if dbname is already specified in path variable #9135

Open rferraton opened 11 months ago

rferraton commented 11 months ago

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

Not really an error message but when using a path variable and if you specified the dbname token in the path, then the path will have database twice : at the position of the token AND at the end of the path.

example :

when backup like that : $MSSQLInstances | Get-DbaDatabase -ExcludeDatabase "tempdb" | Backup-DbaDatabase -Type $BackupType -CompressBackup -Checksum -Verify -FileCount $FileCount -Path "${BackupDirectory}\servername\instancename\dbname\backuptype" -FilePath "servername_dbname_backuptype_timestamp.${BackupExtension}" -TimeStampFormat "yyyyMMdd_HHmm" -ReplaceInName -CreateFolder -WarningVariable WarningVariable -EnableException -Whatif

What if: Performing the operation "Creating a new path on MYMSSQL" on target "G:\BACKUPDB\MYMSSQL\MSSQLSERVER\ReportServer\Full\ReportServer". What if: Performing the operation "Backing up ReportServer to G:\BACKUPDB\MYMSSQL\MSSQLSERVER\ReportServer\Full\ReportServer\MYMSSQL_ReportServer_Full_20231025_1553.bak" on target "MYMSSQL".

Steps to Reproduce

# provide your command(s) executed pertaining to dbatools
# please include variable values (redacted or fake if needed) for reference

$BackupType = "FULL"
$FileCount = 1 
$CMSInstance="localhost\DBA01"
$BackupDirectory="G:\BACKUPDB"

    switch ( $BackupType ) 
        {
            "Full" 
                {
                    $BackupExtension="bak"  

                }
            "Diff" 
                {
                    $BackupExtension="bak" 

                }
            "Log"  
                {
                    $BackupExtension="trn"  

                }

        }

$MSSQLInstances = Get-DbaRegServer -SqlInstance $CMSInstance -Group ALL | Select-Object -Unique -ExpandProperty ServerName

$MSSQLInstances | Get-DbaDatabase -ExcludeDatabase "tempdb" | Backup-DbaDatabase -Type $BackupType -CompressBackup -Checksum -Verify -FileCount $FileCount -Path "${BackupDirectory}\servername\instancename\dbname\backuptype" -FilePath "servername_dbname_backuptype_timestamp.${BackupExtension}" -TimeStampFormat "yyyyMMdd_HHmm" -ReplaceInName -CreateFolder -WarningVariable WarningVariable -EnableException -Whatif

Please confirm that you are running the most recent version of dbatools

2.1.4

Other details or mentions

No response

What PowerShell host was used when producing this error

Windows PowerShell (powershell.exe)

PowerShell Host Version

Name Value


PSVersion 7.3.7 PSEdition Core GitCommitId 7.3.7 OS Microsoft Windows 10.0.17763 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

SQL Server Edition and Build number

Microsoft SQL Server 2019 (RTM-CU18) (KB5017593) - 15.0.4261.1 (X64) Sep 12 2022 15:07:06 Copyright (C) 2019 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2019 Standard 10.0 (Build 17763: ) (Hypervisor)

.NET Framework Version

.NET 7.0.11

andreasjordan commented 11 months ago

I can reproduce the issue, but I'm not sure how to solve it. The parameter "-CreateFolder" currently does two things:

I totally agree that in your case the second task should be done without the first one.

@Stuart-Moore You are the author of the command. Do you have an idea how to solve the issue?

rferraton commented 5 months ago

This problem lead to more frequent backup failures in some cases because of a path too long : "Invalid device name. The length of the device name provided exceeds supported limit (maximum length is:259 "

==> https://github.com/dataplat/dbatools/issues/9341

rferraton commented 5 months ago

I do understand your worries about impacts of such a change, why not adding a new parameter in the Backup-DbaDatabase that will avoid to create a dbname subdirectory : -NoDBSubdir for exemple ?