Backup‑SqlDscDatabase - dsccommunity/SqlServerDsc GitHub Wiki

SYNOPSIS

Performs a backup of a SQL Server database.

SYNTAX

ServerObject (Default)

Backup-SqlDscDatabase -ServerObject <Server> -Name <String> -BackupFile <String> [-BackupType <String>]
 [-CopyOnly] [-Compress] [-Checksum] [-Description <String>] [-RetainDays <Int32>] [-Initialize] [-Refresh]
 [-Force] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

DatabaseObject

Backup-SqlDscDatabase -DatabaseObject <Database> -BackupFile <String> [-BackupType <String>] [-CopyOnly]
 [-Compress] [-Checksum] [-Description <String>] [-RetainDays <Int32>] [-Initialize] [-Force] [-PassThru]
 [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

This command performs a backup of a SQL Server database using SQL Server Management Objects (SMO). It supports full, differential, and transaction log backups with options for compression, verification, and copy-only backups.

EXAMPLES

EXAMPLE 1

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase.bak'

Performs a full backup of the database named MyDatabase to the specified backup file.

EXAMPLE 2

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase'
$databaseObject | Backup-SqlDscDatabase -BackupFile 'C:\Backups\MyDatabase.bak' -Force

Performs a full backup of the database named MyDatabase using a database object from the pipeline, without prompting for confirmation.

EXAMPLE 3

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase_Diff.bak' -BackupType 'Differential'

Performs a differential backup of the database named MyDatabase.

EXAMPLE 4

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase.trn' -BackupType 'Log'

Performs a transaction log backup of the database named MyDatabase.

EXAMPLE 5

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase_CopyOnly.bak' -CopyOnly

Performs a copy-only full backup of the database named MyDatabase. This backup does not affect the normal backup chain.

EXAMPLE 6

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase.bak' -Compress -Checksum

Performs a compressed full backup with checksum verification of the database named MyDatabase.

EXAMPLE 7

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$databaseObject = $serverObject | Backup-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase.bak' -PassThru -Force
$databaseObject | Set-SqlDscDatabaseProperty -RecoveryModel 'Simple' -Force

Performs a full backup of the database named MyDatabase and returns the database object for further pipeline operations.

PARAMETERS

-BackupFile

Specifies the full path to the backup file. For full and differential backups, use the .bak extension. For transaction log backups, use the .trn extension.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-BackupType

Specifies the type of backup to perform. Valid values are 'Full', 'Differential', and 'Log'. Default value is 'Full'.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Full
Accept pipeline input: False
Accept wildcard characters: False

-Checksum

Specifies that checksums should be calculated and verified during the backup operation to help detect backup media errors.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Compress

Specifies that the backup should be compressed. Backup compression requires SQL Server 2008 Enterprise or later, or SQL Server 2008 R2 Standard or later.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-CopyOnly

Specifies that a copy-only backup should be performed. Copy-only backups do not affect the sequence of conventional backups and are useful for taking backups for special purposes without disrupting the normal backup chain. This is particularly useful in AlwaysOn Availability Group scenarios.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-DatabaseObject

Specifies a database object to backup.

Type: Database
Parameter Sets: DatabaseObject
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-Description

Specifies a description for the backup set. This description is stored in the backup media and can be useful for identifying backups.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Force

Specifies that the backup should be performed without any confirmation.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Initialize

Specifies that the backup media should be initialized (overwritten) rather than appending to existing backup sets. Use with caution as this will destroy any existing backups on the media.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Name

Specifies the name of the database to backup.

Type: String
Parameter Sets: ServerObject
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-PassThru

Returns the database object that was backed up. By default, this command does not generate any output.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Refresh

Specifies that the ServerObject's databases should be refreshed before accessing the database. This is helpful when databases could have been modified outside of the ServerObject, for example through T-SQL. But on instances with a large amount of databases it might be better to make sure the ServerObject is recent enough, or pass in DatabaseObject.

Type: SwitchParameter
Parameter Sets: ServerObject
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-RetainDays

Specifies the number of days that must elapse before the backup media can be overwritten. This provides protection against accidental overwrites.

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: False

-ServerObject

Specifies the current server connection object.

Type: Server
Parameter Sets: ServerObject
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

Microsoft.SqlServer.Management.Smo.Server

Server object accepted from the pipeline (ServerObject parameter set).

Microsoft.SqlServer.Management.Smo.Database

Database object accepted from the pipeline (DatabaseObject parameter set).

OUTPUTS

None.

No output when the PassThru parameter is not specified.

Microsoft.SqlServer.Management.Smo.Database

Returns the database object that was backed up when using the PassThru parameter.

NOTES

RELATED LINKS

⚠️ **GitHub.com Fallback** ⚠️