Skip to content

Commit

Permalink
Merge pull request #19 from jpomfret/repl-remove
Browse files Browse the repository at this point in the history
Repl remove
  • Loading branch information
jpomfret authored Apr 15, 2024
2 parents 74542b1 + 716bc72 commit f80e887
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 84 deletions.
2 changes: 1 addition & 1 deletion blog/assets/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"baseUrl": ".",
"paths": {
"*": [
"../../../../tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/!cai!jimmy/hugo-theme-stack/[email protected]/assets/*"
"..\\..\\..\\..\\Users\\jpomf\\AppData\\Local\\hugo_cache\\modules\\filecache\\modules\\pkg\\mod\\github.com\\!cai!jimmy\\hugo-theme-stack\\[email protected]\\assets\\*"
]
}
}
Expand Down
10 changes: 0 additions & 10 deletions blog/content/post/2024/dbatools-repl-get/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,3 @@ This is part of a series of posts covering how to use the dbatools replication c
- dbatools Replication: Tear down replication with dbatools

You can also view any posts I've written on Replication by heading to the [Replication Category](/categories/replication/) page of this blog.

## Presentation at SQLBits

Also don't forget I'm presenting 'managing replication with dbatools' at SQLBits 2024 in just a few weeks!

{{<
figure src="/sqlbits.png"
alt="I'm Speaking at SQLBits"
link="https://sqlbits.com/attend/the-agenda/friday/#Managing_replication_with_dbatools"
>}}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 91 additions & 47 deletions blog/content/post/2024/dbatools-repl-remove/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
title: "dbatools replication - tear down replication"
slug: "dbatools-repl-remove"
description: "Remove all the pieces and parts of replication with dbatools."
date: 2024-03-06T10:31:31Z
date: 2024-03-14T10:31:31Z
categories:
- dbatools
- replication
tags:
- dbatools
- replication
image:
draft: true
image: chris-j-davis-PT_9ux0j-x4-unsplash.jpg
draft: false
---

If you've been following along in this [dbatools](https://dbatools.io) [replication](/categories/replication/) series, thanks for joining us on this ride! I'm not going to commit to this being the final post in the series, but this is the one where we destroy everything. If you need to tear down replication - this is the post for you.

As I mentioned this is part of a series, so if you want to review the other posts I've written before this one you can see them here:

- [dbatools - introducing replication support](/dbatools-replication)
- [dbatools Replication: The Get commands](/dbatools-repl-get)
- [dbatools Replication: Setup Replication](/dbatools-repl-setup)

In fact if you want to follow along with this post, I'd at least recommend the previous post on setting up replication, so you have something ready to tear down.

---

In the [Setup Replication](/dbatools-repl-setup) post we started with enabling the server components needed for replication and then we created a publication, added articles and then finally added a subscription - in that order. To remove replication we will reverse that order, removing the dependencies we created in reverse.

First we will remove the subscriptions.

## Remove Subscription

In replication, subscribers are where the data is replicated too. Let's remove the subscription we added in the last post from the `testPub` publication on `sql1` to `sql2`. I can accomplish this with the `Remove-DbaReplSubscription` command.

>Just a reminder, when we're dealing with subscriptions we will still target the publisher as the `-SqlInstance` parameter. This is because SQL Server stores the information on the publisher, so for us to find the subscriptions we need to go there first.
```PowerShell
$sub = @{
SqlInstance = 'sql1'
Expand All @@ -26,14 +46,20 @@ $sub = @{
Remove-DbaReplSubscription @sub
```

When I run this PowerShell I get a prompt, 'Are you sure?!?'. This command is destructive, and with any dbatools commands that remove items you get a second chance to back out. In this case, I am sure, so I will press `Y` to continue and remove the subscription. I can skip this confirmation, for example when running in an automated fashion, I'll come back to this a little later.

```text
Confirm
Are you sure you want to perform this action?
Performing the operation "Removing subscription to testPub from sql1.AdventureWorksLT2022" on target "sql2".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
```

## remove article
Note there is no output from this command executing successfully, since we removed the object we were aiming at. However, no errors, so we can presume we're good here. We could also use `Get-DbaReplSubscription` to double check if we wanted to.

## Remove Article

It isn't required to remove articles from a publication before you delete a publication. However, to keep this in order I'll show you how to remove an article from a publication next. The command for this is `Remove-DbaReplArticle` and you will specify the publisher instance again, `sql1` in this case. With the parameters below, I'm removing a particular article `SalesLT.Customer` from the `testpub` publication.

```PowerShell
$article = @{
Expand All @@ -42,15 +68,18 @@ $article = @{
Publication = 'testpub'
Schema = 'salesLT'
Name = 'customer'
WhatIf = $true
}
Remove-DbaReplArticle @article -WhatIf
Remove-DbaReplArticle @article
```

`-WhatIf`
You'll notice I've added an extra parameter to this command, `-WhatIf`, this is also available on all destructive dbatools commands (and other PowerShell commands if they are written to make use of it). This means that the command will just output what it would do if you didn't have `-WhatIf` set to `$true`. In this case:

`What if: Performing the operation "Removing the article SalesLT.Customer from the testPub publication on [sql1]" on target "customer".`
```text
What if: Performing the operation "Removing the article SalesLT.Customer from the testPub publication on [sql1]" on target "customer".
```

remove whatif
If I now remove `-WhatIf` and rerun the command I'll get another prompt to confirm I'm sure, and then if I am, the article will be removed and this time the output below will show it was removed.

```Text
Confirm
Expand All @@ -68,25 +97,34 @@ Status : Removed
IsRemoved : True
```

instead we can use piping
This is good if I want to remove one article, but there isn't really much benefit to using PowerShell over just finding that publication in SSMS and removing a single article.

However, if I wanted to remove all articles, from all publications on the publisher instance, or maybe even from multiple instances - this would be a pain in SSMS (unless you generate some T-SQL code), but in PowerShell - all we need is a single line.

In PowerShell we have a concept called [Piping](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.4), where the output from one command is passed through the 'pipe' (`|`) symbol onto the next command. The output will then be acted upon by the next command you specify.

If I run the following code. First, `Get-DbaReplArticle` will retrieve all the articles for publications on `sql1` and then they will be passed along the pipeline to `Remove-DbaReplArticle` to be removed. However, I've again added `-WhatIf` so we can preview what would happen.

```PowerShell
Get-DbaReplArticle -SqlInstance sql1 | Remove-DbaReplArticle -WhatIf
```

The output shows that two articles were found and will be removed from the `snappy` and `mergey` publications respectively.

```text
What if: Performing the operation "Removing the article SalesLT.Address from the snappy publication on [sql1]" on target "address".
What if: Performing the operation "Removing the article SalesLT.Product from the mergey publication on [sql1]" on target "product".
```

remove whatif and run it for real
Now, I'm sure I want to remove all these articles so if I remove `-WhatIf` and instead replace it with `-Confirm`, I won't be asked if I'm sure - this skips the confirmation prompt.

```text
Confirm
Are you sure you want to perform this action?
Performing the operation "Removing the article SalesLT.Address from the snappy publication on [sql1]" on target "address".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
```PowerShell
Get-DbaReplArticle -SqlInstance sql1 | Remove-DbaReplArticle -Confirm:$false
```

The output below shows that two articles were removed.

```text
ComputerName : sql1
InstanceName : MSSQLSERVER
SqlInstance : [sql1]
Expand All @@ -96,11 +134,6 @@ ObjectSchema : SalesLT
Status : Removed
IsRemoved : True
Confirm
Are you sure you want to perform this action?
Performing the operation "Removing the article SalesLT.Product from the mergey publication on [sql1]" on target "product".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
ComputerName : sql1
InstanceName : MSSQLSERVER
SqlInstance : [sql1]
Expand All @@ -113,15 +146,21 @@ IsRemoved : True

## Remove Publication

The final objects we have to clear up are any publications. I'll call `Remove-DbaReplPublication` with the following parameters to remove the `TestPub` publication from the `sql1` instance.

```PowerShell
$pub = @{
SqlInstance = 'sql1'
Database = 'AdventureWorksLT2022'
Name = 'TestPub'
}
Remove-DbaReplPublication @pub
$pub = @{
SqlInstance = 'sql1'
Database = 'AdventureWorksLT2022'
Name = 'TestPub'
}
Remove-DbaReplPublication @pub
```

Again since I didn't specify `-Confirm:$false` I will have to confirm I'm sure, and then the publication will be removed.

In the text output below, you'll notice there are actually two confirmation prompts from running this command. First to remove the publication, and second to remove the SQL Agent job that is associated with this publication.

```text
Confirm
Are you sure you want to perform this action?
Expand All @@ -143,13 +182,18 @@ Status : Removed
IsRemoved : True
```

## Disable Publishing

## disable publishing
At this point all the publications, articles and subscriptions have been cleared up, but the `sql1` instance is still enabled for publishing, and is setup as a distributor. Let's put things back to how they were by disabling these components.

First, I'll disable publishing on `sql1` with the following command.

```PowerShell
Disable-DbaReplPublishing -SqlInstance sql1 -force
Disable-DbaReplPublishing -SqlInstance sql1
```

Again, our friendly confirmation prompt, but then you might also notice a warning. The warning states that there are databases still enabled for replication, this is a bug within RMO, the library dbatools uses, and needs further investigation to resolve. The good news is that although we get this message the command does successfully disable publishing.

```text
Confirm
Are you sure you want to perform this action?
Expand All @@ -159,18 +203,19 @@ WARNING: [15:33:35][Disable-DbaReplPublishing] Unable to disable replication pub
Changed database context to 'distribution'.
```

## disable distribution
Once this is complete we can move onto the final step.

## Disable Distribution

Again, one more line of PowerShell to disable distribution for `sql1`, this also cleans up the `distribution` database (or whatever special name you gave it when you set it up). This time I will specify `-Confirm:$false` to skip the confirmation prompt.

```PowerShell
Disable-DbaReplDistributor -SqlInstance sql1
Disable-DbaReplDistributor -SqlInstance sql1 -Confirm:$false
```

```text
Confirm
Are you sure you want to perform this action?
Performing the operation "Disabling and removing distribution on sql1" on target "sql1".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
The output of this command is the same as what `Get-DbaReplServer` would return at this point. It shows that `IsDistributor` and `IsPublisher` are both set to 'False' and our `sql1` instance is back to how it looked before we started to build out replication in the last post.

```text
ComputerName : sql1
InstanceName : MSSQLSERVER
SqlInstance : sql1
Expand All @@ -180,16 +225,15 @@ DistributionServer : SQL1
DistributionDatabase :
```

```PowerShell
Get-DbaReplServer -SqlInstance sql1
```
## Series

```
ComputerName : sql1
InstanceName : MSSQLSERVER
SqlInstance : sql1
IsDistributor : False
IsPublisher : False
DistributionServer :
DistributionDatabase :
```
This is part of a series of posts covering how to use the dbatools replication commands, other posts in the series:

- [dbatools - introducing replication support](/dbatools-replication)
- [dbatools Replication: The Get commands](/dbatools-repl-get)
- [dbatools Replication: Setup replication with dbatools](/dbatools-repl-setup)
- dbatools Replication: Tear down replication with dbatools - this post!

You can also view any posts I've written on Replication by heading to the [Replication Category](/categories/replication/) page of this blog.

Header Photo by [Chris J. Davis](https://unsplash.com/@chrisjdavis?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/a-close-up-of-a-computer-keyboard-on-a-desk-PT_9ux0j-x4?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash)
22 changes: 6 additions & 16 deletions blog/content/post/2024/dbatools-repl-setup/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ Welcome to another post in my [dbatools](https://dbatools.io) [replication](/cat

---

This post is focusing on how to setup replication with dbatools. We support all three flavours - [snapshot](https://learn.microsoft.com/sql/relational-databases/replication/snapshot-replication?view=sql-server-ver16&WT.mc_id=AZ-MVP-5003655), [transactional](https://learn.microsoft.com/en-us/sql/relational-databases/replication/transactional/transactional-replication?view=sql-server-ver16?wt.mc_id=AZ-MVP-5003655) and even [merge replication](https://learn.microsoft.com/sql/relational-databases/replication/merge/merge-replication?view=sql-server-ver16&WT.mc_id=AZ-MVP-5003655)!
This post is focusing on how to setup replication with dbatools. We support all three flavours - [snapshot](https://learn.microsoft.com/sql/relational-databases/replication/snapshot-replication?view=sql-server-ver16&WT.mc_id=AZ-MVP-5003655), [transactional](https://learn.microsoft.com/en-us/sql/relational-databases/replication/transactional/transactional-replication?view=sql-server-ver16?wt.mc_id=AZ-MVP-5003655) and even [merge replication](https://learn.microsoft.com/sql/relational-databases/replication/merge/merge-replication?view=sql-server-ver16&WT.mc_id=AZ-MVP-5003655)!

In this article I'll be creating a transactional publication, but the steps for setup are very similar no matter which flavour you're implementing.

I'll walk through and demonstrate all the steps to setup replication in this article as dbatools allows us to complete them all. However, I won't go into a lot of details on why or how replication works, or provide guidance on best practices. If you need more information on replication as a technology then I recommend visiting the [Microsoft Docs](https://learn.microsoft.com/en-us/sql/relational-databases/replication/sql-server-replication?view=sql-server-ver16?wt.mc_id=AZ-MVP-5003655).

## Setup Distributor and Publisher

Alright, step 1!
Alright, step 1!

Replication requires a server that is configured as a distributor, and a server that is configured as a publisher.
Replication requires a server that is configured as a distributor, and a server that is configured as a publisher.

Good news, these pieces of the puzzle can both be configured on the same server which is what I'll demonstrate in my test environment. In environments where replication has a high throughput and/or requires peak performance you can configure a separate server for distribution to move some of the load off of the publisher.

Expand All @@ -46,7 +46,7 @@ You can see the following output is returned, note that the `DistributionDatabas

There is an optional parameter `-DistributionDatabase` if you want to specify a certain name for the database that is created on the distributor, but if you don't specify, the database will be called `distribution` which is the default. Same as if you were to create it through SSMS.

```
```text
ComputerName : sql1
InstanceName : MSSQLSERVER
SqlInstance : sql1
Expand Down Expand Up @@ -179,7 +179,6 @@ More information on this command is available in the dbatools help documentation

- [Add-DbaReplArticle](https://dbatools.io/Add-DbaReplArticle)


## Add a Subscription

Alright, the final piece of this puzzle is to add a subscription.
Expand Down Expand Up @@ -223,6 +222,7 @@ Replication relies on executables that live outside of the SQL Server Engine, bu
Get-DbaAgentJob -SqlInstance sql1 -Category repl-snapshot |
Start-DbaAgentJob
```

The output of this command shows it found the `SQL1-AdventureWorksLT2022-testPub-1` job and started it.

```text
Expand Down Expand Up @@ -254,7 +254,6 @@ Now these commands aren't specifically related to replication, but all dbatools
- [Get-DbaAgentJob](https://dbatools.io/Get-DbaAgentJob)
- [Start-DbaAgentJob](https://dbatools.io/Start-DbaAgentJob)


## Testing time

With transactional replication, the magic is that rows are replicated (in near real-time) as they change on the publisher. Let's prove the replication we've configured is working as expected by inserting a row into the `SalesLT.Customer` table on the `sql1` instance.
Expand Down Expand Up @@ -285,6 +284,7 @@ VALUES ('0','Mr.','Bill','Gates','ElzTpSNbUW1Ut+L5cWlfR7MF6nBZia8WpmGaQPjLOJA=',
```

> We could also do this using PowerShell and dbatools with the following call to `Invoke-DbaQuery`.
>
> ```PowerShell
>$insertParams = @{
> SqlInstance = 'sql1'
Expand Down Expand Up @@ -330,14 +330,4 @@ This is part of a series of posts covering how to use the dbatools replication c

You can also view any posts I've written on Replication by heading to the [Replication Category](/categories/replication/) page of this blog.

## Presentation at SQLBits

Also don't forget I'm presenting 'managing replication with dbatools' at SQLBits 2024 in just a couple of weeks!

{{<
figure src="/sqlbits.png"
alt="I'm Speaking at SQLBits"
link="https://sqlbits.com/attend/the-agenda/friday/#Managing_replication_with_dbatools"
>}}
Header Photo by [Gabor Szucs](https://unsplash.com/@gabcsika?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/body-of-water-surrounded-with-trees-under-white-skies-b4b-5FodP3I?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash)
10 changes: 0 additions & 10 deletions blog/content/post/2024/dbatools-replication/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,3 @@ This is an exciting time, but this is just the introductory post for a series on
- dbatools Replication: Tear down replication with dbatools

You can also view any posts I've written on Replication by heading to the [Replication Category](/categories/replication/) page of this blog.

## "But I want to hear a presentation on the topic!"

Well great news here, I'll be presenting [Managing replication with dbatools](https://sqlbits.com/attend/the-agenda/friday/#Managing_replication_with_dbatools) at [SQLBits 2024](https://sqlbits.com/) on Friday 22nd March in Farnborough, I'll link to the recording if\when it becomes available.

{{<
figure src="/sqlbits.png"
alt="I'm Speaking at SQLBits"
link="https://sqlbits.com/attend/the-agenda/friday/#Managing_replication_with_dbatools"
>}}

0 comments on commit f80e887

Please sign in to comment.