commands

^

Backup-DbaDatabase

Author Stuart Moore (@napalmgram), stuart-moore.com
Availability Windows, Linux, macOS

 

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

Synopsis

Backup one or more SQL Server databases from a single SQL Server SqlInstance.

Description

Performs a backup of a specified type of 1 or more databases on a single SQL Server Instance. These backups may be Full, Differential or Transaction log backups.

Syntax

Backup-DbaDatabase
    [-SqlCredential <PSCredential>]
    [-Database <Object[]>]
    [-ExcludeDatabase <Object[]>]
    [-Path <String[]>]
    [-FilePath <String>]
    [-IncrementPrefix]
    [-ReplaceInName]
    [-NoAppendDbNameInPath]
    [-CopyOnly]
    [-Type <String>]
    [-CreateFolder]
    [-FileCount <Int32>]
    [-CompressBackup]
    [-Checksum]
    [-Verify]
    [-MaxTransferSize <Int32>]
    [-BlockSize <Int32>]
    [-BufferCount <Int32>]
    [-AzureBaseUrl <String[]>]
    [-AzureCredential <String>]
    [-NoRecovery]
    [-BuildPath]
    [-WithFormat]
    [-Initialize]
    [-SkipTapeHeader]
    [-TimeStampFormat <String>]
    [-IgnoreFileChecks]
    [-OutputScriptOnly]
    [-EncryptionAlgorithm <String>]
    [-EncryptionCertificate <String>]
    [-Description <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Backup-DbaDatabase -SqlInstance <DbaInstanceParameter>
    [-SqlCredential <PSCredential>]
    [-Database <Object[]>]
    [-ExcludeDatabase <Object[]>]
    [-Path <String[]>]
    [-FilePath <String>]
    [-IncrementPrefix]
    [-ReplaceInName]
    [-NoAppendDbNameInPath]
    [-CopyOnly]
    [-Type <String>]
    [-CreateFolder]
    [-FileCount <Int32>]
    [-CompressBackup]
    [-Checksum]
    [-Verify]
    [-MaxTransferSize <Int32>]
    [-BlockSize <Int32>]
    [-BufferCount <Int32>]
    [-AzureBaseUrl <String[]>]
    [-AzureCredential <String>]
    [-NoRecovery]
    [-BuildPath]
    [-WithFormat]
    [-Initialize]
    [-SkipTapeHeader]
    [-TimeStampFormat <String>]
    [-IgnoreFileChecks]
    [-OutputScriptOnly]
    [-EncryptionAlgorithm <String>]
    [-EncryptionCertificate <String>]
    [-Description <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Backup-DbaDatabase
    [-SqlCredential <PSCredential>]
    [-Database <Object[]>]
    [-ExcludeDatabase <Object[]>]
    [-Path <String[]>]
    [-FilePath <String>]
    [-IncrementPrefix]
    [-ReplaceInName]
    [-NoAppendDbNameInPath]
    [-CopyOnly]
    [-Type <String>]
    -InputObject <Object[]>
    [-CreateFolder]
    [-FileCount <Int32>]
    [-CompressBackup]
    [-Checksum]
    [-Verify]
    [-MaxTransferSize <Int32>]
    [-BlockSize <Int32>]
    [-BufferCount <Int32>]
    [-AzureBaseUrl <String[]>]
    [-AzureCredential <String>]
    [-NoRecovery]
    [-BuildPath]
    [-WithFormat]
    [-Initialize]
    [-SkipTapeHeader]
    [-TimeStampFormat <String>]
    [-IgnoreFileChecks]
    [-OutputScriptOnly]
    [-EncryptionAlgorithm <String>]
    [-EncryptionCertificate <String>]
    [-Description <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Backup-DbaDatabase -SqlInstance Server1 -Database HR, Finance

This will perform a full database backup on the databases HR and Finance on SQL Server Instance Server1 to Server1 default backup directory.

Example: 2
PS C:\> Backup-DbaDatabase -SqlInstance sql2016 -Path C:\temp -Database AdventureWorks2014 -Type Full

Backs up AdventureWorks2014 to sql2016 C:\temp folder.

Example: 3
PS C:\> Backup-DbaDatabase -SqlInstance sql2016 -AzureBaseUrl https://dbatoolsaz.blob.core.windows.net/azbackups/ -AzureCredential dbatoolscred -Type Full -CreateFolder

Performs a full backup of all databases on the sql2016 instance to their own containers under the https://dbatoolsaz.blob.core.windows.net/azbackups/ container on Azure blob storage using the sql
credential "dbatoolscred" registered on the sql2016 instance.

Example: 4
PS C:\> Backup-DbaDatabase -SqlInstance sql2016 -AzureBaseUrl https://dbatoolsaz.blob.core.windows.net/azbackups/  -Type Full

Performs a full backup of all databases on the sql2016 instance to the https://dbatoolsaz.blob.core.windows.net/azbackups/ container on Azure blob storage using the Shared Access Signature sql
credential "https://dbatoolsaz.blob.core.windows.net/azbackups" registered on the sql2016 instance.

Example: 5
PS C:\> Backup-DbaDatabase -SqlInstance Server1\Prod -Database db1 -Path \\filestore\backups\servername\instancename\dbname\backuptype -Type Full -ReplaceInName

Performs a full backup of db1 into the folder \filestore\backups\server1\prod\db1\Full

Example: 6
PS C:\> Backup-DbaDatabase -SqlInstance Server1\Prod -Path \\filestore\backups\servername\instancename\dbname\backuptype -FilePath dbname-backuptype-timestamp.trn -Type Log -ReplaceInName

Performs a log backup for every database. For the database db1 this would results in backup files in \filestore\backups\server1\prod\db1\Log\db1-log-31102018.trn

Example: 7
PS C:\> Backup-DbaDatabase -SqlInstance Sql2017 -Database master -FilePath NUL

Performs a backup of master, but sends the output to the NUL device (ie; throws it away)

Example: 8
PS C:\> Backup-DbaDatabase -SqlInstance Sql2016 -Database stripetest -AzureBaseUrl https://az.blob.core.windows.net/sql,https://dbatools.blob.core.windows.net/sql

Performs a backup of the database stripetest, striping it across the 2 Azure blob containers at https://az.blob.core.windows.net/sql and https://dbatools.blob.core.windows.net/sql, assuming that
Shared Access Signature credentials for both containers exist on the source instance

Example: 9
PS C:\> Backup-DbaDatabase -SqlInstance Sql2017 -Database master -EncryptionAlgorithm AES256 -EncryptionCertificate BackupCert

Backs up the master database using the BackupCert certificate and the AES256 algorithm.

Required Parameters

-SqlInstance

The SQL Server instance hosting the databases to be backed up.

Alias
Required True
Pipeline false
Default Value
-InputObject

Internal parameter

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

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
-Path

Path in which to place the backup files. If not specified, the backups will be placed in the default backup location for SqlInstance.
If multiple paths are specified, the backups will be striped across these locations. This will overwrite the FileCount option.
If the path does not exist, Sql Server will attempt to create it. Folders are created by the Sql Instance, and checks will be made for write permissions.
File Names with be suffixed with x-of-y to enable identifying striped sets, where y is the number of files in the set and x ranges from 1 to y.

Alias BackupDirectory
Required False
Pipeline false
Default Value
-FilePath

The name of the file to backup to. This is only accepted for single database backups.
If no name is specified then the backup files will be named DatabaseName_yyyyMMddHHmm (i.e. "Database1_201714022131") with the appropriate extension.
If the same name is used repeatedly, SQL Server will add backups to the same file at an incrementing position.
SQL Server needs permissions to write to the specified location. Path names are based on the SQL Server (C:\ is the C drive on the SQL Server, not the machine running the script).
Passing in NUL as the FilePath will backup to the NUL: device

Alias BackupFileName
Required False
Pipeline false
Default Value
-IncrementPrefix

If enabled, this will prefix backup files with an incrementing integer (ie; '1-', '2-'). Using this has been alleged to improved restore times on some Azure based SQL Database platforms

Alias
Required False
Pipeline false
Default Value False
-ReplaceInName

If this switch is set, the following list of strings will be replaced in the FilePath and Path strings:
instancename - will be replaced with the instance Name
servername - will be replaced with the server name
dbname - will be replaced with the database name
timestamp - will be replaced with the timestamp (either the default, or the format provided)
backuptype - will be replaced with Full, Log or Differential as appropriate

Alias
Required False
Pipeline false
Default Value False
-NoAppendDbNameInPath

A switch that will prevent to systematically appended dbname to the path when creating the backup file path

Alias
Required False
Pipeline false
Default Value False
-CopyOnly

If this switch is enabled, CopyOnly backups will be taken. By default function performs a normal backup, these backups interfere with the restore chain of the database. CopyOnly backups will not
interfere with the restore chain of the database.
For more details please refer to this MSDN article - https://msdn.microsoft.com/en-us/library/ms191495.aspx

Alias
Required False
Pipeline false
Default Value False
-Type

The type of SQL Server backup to perform. Accepted values are "Full", "Log", "Differential", "Diff", "Database"

Alias
Required False
Pipeline false
Default Value Database
Accepted Values Full,Log,Differential,Diff,Database
-CreateFolder

If this switch is enabled, each database will be backed up into a separate folder on each of the paths specified by Path.

Alias
Required False
Pipeline false
Default Value False
-FileCount

This is the number of striped copies of the backups you wish to create. This value is overwritten if you specify multiple Backup Directories.

Alias
Required False
Pipeline false
Default Value 0
-CompressBackup

If this switch is enabled, the function will try to perform a compressed backup if supported by the version and edition of SQL Server. Otherwise, this function will use the server(s) default setting
for compression.
NOTE: Explicitly providing a value of false will disable backup compression.

Alias
Required False
Pipeline false
Default Value False
-Checksum

If this switch is enabled, the backup checksum will be calculated.

Alias
Required False
Pipeline false
Default Value False
-Verify

If this switch is enabled, the backup will be verified by running a RESTORE VERIFYONLY against the SqlInstance

Alias
Required False
Pipeline false
Default Value False
-MaxTransferSize

Sets the size of the unit of transfer. Values must be a multiple of 64kb.

Alias
Required False
Pipeline false
Default Value 0
-BlockSize

Specifies the block size to use. Must be one of 0.5KB, 1KB, 2KB, 4KB, 8KB, 16KB, 32KB or 64KB. This can be specified in bytes.
Refer to https://msdn.microsoft.com/en-us/library/ms178615.aspx for more detail

Alias
Required False
Pipeline false
Default Value 0
-BufferCount

Number of I/O buffers to use to perform the operation.
Refer to https://msdn.microsoft.com/en-us/library/ms178615.aspx for more detail

Alias
Required False
Pipeline false
Default Value 0
-AzureBaseUrl

The URL(s) to the base container of an Azure Storage account to write backups to.
If specifying the AzureCredential parameter you can only provide 1 value as page blobs do not support multiple URLs
If using Shared Access keys, you may specify as many URLs as you want, as long as a corresponding credential exists on the source server.
If specified, the only other parameters than can be used are "CopyOnly", "Type", "CompressBackup", "Checksum", "Verify", "AzureCredential", "CreateFolder".

Alias
Required False
Pipeline false
Default Value
-AzureCredential

The name of the credential on the SQL instance that can write to the AzureBaseUrl, only needed if using Storage access keys
If using SAS credentials, the command will look for a credential with a name matching the AzureBaseUrl. As page blobs are used with this option we force the number of files to 1 and ignore any value
passed in for BlockSize or MaxTransferSize

Alias
Required False
Pipeline false
Default Value
-NoRecovery

This is passed in to perform a tail log backup if needed

Alias
Required False
Pipeline false
Default Value False
-BuildPath

By default this command will not attempt to create missing paths, this switch will change the behaviour so that it will

Alias
Required False
Pipeline false
Default Value False
-WithFormat

Formats the media as the first step of the backup operation. NOTE: This will set Initialize and SkipTapeHeader to $true.

Alias
Required False
Pipeline false
Default Value False
-Initialize

Initializes the media as part of the backup operation.

Alias
Required False
Pipeline false
Default Value False
-SkipTapeHeader

Initializes the media as part of the backup operation.

Alias
Required False
Pipeline false
Default Value False
-TimeStampFormat

By default the command timestamps backups using the format yyyyMMddHHmm. Using this parameter this can be overridden. The timestamp format should be defined using the Get-Date formats, illegal
formats will cause an error to be thrown

Alias
Required False
Pipeline false
Default Value
-IgnoreFileChecks

This switch stops the function from checking for the validity of paths. This can be useful if SQL Server only has read access to the backup area.
Note, that as we cannot check the path you may well end up with errors.

Alias
Required False
Pipeline false
Default Value False
-OutputScriptOnly

Switch causes only the T-SQL script for the backup to be generated. Will not create any paths if they do not exist

Alias
Required False
Pipeline false
Default Value False
-EncryptionAlgorithm

Specified the Encryption Algorithm to used. Must be one of 'AES128','AES192','AES256' or 'TRIPLEDES'
Must specify one of EncryptionCertificate or EncryptionKey as well.

Alias
Required False
Pipeline false
Default Value
Accepted Values AES128,AES192,AES256,TRIPLEDES
-EncryptionCertificate

The name of the certificate to be used to encrypt the backups. The existence of the certificate will be checked, and will not proceed if it does not exist
Is mutually exclusive with the EncryptionKey option

Alias
Required False
Pipeline false
Default Value
-Description

The text describing the backup set like in BACKUP ... WITH DESCRITION = ''.

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
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

Alias cf
Required False
Pipeline false
Default Value