Skip to content

Commit 204954e

Browse files
Backup-DbaDatabase - Improve error messages for mapped drive path failures
When SQL Server cannot access a drive-letter path (mapped drive), explain that net use mappings are per user session and not visible to the SQL Server service account, and suggest using a UNC path instead. Also detect OS Error 3 in backup failures and surface the same guidance in the catch block. Fixes #10366 (do Backup-DbaDatabase) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 0e980fa commit 204954e

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

public/Backup-DbaDatabase.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function Backup-DbaDatabase {
163163
Skips path validation checks before backup operations, useful when SQL Server has limited filesystem access.
164164
Bypasses safety checks that normally prevent backup failures due to permissions or missing paths.
165165
Use with caution as it may result in backup failures that could have been prevented.
166+
Note: This does not help when using mapped drives (e.g. net use). Mapped drives are only visible to the user session that created them, not to the SQL Server service account. Use UNC paths (e.g. \\server\share\path) for network locations.
166167
167168
.PARAMETER OutputScriptOnly
168169
Generates and returns the T-SQL BACKUP commands without executing them.
@@ -918,7 +919,11 @@ function Backup-DbaDatabase {
918919
if (($BuildPath -eq $true) -or ($CreateFolder -eq $True)) {
919920
$null = New-DbaDirectory -SqlInstance $server -Path $parentPath
920921
} else {
921-
$failreason += "SQL Server cannot check if $parentPath exists. You can try disabling this check with -IgnoreFileChecks"
922+
if ($parentPath -match "^[A-Za-z]:\\") {
923+
$failreason += "SQL Server cannot access $parentPath. Drive letter paths (e.g. $($parentPath.Substring(0,2))) are mapped per user session and are not visible to the SQL Server service account. Use a UNC path instead (e.g. \\server\share\path)."
924+
} else {
925+
$failreason += "SQL Server cannot access $parentPath. Verify that the SQL Server service account has read/write permissions to this path. You can bypass this check with -IgnoreFileChecks, but the backup will still fail if SQL Server cannot access the path."
926+
}
922927
$failures += $failreason
923928
Write-Message -Level Warning -Message "$failreason"
924929
}
@@ -1072,12 +1077,16 @@ function Backup-DbaDatabase {
10721077
}
10731078
}
10741079
} catch {
1075-
if ($NoRecovery -and ($_.Exception.InnerException.InnerException.InnerException -like '*cannot be opened. It is in the middle of a restore.')) {
1080+
if ($NoRecovery -and ($_.Exception.InnerException.InnerException.InnerException -like "*cannot be opened. It is in the middle of a restore.")) {
10761081
Write-Message -Message "Exception thrown by db going into restoring mode due to recovery" -Level Verbose
10771082
} else {
10781083
Write-Progress -Id $ProgressId -Activity "Backup" -Completed
10791084
Write-Progress -Id $topProgressId -Activity "Backup" -Completed
1080-
Stop-Function -message "Backup of [$dbName] failed" -ErrorRecord $_ -Target $dbName -Continue
1085+
$errMsg = "Backup of [$dbName] failed"
1086+
if ($_.Exception.Message -like "*Operating system error 3*" -or $_.Exception.InnerException.Message -like "*Operating system error 3*") {
1087+
$errMsg += " | The SQL Server service account cannot find the backup path. If you used a mapped drive (e.g. net use), use a UNC path instead (e.g. \\server\share\path), as mapped drives are not visible to the SQL Server service account. Run Test-DbaPath -SqlInstance $($server.Name) -Path `"$humanBackupFile`" to verify accessibility from SQL Server."
1088+
}
1089+
Stop-Function -message $errMsg -ErrorRecord $_ -Target $dbName -Continue
10811090
$BackupComplete = $false
10821091
}
10831092
}

0 commit comments

Comments
 (0)