Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Update the NODE_VERSION arg in docker-compose.yml to pick a Node version: 18, 16, 14
ARG NODE_VERSION=16
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${NODE_VERSION}
# Update the NODE_VERSION arg in docker-compose.yml to pick a Node version: 22, 20, 18
ARG NODE_VERSION=20
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:1-${NODE_VERSION}-bookworm

# VARIANT can be either 'hugo' for the standard version or 'hugo_extended' for the extended version.
ARG VARIANT=hugo
Expand Down
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// Example versions: latest, 0.73.0, 0,71.1
// Rebuild the container if it already exists to update.
"VERSION": "latest",
// Update NODE_VERSION to pick the Node.js version: 12, 14
"NODE_VERSION": "14"
// Update NODE_VERSION to pick the Node.js version: 22, 20, 18
"NODE_VERSION": "20"
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Well tonight marks three weeks since I gave my first user group presentation and

TL;DR I didn’t die, the SQL Server community is fantastic and I have amazing supportive friends.

{{< tweet user="Pittfurg" id="1004125722082934784" >}}
{{< x user="Pittfurg" id="1004125722082934784" >}}

## Why Present?

Expand Down
2 changes: 1 addition & 1 deletion blog/content/post/2018/t-sql-tuesday-108/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The side goal is docker/containers/kubernetes (maybe), I’m wondering if I coul

I saw the tweet below last week from the beard, [Rob Sewell](https://twitter.com/sqldbawithbeard), that quoted [Bob Ward’s](https://twitter.com/bobwardms) thoughts on learning directions.  Feels like this is probably solid advice to justify my side goal.

{{< tweet user="sqldbawithbeard" id="1061032613979267072" >}}
{{< x user="sqldbawithbeard" id="1061032613979267072" >}}

## Learning Plan

Expand Down
2 changes: 1 addition & 1 deletion blog/content/post/2019/multiple-triggers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Well it has been a little quiet here recently. I just (or it’s been two weeks

This is also going to be a quick post. I asked a question on Twitter last week about what happens when you have multiple triggers on a table. I got the answer (Thanks Aaron!), but figured this would be a good thing to demonstrate.

{{< tweet user="AaronBertrand" id="1121436026956861445" >}}
{{< x user="AaronBertrand" id="1121436026956861445" >}}

I have also been playing with Azure Data Studio and the new notebook feature, so I answered this question with a step-by-step example in a notebook. I also found that you can easily store these notebooks on GitHub so I have uploaded it to my demos repo for you to follow along.

Expand Down
2 changes: 1 addition & 1 deletion blog/content/post/2020/t-sql-tuesday-127/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Whether it’s chrome or SSMS, I cannot help myself when it comes to opening new

I even got called out by my good friend Andrew ([B](https://awickham.com/)|[T](https://twitter.com/awickham)) this last week:

{{< tweet user="awickham" id="1267503576571674624" >}}
{{< x user="awickham" id="1267503576571674624" >}}

My tips & tricks are focused around managing tabs, and they work in all browsers (at least all that I have on my laptop. Chrome, Edge, IE11).

Expand Down
188 changes: 94 additions & 94 deletions blog/content/post/2025/dbatools-backup-replacename/index.md
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
---
title: "dbatools Backup-DbaDatabase -ReplaceInName"
slug: "dbatools-backup-replacename"
description: "Recently I was reading the docs for `Backup-DbaDatabase` and found a parameter I didn't realise existed, but is so useful when you want to automate backups, but keep control of the file names."
date: 2025-04-18T12:00:00Z
categories:
- dbatools
- backups
tags:
- dbatools
- backups
image: guillaume-auceps-vffNjorpNrg-unsplash.jpg
draft: false
---

I use [dbatools](https://dbatools.io) all the time, I'm talking literally every workday, but how often do I read the docs? Probably not often enough!

## Get-Help

Whenever I'm teaching PowerShell and especially dbatools, I talk about `Get-Help` because without leaving your console, you can review the full documentation of the commands. Now, this documentation has to have been added by the author of the command, but we guarantee that for every dbatools command with built in Pester tests!

## Backup step for code release

So, not too long ago, I was writing a script that would be part of a release pipeline. The client wanted to do a quick `COPY_ONLY` backup of all the databases on a few separate instances, before they deployed new application code, which could be making schema changes. Basically, creating an easy rollback plan if the deployment went bad.

Not a problem with `Backup-DbaDatabase`, I already know we can pass in multiple instances, and specify `-CopyOnly` to not interfere with the LSN chain of the regular backups.

The difference was, there was already a specified naming convention for the backups, and we wanted to match that naming with our new automated script.

If I start with the following script, it will perform a `COPY_ONLY` backup of all databases on the `mssql1` instance, to the specified folder `/shared/release/v7`, which in my case I was passing the version folder name through from my Azure DevOps pipeline.

```PowerShell
$backupParams = @{
SqlInstance = "mssql1"
Path = "/shared/release/v7"
CopyOnly = $true
}
Backup-DbaDatabase @backupParams
```

You can see we got the files we needed, but using the standard dbatools naming convention of `databaseName_timestamp.bak`.

{{<
figure src="backupFiles.png"
alt="ls of backup directory showing backup files"
>}}

For most situations, this is fine and I leave it at that, but how would I manage to change the location to be `/shared/release/v7/mssql1/MSSQLSERVER/pubs.bak`. So the server and instance names are folders, and then the file is just named after the database name?

You could do this in PowerShell, get the list of databases, loop through them setting the full name of the backup. But instead, if we check the documentation for `Backup-DbaDatabase` we can see this functionality is already built in with the `-ReplaceInName` parameter.

## -ReplaceInName Parameter

By checking the help we can see which values can be replaced on the fly.

```PowerShell
Get-Help Backup-DbaDatabase
```

This is straight from the docs, and if you want to read it in the online version you can head to [docs.dbatools.io](https://docs.dbatools.io/Backup-DbaDatabase.html).

```text
-ReplaceInName [<Switch>]
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
```

So, with this information, we can change the script to look like this. Using the keywords `servername`, `instancename` and `dbname` in the parameter values, and including the `ReplaceInName` switch.

```PowerShell
$backupParams = @{
SqlInstance = "mssql1"
Path = "/shared/release/v7/servername/instancename"
FilePath = "dbname.bak"
ReplaceInName = $true
CopyOnly = $true
}
Backup-DbaDatabase @backupParams
```

The results look like this, which is exactly what we needed. Now obviously, depending on what you want the file names to be, these keywords might not be enough, but they give you a decent amount of flexibility to customise the output.

{{<
figure src="backupFilesNames.png"
alt="ls of backup folder after using -ReplaceInName param"
>}}

Hope you find this one useful, and it's a good reminder for all of us to read the docs! There is so much functionality in dbatools, none of us know everything this module is capable of!

Header image by [Guillaume Auceps](https://unsplash.com/@gauceps?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/a-row-of-boats-floating-on-top-of-a-body-of-water-vffNjorpNrg?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash).
---
title: "dbatools Backup-DbaDatabase -ReplaceInName"
slug: "dbatools-backup-replacename"
description: "Recently I was reading the docs for `Backup-DbaDatabase` and found a parameter I didn't realise existed, but is so useful when you want to automate backups, but keep control of the file names."
date: 2025-04-18T12:00:00Z
categories:
- dbatools
- backups
tags:
- dbatools
- backups
image: guillaume-auceps-vffNjorpNrg-unsplash.jpg
draft: false
---
I use [dbatools](https://dbatools.io) all the time, I'm talking literally every workday, but how often do I read the docs? Probably not often enough!
## Get-Help
Whenever I'm teaching PowerShell and especially dbatools, I talk about `Get-Help` because without leaving your console, you can review the full documentation of the commands. Now, this documentation has to have been added by the author of the command, but we guarantee that for every dbatools command with built in Pester tests!
## Backup step for code release
So, not too long ago, I was writing a script that would be part of a release pipeline. The client wanted to do a quick `COPY_ONLY` backup of all the databases on a few separate instances, before they deployed new application code, which could be making schema changes. Basically, creating an easy rollback plan if the deployment went bad.
Not a problem with `Backup-DbaDatabase`, I already know we can pass in multiple instances, and specify `-CopyOnly` to not interfere with the LSN chain of the regular backups.
The difference was, there was already a specified naming convention for the backups, and we wanted to match that naming with our new automated script.
If I start with the following script, it will perform a `COPY_ONLY` backup of all databases on the `mssql1` instance, to the specified folder `/shared/release/v7`, which in my case I was passing the version folder name through from my Azure DevOps pipeline.
```PowerShell
$backupParams = @{
SqlInstance = "mssql1"
Path = "/shared/release/v7"
CopyOnly = $true
}
Backup-DbaDatabase @backupParams
```
You can see we got the files we needed, but using the standard dbatools naming convention of `databaseName_timestamp.bak`.
{{<
figure src="backupFiles.png"
alt="ls of backup directory showing backup files"
>}}
For most situations, this is fine and I leave it at that, but how would I manage to change the location to be `/shared/release/v7/mssql1/MSSQLSERVER/pubs.bak`. So the server and instance names are folders, and then the file is just named after the database name?
You could do this in PowerShell, get the list of databases, loop through them setting the full name of the backup. But instead, if we check the documentation for `Backup-DbaDatabase` we can see this functionality is already built in with the `-ReplaceInName` parameter.
## -ReplaceInName Parameter
By checking the help we can see which values can be replaced on the fly.
```PowerShell
Get-Help Backup-DbaDatabase
```
This is straight from the docs, and if you want to read it in the online version you can head to [docs.dbatools.io](https://docs.dbatools.io/Backup-DbaDatabase.html).
```text
-ReplaceInName [<Switch>]
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
```
So, with this information, we can change the script to look like this. Using the keywords `servername`, `instancename` and `dbname` in the parameter values, and including the `ReplaceInName` switch.
```PowerShell
$backupParams = @{
SqlInstance = "mssql1"
Path = "/shared/release/v7/servername/instancename"
FilePath = "dbname.bak"
ReplaceInName = $true
CopyOnly = $true
}
Backup-DbaDatabase @backupParams
```
The results look like this, which is exactly what we needed. Now obviously, depending on what you want the file names to be, these keywords might not be enough, but they give you a decent amount of flexibility to customise the output.
{{<
figure src="backupFilesNames.png"
alt="ls of backup folder after using -ReplaceInName param"
>}}
Hope you find this one useful, and it's a good reminder for all of us to read the docs! There is so much functionality in dbatools, none of us know everything this module is capable of!
Header image by [Guillaume Auceps](https://unsplash.com/@gauceps?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/a-row-of-boats-floating-on-top-of-a-body-of-water-vffNjorpNrg?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading