commands

^

Export-DbaDacPackage

Author Richie lee (@richiebzzzt)
Availability Windows, Linux, macOS

 

Synopsis

Exports a dacpac from a server.

Description

Using SQLPackage, export a dacpac from an instance of SQL Server.

Note - Extract from SQL Server is notoriously flaky - for example if you have three part references to external databases it will not work.

For help with the extract action parameters and properties, refer to https://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx

Syntax

Export-DbaDacPackage -SqlInstance <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <Object[]>]
    [-ExcludeDatabase <Object[]>]
    [-AllUserDatabases]
    [-Path <String>]
    [-FilePath <String>]
    [-DacOption <Object>]
    [-Type <String>]
    [-Table <String[]>]
    [-EnableException]
    [<CommonParameters>]

Export-DbaDacPackage -SqlInstance <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <Object[]>]
    [-ExcludeDatabase <Object[]>]
    [-AllUserDatabases]
    [-Path <String>]
    [-FilePath <String>]
    [-ExtendedParameters <String>]
    [-ExtendedProperties <String>]
    [-Type <String>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Export-DbaDacPackage -SqlInstance sql2016 -Database SharePoint_Config -FilePath C:\SharePoint_Config.dacpac

Exports the dacpac for SharePoint_Config on sql2016 to C:\SharePoint_Config.dacpac

Example: 2
PS C:\> $options = New-DbaDacOption -Type Dacpac -Action Export
PS C:\> $options.ExtractAllTableData = $true
PS C:\> $options.CommandTimeout = 0
PS C:\> Export-DbaDacPackage -SqlInstance sql2016 -Database DB1 -DacOption $options

Uses DacOption object to set the CommandTimeout to 0 then extracts the dacpac for DB1 on sql2016 to C:\Users\username\Documents\DbatoolsExport\sql2016-DB1-20201227140759-dacpackage.dacpac including
all table data. As noted the generated filename will contain the server name, database name, and the current timestamp in the "%Y%m%d%H%M%S" format.

Example: 3
PS C:\> Export-DbaDacPackage -SqlInstance sql2016 -AllUserDatabases -ExcludeDatabase "DBMaintenance","DBMonitoring" -Path "C:\temp"

Exports dacpac packages for all USER databases, excluding "DBMaintenance" & "DBMonitoring", on sql2016 and saves them to C:\temp. The generated filename(s) will contain the server name, database
name, and the current timestamp in the "%Y%m%d%H%M%S" format.

Example: 4
PS C:\> $moreparams = "/OverwriteFiles:$true /Quiet:$true"
PS C:\> Export-DbaDacPackage -SqlInstance sql2016 -Database SharePoint_Config -Path C:\temp -ExtendedParameters $moreparams

Using extended parameters to over-write the files and performs the extraction in quiet mode to C:\temp\sql2016-SharePoint_Config-20201227140759-dacpackage.dacpac. Uses command line instead of SMO
behind the scenes. As noted the generated filename will contain the server name, database name, and the current timestamp in the "%Y%m%d%H%M%S" format.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Only SQL authentication is supported. When not specified, uses Trusted Authentication.

Alias
Required False
Pipeline false
Default Value
-Database

The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

The database(s) to exclude - this list is auto-populated from the server

Alias
Required False
Pipeline false
Default Value
-AllUserDatabases

Run command against all user databases

Alias
Required False
Pipeline false
Default Value False
-Path

Specifies the directory where the file or files will be exported.

Alias
Required False
Pipeline false
Default Value (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport')
-FilePath

Specifies the full file path of the output file.

Alias OutFile,FileName
Required False
Pipeline false
Default Value
-DacOption

Export options for a corresponding export type. Can be created by New-DbaDacOption -Type Dacpac | Bacpac

Alias ExtractOptions,ExportOptions,DacExtractOptions,DacExportOptions,Options,Option
Required False
Pipeline false
Default Value
-ExtendedParameters

Optional parameters used to extract the DACPAC. More information can be found at https://msdn.microsoft.com/en-us/library/hh550080.aspx

Alias
Required False
Pipeline false
Default Value
-ExtendedProperties

Optional properties used to extract the DACPAC. More information can be found at https://msdn.microsoft.com/en-us/library/hh550080.aspx

Alias
Required False
Pipeline false
Default Value
-Type

Selecting the type of the export: Dacpac (default) or Bacpac.

Alias
Required False
Pipeline false
Default Value Dacpac
Accepted Values Dacpac,Bacpac
-Table

List of the tables to include into the export. Should be provided as an array of strings: dbo.Table1, Table2, Schema1.Table3.

Alias
Required False
Pipeline false
Default Value
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False

 

Want to see the source code for this command? Check out Export-DbaDacPackage on GitHub.
Want to see the Bill Of Health for this command? Check out Export-DbaDacPackage.