< Back

Invoke-DbaDbMirroring

Mon Jan 13, 2020 12:22 pm

NAME Invoke-DbaDbMirroring



SYNOPSIS

Automates the creation of database mirrors.





SYNTAX

Invoke-DbaDbMirroring [[-Primary] <DbaInstanceParameter>] [[-PrimarySqlCredential] <Pscredential>] [-Mirror]

<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-MirrorSqlCredential] <Pscredential>] [[-Witness]

<DbaInstanceParameter>] [[-WitnessSqlCredential] <Pscredential>] [[-Database] <System.String[]>] [[-SharedPath]

<String>] [[-InputObject] <Microsoft.SqlServer.Management.Smo.Database[]>] [-UseLastBackup <Switch>] [-Force

<Switch>] [-EnableException <Switch>] [<CommonParameters>]





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.





PARAMETERS

-Database [<System.String[]>]

The database or databases to mirror.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Force [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]

Enables piping from Get-DbaDatabase.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Mirror [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MirrorSqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Primary [<DbaInstanceParameter>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-PrimarySqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SharedPath [<String>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-UseLastBackup [<Switch>]

Use the last full backup of database.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Witness [<DbaInstanceParameter>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-WitnessSqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Mirroring, Mirror, HA

Author: Chrissy LeMaire (@cl), netnerds.net



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



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



RELATED LINKS

https://dbatools.io/Invoke-DbaDbMirroring