Skip to content

Commit 978b97c

Browse files
authored
Backup-DbaDbMasterKey, add FileBaseName (#9599)
1 parent 4cec791 commit 978b97c

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

public/Backup-DbaDbMasterKey.ps1

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ function Backup-DbaDbMasterKey {
1616
1717
For MFA support, please use Connect-DbaInstance.
1818
19+
.PARAMETER Credential
20+
Pass a credential object for the password
21+
1922
.PARAMETER Database
2023
Backup master key from specific database(s).
2124
2225
.PARAMETER ExcludeDatabase
2326
The database(s) to exclude - this list is auto-populated from the server.
2427
28+
.PARAMETER SecurePassword
29+
The password to encrypt the exported key. This must be a SecureString.
30+
2531
.PARAMETER Path
2632
The directory to export the key. If no path is specified, the default backup directory for the instance will be used.
2733
28-
.PARAMETER Credential
29-
Pass a credential object for the password
30-
31-
.PARAMETER SecurePassword
32-
The password to encrypt the exported key. This must be a SecureString.
34+
.PARAMETER FileBaseName
35+
Override the default naming convention with a fixed name for the database master key, useful when exporting a single one.
36+
".key" will be appended to the filename.
3337
3438
.PARAMETER InputObject
3539
Database object piped in from Get-DbaDatabase
@@ -82,6 +86,7 @@ function Backup-DbaDbMasterKey {
8286
[Alias("Password")]
8387
[Security.SecureString]$SecurePassword,
8488
[string]$Path,
89+
[string]$FileBaseName,
8590
[parameter(ValueFromPipeline)]
8691
[Microsoft.SqlServer.Management.Smo.Database[]]$InputObject,
8792
[switch]$EnableException
@@ -90,7 +95,6 @@ function Backup-DbaDbMasterKey {
9095
if ($Credential) {
9196
$SecurePassword = $Credential.Password
9297
}
93-
$time = Get-Date -Format yyyMMddHHmmss
9498
}
9599
process {
96100
foreach ($instance in $SqlInstance) {
@@ -144,31 +148,36 @@ function Backup-DbaDbMasterKey {
144148
}
145149

146150
$fileinstance = $instance.ToString().Replace('\', '$')
147-
$filename = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath "$fileinstance-$dbname-masterkey.key"
151+
$targetBaseName = "$fileinstance-$dbname-masterkey"
152+
if ($FileBaseName) {
153+
$targetBaseName = $FileBaseName
154+
}
155+
156+
$exportFileName = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath "$targetBaseName.key"
148157

149158
# if the base file name exists, then default to old style of appending a timestamp
150-
if (Test-DbaPath -SqlInstance $server -Path $filename) {
151-
$filename = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath "$fileinstance-$dbname-masterkey-$time.key"
159+
if (Test-DbaPath -SqlInstance $server -Path $exportFileName) {
160+
$time = Get-Date -Format yyyMMddHHmmss
161+
$exportFileName = Join-DbaPath -SqlInstance $server -Path $actualPath -ChildPath "$targetBaseName-$time.key"
162+
# Sleep for a second to avoid another export in the same second
163+
Start-Sleep -Seconds 1
152164
}
153165

154-
if ($Pscmdlet.ShouldProcess($instance, "Backing up master key to $filename")) {
166+
if ($Pscmdlet.ShouldProcess($instance, "Backing up master key to $exportFileName")) {
155167
try {
156-
$masterkey.Export($filename, ($SecurePassword | ConvertFrom-SecurePass))
168+
$masterkey.Export($exportFileName, ($SecurePassword | ConvertFrom-SecurePass))
157169
$status = "Success"
158170
} catch {
159171
$status = "Failure"
160172
Write-Message -Level Warning -Message "Backup failure: $($_.Exception.InnerException)"
161173
}
162174

163-
# Sleep for a second to avoid another export in the same second
164-
Start-Sleep -Seconds 1
165-
166175
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
167176
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
168177
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
169178
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Database -value $dbName
170179
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name DatabaseID -value $db.ID
171-
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Filename -value $filename
180+
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Filename -value $exportFileName
172181
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Status -value $status
173182

174183
Select-DefaultView -InputObject $masterkey -Property ComputerName, InstanceName, SqlInstance, Database, 'Filename as Path', Status

tests/Backup-DbaDbMasterKey.Tests.ps1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Describe "Backup-DbaDbMasterKey" -Tag "UnitTests" {
1717
"ExcludeDatabase",
1818
"SecurePassword",
1919
"Path",
20+
"FileBaseName",
2021
"InputObject",
2122
"EnableException",
2223
"WhatIf",
@@ -45,27 +46,45 @@ Describe "Backup-DbaDbMasterKey" -Tag "IntegrationTests" {
4546
if (-not (Get-DbaDbMasterKey -SqlInstance $instance -Database $database)) {
4647
$null = New-DbaDbMasterKey -SqlInstance $instance -Database $database -Password $password -Confirm:$false
4748
}
49+
}
50+
51+
AfterAll {
52+
Get-DbaDbMasterKey -SqlInstance $instance -Database $database | Remove-DbaDbMasterKey -Confirm:$false
53+
}
4854

55+
It "Backs up the database master key" {
4956
$splatBackup = @{
5057
SqlInstance = $instance
5158
Database = $database
5259
SecurePassword = $password
5360
Confirm = $false
5461
}
55-
}
62+
$results = Backup-DbaDbMasterKey @splatBackup
63+
$results | Should -Not -BeNullOrEmpty
64+
$results.Database | Should -Be $database
65+
$results.Status | Should -Be "Success"
66+
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $instance -Database $database).ID
5667

57-
AfterAll {
58-
Get-DbaDbMasterKey -SqlInstance $instance -Database $database | Remove-DbaDbMasterKey -Confirm:$false
68+
$null = Remove-Item -Path $results.Path -ErrorAction SilentlyContinue -Confirm:$false
5969
}
6070

61-
It "Backs up the database master key" {
71+
It "Backs up the database master key with a specific filename (see #9484)" {
72+
$random = Get-Random
73+
$splatBackup = @{
74+
SqlInstance = $instance
75+
Database = $database
76+
SecurePassword = $password
77+
FileBaseName = "dbatoolscli_dbmasterkey_$random"
78+
Confirm = $false
79+
}
6280
$results = Backup-DbaDbMasterKey @splatBackup
6381
$results | Should -Not -BeNullOrEmpty
6482
$results.Database | Should -Be $database
6583
$results.Status | Should -Be "Success"
6684
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $instance -Database $database).ID
67-
85+
[IO.Path]::GetFileNameWithoutExtension($results.Path) | Should -Be "dbatoolscli_dbmasterkey_$random"
6886
$null = Remove-Item -Path $results.Path -ErrorAction SilentlyContinue -Confirm:$false
6987
}
88+
7089
}
7190
}

0 commit comments

Comments
 (0)