Skip to content

Commit

Permalink
Backup-DbaDbCertificate, new FileBaseName parameter (#9597)
Browse files Browse the repository at this point in the history
  • Loading branch information
niphlod authored Feb 24, 2025
1 parent 00aaf8d commit dcf00d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
19 changes: 13 additions & 6 deletions public/Backup-DbaDbCertificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function Backup-DbaDbCertificate {
.PARAMETER Suffix
The suffix of the filename of the exported certificate.
.PARAMETER FileBaseName
Override the default naming convention with a fixed name for the certificate and private key file name, useful when exporting a single certificate.
".cer" will be appended to the certificate file name and ".pvk" will be appended to the private key file name.
.PARAMETER InputObject
Enables piping from Get-DbaDbCertificate
Expand Down Expand Up @@ -131,6 +135,7 @@ function Backup-DbaDbCertificate {
[Security.SecureString]$DecryptionPassword,
[System.IO.FileInfo]$Path,
[string]$Suffix,
[string]$FileBaseName,
[parameter(ValueFromPipeline, ParameterSetName = "collection")]
[Microsoft.SqlServer.Management.Smo.Certificate[]]$InputObject,
[switch]$EnableException
Expand All @@ -140,7 +145,6 @@ function Backup-DbaDbCertificate {
if (-not $EncryptionPassword -and $DecryptionPassword) {
Stop-Function -Message "If you specify a decryption password, you must also specify an encryption password" -Target $DecryptionPassword
}
$time = Get-Date -Format yyyMMddHHmmss

function export-cert ($cert) {
$certName = $cert.Name
Expand All @@ -164,14 +168,21 @@ function Backup-DbaDbCertificate {
}

$fileinstance = $instance.ToString().Replace('\', '$')
$fullCertName = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath "$fileinstance-$dbname-$certName$Suffix"
$targetBaseName = "$fileinstance-$dbname-$certName$Suffix"
if ($FileBaseName) {
$targetBaseName = $FileBaseName
}
$fullCertName = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath $targetBaseName

# if the base file name exists, then default to old style of appending a timestamp
if (Test-DbaPath -SqlInstance $server -Path "$fullCertName.cer") {
if ($Suffix) {
Stop-Function -Message "$fullCertName.cer already exists on $($server.Name)" -Target $actualPath -Continue
} else {
$time = Get-Date -Format yyyyMMddHHmmss
$fullCertName = "$fullCertName-$time"
# Sleep for a second to avoid another export in the same second
Start-Sleep -Seconds 1
}
}

Expand All @@ -180,7 +191,6 @@ function Backup-DbaDbCertificate {
if ($Pscmdlet.ShouldProcess($instance, "Exporting certificate $certName from $db on $instance to $actualPath")) {
Write-Message -Level Verbose -Message "Exporting Certificate: $certName to $fullCertName"
try {

$exportPathCert = "$fullCertName.cer"

# because the password shouldn't go to memory...
Expand Down Expand Up @@ -208,9 +218,6 @@ function Backup-DbaDbCertificate {
$cert.export($exportPathCert)
}

# Sleep for a second to avoid another export in the same second
Start-Sleep -Seconds 1

[PSCustomObject]@{
ComputerName = $server.ComputerName
InstanceName = $server.ServiceName
Expand Down
11 changes: 11 additions & 0 deletions tests/Backup-DbaDbCertificate.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Describe "Backup-DbaDbCertificate" -Tag "UnitTests" {
"DecryptionPassword",
"Path",
"Suffix",
"FileBaseName",
"InputObject",
"EnableException",
"Confirm",
Expand Down Expand Up @@ -63,6 +64,15 @@ Describe "Backup-DbaDbCertificate" -Tag "IntegrationTests" {
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $db1Name).ID
}

It "backs up the db cert with a filename (see #9485)" {
$results = Backup-DbaDbCertificate -SqlInstance $TestConfig.instance1 -Certificate $cert.Name -Database $db1Name -EncryptionPassword $pw -DecryptionPassword $pw -FileBaseName "dbatoolscli_cert1_$random"
$results.Certificate | Should -Be $cert.Name
$results.Status | Should -Match "Success"
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $db1Name).ID
[IO.Path]::GetFileNameWithoutExtension($results.Path) | Should -Be "dbatoolscli_cert1_$random"
$null = Get-ChildItem -Path $results.Path -ErrorAction Ignore | Remove-Item -Confirm:$false -ErrorAction Ignore
}

It "warns the caller if the cert cannot be found" {
$invalidDBCertName = "dbatoolscli_invalidCertName"
$invalidDBCertName2 = "dbatoolscli_invalidCertName2"
Expand All @@ -82,5 +92,6 @@ Describe "Backup-DbaDbCertificate" -Tag "IntegrationTests" {
$results = Backup-DbaDbCertificate -SqlInstance $TestConfig.instance1 -EncryptionPassword $pw
$null = Get-ChildItem -Path $results.Path -ErrorAction Ignore | Remove-Item -Confirm:$false -ErrorAction Ignore
}

}
}

0 comments on commit dcf00d7

Please sign in to comment.