commands

^

Invoke-DbaDbMirroring

Author Chrissy LeMaire (@cl), netnerds.net
Availability Windows, Linux, macOS

 

Synopsis

Automates the creation of database mirrors.

Description

Automates the creation of database mirrors.

  • Verifies that a mirror is possible
  • Sets the recovery model to Full if needed
  • If the database does not exist on mirror or witness, a backup/restore is performed
  • Sets up endpoints if necessary
  • Creates a login and grants permissions to service accounts if needed
  • Starts endpoints if needed
  • Sets up partner for mirror
  • Sets up partner for primary
  • Sets up witness if one is specified

NOTE: If a backup / restore is performed, the backups will be left in tact on the network share.

Syntax

Invoke-DbaDbMirroring
    [[-Primary] <DbaInstanceParameter>]
    [[-PrimarySqlCredential] <PSCredential>]
    [-Mirror] <DbaInstanceParameter[]>
    [[-MirrorSqlCredential] <PSCredential>]
    [[-Witness] <DbaInstanceParameter>]
    [[-WitnessSqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-SharedPath] <String>]
    [[-InputObject] <Database[]>]
    [-UseLastBackup]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> $params = @{
>> Primary = 'sql2017a'
>> Mirror = 'sql2017b'
>> MirrorSqlCredential = 'sqladmin'
>> Witness = 'sql2019'
>> Database = 'pubs'
>> SharedPath = '\\nas\sql\share'
>> }
>>
PS C:\> Invoke-DbaDbMirroring @params

Performs a bunch of checks to ensure the pubs database on sql2017a
can be mirrored from sql2017a to sql2017b. Logs in to sql2019 and sql2017a
using Windows credentials and sql2017b using a SQL credential.
Prompts for confirmation for most changes. To avoid confirmation, use -Confirm:$false or
use the syntax in the second example.

Example: 2
PS C:\> $params = @{
>> Primary = 'sql2017a'
>> Mirror = 'sql2017b'
>> MirrorSqlCredential = 'sqladmin'
>> Witness = 'sql2019'
>> Database = 'pubs'
>> SharedPath = '\\nas\sql\share'
>> Force = $true
>> Confirm = $false
>> }
>>
PS C:\> Invoke-DbaDbMirroring @params

Performs a bunch of checks to ensure the pubs database on sql2017a
can be mirrored from sql2017a to sql2017b. Logs in to sql2019 and sql2017a
using Windows credentials and sql2017b using a SQL credential.
Drops existing pubs database on Mirror and Witness and restores them with
a fresh backup.
Does all the things in the description, does not prompt for confirmation.

Example: 3
PS C:\> $map = @{ 'database_data' = 'M:\Data\database_data.mdf' 'database_log' = 'L:\Log\database_log.ldf' }
PS C:\> Get-ChildItem \\nas\seed | Restore-DbaDatabase -SqlInstance sql2017b -FileMapping $map -NoRecovery
PS C:\> Get-DbaDatabase -SqlInstance sql2017a -Database pubs | Invoke-DbaDbMirroring -Mirror sql2017b -Confirm:$false

Restores backups from sql2017a to a specific file structure on sql2017b then creates mirror with no prompts for confirmation.

Example: 4
PS C:\> Get-DbaDatabase -SqlInstance sql2017a -Database pubs |
>> Invoke-DbaDbMirroring -Mirror sql2017b -UseLastBackup -Confirm:$false

Mirrors pubs on sql2017a to sql2017b and uses the last full and logs from sql2017a to seed. Doesn't prompt for confirmation.

Required Parameters

-Mirror

SQL Server name or SMO object representing the mirror SQL Server.

Alias
Required True
Pipeline false
Default Value

Optional Parameters

-Primary

SQL Server name or SMO object representing the primary SQL Server.

Alias
Required False
Pipeline false
Default Value
-PrimarySqlCredential

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

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

SQL Server name or SMO object representing the witness SQL Server.

Alias
Required False
Pipeline false
Default Value
-WitnessSqlCredential

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 or databases to mirror.

Alias
Required False
Pipeline false
Default Value
-SharedPath

The network share where the backups will be backed up and restored from. Each SQL Server service account must have access to this share. NOTE: If a backup / restore is performed, the backups will be left in tact on the network share.

Alias
Required False
Pipeline false
Default Value
-InputObject

Enables piping from Get-DbaDatabase.

Alias
Required False
Pipeline true (ByValue)
Default Value
-UseLastBackup

Use the last full backup of database.

Alias
Required False
Pipeline false
Default Value False
-Force

Drop and recreate the database on remote servers using fresh backup.

Alias
Required False
Pipeline false
Default Value False
-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

Shows what would happen if the command were to run. No actions are actually performed.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

Alias cf
Required False
Pipeline false
Default Value

 

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