Skip to content

Commit fe26c87

Browse files
Export-DbaInstance - Wire up IncludeDbMasterKey to export certs and master keys (#10251)
1 parent 6084ecb commit fe26c87

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

public/Export-DbaInstance.ps1

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ function Export-DbaInstance {
3232
All Availability Groups.
3333
All OLEDB Providers.
3434
35+
When -IncludeDbMasterKey is specified: all database certificates (exported as .cer files; private keys exported as .pvk files when -EncryptionPassword is provided) and all database master keys encrypted with the -EncryptionPassword.
36+
3537
The exported files are written to a folder using the naming convention "machinename$instance-yyyyMMddHHmmss", making it easy to identify the source instance and export timestamp.
3638
3739
This command is particularly valuable for:
@@ -77,13 +79,25 @@ function Export-DbaInstance {
7779
Required when generating restore scripts for databases backed up to Azure storage containers.
7880
7981
.PARAMETER IncludeDbMasterKey
80-
Exports database master keys from system databases and copies them to the export directory.
81-
Critical for environments using Transparent Data Encryption (TDE) or encrypted backups where master keys are required for restoration.
82+
When specified, exports database certificates (.cer files) and database master keys (.key files) to the export directory.
83+
Certificate private keys (.pvk files) are also exported when -EncryptionPassword is provided.
84+
Database master keys require -EncryptionPassword to be specified; if omitted, only certificates are exported.
85+
Use -Exclude DbCertificates to suppress certificate export while still exporting master keys.
86+
87+
.PARAMETER EncryptionPassword
88+
Secure password used to encrypt exported certificate private key files (.pvk) and database master key backups (.key).
89+
When specified with -IncludeDbMasterKey, enables export of private keys alongside certificates and also backs up database master keys.
90+
Required for database master key export; optional for certificate export (without it only .cer files are generated).
91+
92+
.PARAMETER DecryptionPassword
93+
Password required to decrypt the certificate's existing private key before it can be re-encrypted for backup.
94+
Use this when certificates were originally created with a password or imported from a password-protected source.
95+
Only applies when -IncludeDbMasterKey is specified and DbCertificates is not in -Exclude.
8296
8397
.PARAMETER Exclude
8498
Skips specific object types from the export to reduce scope or avoid problematic areas.
8599
Useful when you only need certain components or when specific features cause export issues in your environment.
86-
Valid values: Databases, Logins, AgentServer, Credentials, LinkedServers, SpConfigure, CentralManagementServer, DatabaseMail, SysDbUserObjects, SystemTriggers, BackupDevices, Audits, Endpoints, ExtendedEvents, PolicyManagement, ResourceGovernor, ServerAuditSpecifications, CustomErrors, ServerRoles, AvailabilityGroups, ReplicationSettings, OleDbProvider.
100+
Valid values: Databases, Logins, AgentServer, Credentials, LinkedServers, SpConfigure, CentralManagementServer, DatabaseMail, SysDbUserObjects, SystemTriggers, BackupDevices, Audits, Endpoints, ExtendedEvents, PolicyManagement, ResourceGovernor, ServerAuditSpecifications, CustomErrors, ServerRoles, AvailabilityGroups, ReplicationSettings, OleDbProvider, DbCertificates.
87101
88102
.PARAMETER BatchSeparator
89103
Defines the T-SQL batch separator used in generated scripts, defaults to "GO".
@@ -191,7 +205,9 @@ function Export-DbaInstance {
191205
[switch]$NoRecovery,
192206
[string]$AzureCredential,
193207
[switch]$IncludeDbMasterKey,
194-
[ValidateSet('AgentServer', 'Audits', 'AvailabilityGroups', 'BackupDevices', 'CentralManagementServer', 'Credentials', 'CustomErrors', 'DatabaseMail', 'Databases', 'Endpoints', 'ExtendedEvents', 'LinkedServers', 'Logins', 'PolicyManagement', 'ReplicationSettings', 'ResourceGovernor', 'ServerAuditSpecifications', 'ServerRoles', 'SpConfigure', 'SysDbUserObjects', 'SystemTriggers', 'OleDbProvider')]
208+
[Security.SecureString]$EncryptionPassword,
209+
[Security.SecureString]$DecryptionPassword,
210+
[ValidateSet('AgentServer', 'Audits', 'AvailabilityGroups', 'BackupDevices', 'CentralManagementServer', 'Credentials', 'CustomErrors', 'DatabaseMail', 'Databases', 'DbCertificates', 'Endpoints', 'ExtendedEvents', 'LinkedServers', 'Logins', 'PolicyManagement', 'ReplicationSettings', 'ResourceGovernor', 'ServerAuditSpecifications', 'ServerRoles', 'SpConfigure', 'SysDbUserObjects', 'SystemTriggers', 'OleDbProvider')]
195211
[string[]]$Exclude,
196212
[string]$BatchSeparator = (Get-DbatoolsConfigValue -FullName 'formatting.batchseparator'),
197213
[Microsoft.SqlServer.Management.Smo.ScriptingOptions]$ScriptingOption,
@@ -469,6 +485,37 @@ function Export-DbaInstance {
469485
$null = Get-DbaOleDbProvider -SqlInstance $server -WarningAction SilentlyContinue -EnableException:$EnableException | Export-DbaScript -FilePath "$exportPath\OleDbProvider.sql" -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix -ScriptingOptionsObject $ScriptingOption -EnableException:$EnableException
470486
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\oledbprovider.sql"
471487
}
488+
489+
if ($IncludeDbMasterKey -and $Exclude -notcontains 'DbCertificates') {
490+
Write-Message -Level Verbose -Message "Exporting database certificates"
491+
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting database certificates"
492+
$splatDbCert = @{
493+
SqlInstance = $server
494+
Path = $exportPath
495+
EnableException = $EnableException
496+
}
497+
if ($EncryptionPassword) {
498+
$splatDbCert["EncryptionPassword"] = $EncryptionPassword
499+
}
500+
if ($DecryptionPassword) {
501+
$splatDbCert["DecryptionPassword"] = $DecryptionPassword
502+
}
503+
Backup-DbaDbCertificate @splatDbCert
504+
}
505+
506+
if ($IncludeDbMasterKey -and $EncryptionPassword) {
507+
Write-Message -Level Verbose -Message "Exporting database master keys"
508+
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting database master keys"
509+
$splatMasterKey = @{
510+
SqlInstance = $server
511+
Path = $exportPath
512+
SecurePassword = $EncryptionPassword
513+
EnableException = $EnableException
514+
}
515+
Backup-DbaDbMasterKey @splatMasterKey
516+
} elseif ($IncludeDbMasterKey -and -not $EncryptionPassword) {
517+
Write-Message -Level Warning -Message "IncludeDbMasterKey was specified but no EncryptionPassword was provided. Skipping database master key export."
518+
}
472519
} catch {
473520
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
474521
}

tests/Export-DbaInstance.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Describe $CommandName -Tag UnitTests {
1717
"Path",
1818
"NoRecovery",
1919
"IncludeDbMasterKey",
20+
"EncryptionPassword",
21+
"DecryptionPassword",
2022
"Exclude",
2123
"BatchSeparator",
2224
"ScriptingOption",

0 commit comments

Comments
 (0)