How To Use Powershell to Configure Multiple Databases for Log Shipping

I recently needed to configure log shipping for multiple databases at once as part of a migration project. I turned to PowerShell to do this.

But before we get to that part, this post assumes that you’ve done the upfront work to create shares for the backups to write to and for the backups to be copied to. This will involve providing the right permissions for the SQL Server service accounts involved in Log Shipping. If you are not familiar with this, that’s perfectly fine. Check out this article in MS Docs first.

Configure Log Shipping With PowerShell

I first used some commands to find what I could use to do this.

Get-Command *LogShipping* -Module DBATools

#This returned Invoke-DbaDbLogShipping

#Then I ran the below to look at the help for this command
Help Invoke-DbaDbLogShipping -Full 

<#I knew I would need to get the databases on the SQL instance so I did the
below and I came across Get-DbaDatabase
#>
Get-Command *database* -Module DBATools

<#Then I ran the help for that to see how to use the command#>
Help Get-DbaDatabase -FULL

 

Then I wrote two options for providing a list of databases to the script. The first option is a hard-coded list of databases. I did this because you might not want to set up every database on an instance to be log shipped. Perhaps you’re only interested in certain ones.  Rather than hard-coding in the PoSH script, you could change this to have the script read a list of databases from a provided text file as well.

The second option assumes that you want to log ship all user databases. I used the switch -ExcludeSystem to make sure the script didn’t try to log ship my system databases.

#Populate a list of databases on the instance that you want to configure logShipping for 
$SQLInstanceName = "MySQLInstance" 
$Databases = "CollegeFootball", "DBAUtility", "StackOverflow2013", "UMSLIntermediateSQLClass" 

<#Or to set up Log Shipping for every user database on the SQL instance#> 
$SQLInstanceName = "MySQLInstance" 
$Databases = get-dbadatabase -sqlinstance $SQLInstanceName -ExcludeSystem -Database $Databases | SELECT Name

 

An Example from the PowerShell Help

Next, from the help for the log shipping command I pulled out one of the examples. This only gets me one database at a time though, so I need to modify this so I can set up log shipping for multiple databases at once.

PS C:\>$params = @{

>> SourceSqlInstance = 'sql1'
>> DestinationSqlInstance = 'sql2'
>> Database = 'db1'
>> SharedPath= '\\sql1\logshipping'
>> LocalPath= 'D:\Data\logshipping'
>> BackupScheduleFrequencyType = 'daily'
>> BackupScheduleFrequencyInterval = 1
>> CompressBackup = $true
>> CopyScheduleFrequencyType = 'daily'
>> CopyScheduleFrequencyInterval = 1
>> GenerateFullBackup = $true
>> RestoreScheduleFrequencyType = 'daily'
>> RestoreScheduleFrequencyInterval = 1
>> SecondaryDatabaseSuffix = 'DR'
>> CopyDestinationFolder = '\\sql2\logshippingdest'
>> Force = $true
>> }
>>
PS C:\> Invoke-DbaDbLogShipping @params

I add in a foreach loop and put it all together to arrive at the below to do this for a selected list of databases.

<#Populate a list of databases on the instance that you want to configure logShipping for. You could also get the list from a text file, if you wanted to.
#>
$SQLInstanceName = "MySQLInstance"
$Databases =  "CollegeFootball", "DBAUtility", "StackOverflow2013"

# Configure Log Shipping.
foreach ($Database in $Databases)
{
  $params = @{
    SourceSqlInstance = $SQLInstanceName
    DestinationSqlInstance = 'MySQLInstance\sql2019'
    Database = $Databases
    BackupNetworkPath = '\\MySQLInstance\LSBackups'
    BackupLocalPath = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\LSBackups'
    BackupScheduleFrequencySubdayType = 'Minutes'
    BackupScheduleFrequencySubdayInterval = 5
    CopyDestinationFolder = 'D:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA\LogShippingCopy'
    CopyScheduleFrequencySubdayType = 'Minutes'
    CopyScheduleFrequencySubdayInterval = 5
    RestoreScheduleFrequencySubdayType = 'Minutes'
    RestoreScheduleFrequencySubdayInterval = 5
    GenerateFullbackup = $true
    CompressBackup = $true
    NoInitialization = $false
    NoRecovery = $true
    Force = $true
    }
  Invoke-DbaDbLogShipping @params 
}

Take note of a few things.

  1. First, there is a switch in the script called GenerateFullBackup that I’m setting to true. If you have existing backups you want to use, there is a parameter for that called UseExistingFullBackup, and a related parameter called UseBackupFolder.
  2. Second, compressing the backup is not enabled by default, so I used the CompressBackup parameter and set it to true in order to compress backups.
  3. Third, a parameter called DisconnectUsers exists that, in combination with setting the StandBy parameter to true, will make the databases on the secondary readable and then disconnect users when the Log Shipping schedule for the restores kicks in.
  4. I am using the NoRecovery switch set to True. You will need to remove that if you want to use the StandBy Parameter.

 

Log Shipping All User Databases

To configure log shipping with PowerShell so that all the eligible user databases on a SQL instance are done, you would do the following. Notice in this example, I’ve changed the $params for Database from simply $Databases to $Database.Name.

<# 
For setting up Log Shipping on all eligible user databases 
Notice in this script that I changed the Database part of the script to $Database.Name
#>
$SQLInstanceName = "MySQLInstance"
$Databases = get-dbadatabase -sqlinstance $SQLInstanceName -ExcludeSystem | SELECT Name

# Configure Log Shipping 

foreach ($Database in $Databases)
{
    $params = @{
        SourceSqlInstance = 'MySQLInstance'
        DestinationSqlInstance = 'MySQLInstance\sql2019'
        Database = $Database.Name
        BackupNetworkPath = '\\MySQLInstance\LSBackups'
        BackupLocalPath = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\LSBackups'
        BackupScheduleFrequencySubdayType = 'Minutes'
        BackupScheduleFrequencySubdayInterval = 5
        CopyDestinationFolder = 'D:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA\LogShippingCopy'
        CopyScheduleFrequencySubdayType = 'Minutes'
        CopyScheduleFrequencySubdayInterval = 5
        RestoreScheduleFrequencySubdayType = 'Minutes'
        RestoreScheduleFrequencySubdayInterval = 5
        GenerateFullbackup = $true
        CompressBackup = $true
        NoInitialization = $false
        NoRecovery = $true
        Force = $true
    }

    Invoke-DbaDbLogShipping @params 

}

 

These scripts allow you to configure Log Shipping with PowerShell either for a pre-determined list of databases or for all eligible databases on the SQL instance.

Next Steps to Take

  1. Consider looking up other articles about Log Shipping to learn the details and variations. For example, here and here.
  2. Find a good introduction to PowerShell for use with SQL Server because automation is key these days, especially as your work environment gets larger.
  3. If you would like help with anything in this post, or with something else related to SQL Server, reach out to me here, or on Twitter, and I’ll be glad to offer assistance.

 

What I Learned From Giving My First Presentation

I recently was thrust into a conundrum. I suddenly found myself in need of a speaker for a recent SQL Server Meetup this month. My co-organizer and I had one week to find a presenter, set up a new Meetup, and then publicize so people outside of Meetup would know about it. I decided that was too short notice to go out to  Twitter and ask for volunteers to speak.

I thought, “What could I talk about?” You see, presenting is something I’ve always wanted to start doing. I just never did it. For many years, I thought that I didn’t know enough and that presenting was for the “famous” SQL Server people to do and I should just wait on the sidelines. However, over the last couple of years I’ve felt more confident about what I know.

Alos, I’ve come to realize that even people starting out have things they can share. For example, presentations can just be a series of short talks and work problems that a person has solved. Everyone has those examples. you don’t have to Brent Ozar, Kendra Little or Grant Fritchey to share problems you’ve solved at work. Presenting that information helps the presenter grow and helps the people hearing the content. People in the audience might hear a new, inventive way to resolve an issue, or they might have this problem in the days immediately after the presentation and be helped by having recently heard someone’s explanation of how they solved it, even if the solution wasn’t “ingenious”.

Almost out of nowhere I had an idea. I could talk about features in modern SQL Server that make certain aspects of working with the product easier. These could be enhancements like brand new features and feature sets or things like enhancement to T-SQL. I put together a rough list of items to include. There were close to 20 things on the list. As I worked through the presentation, I narrowed the list to 12 features or enhancements. After about 11 hours building slides, doing research, creating and testing demos and another 2 hours practicing giving the presentation, I was ready. You can see the results of that work here.

However, this post isn’t about the content of the presentation. I want to share what I learned as the result of putting together and giving the presentation. Without further delay, let’s look at the bullet points that follow.

 

  • Be careful what you eat before presenting. You want to focus on your presentation and not your queasy stomach or your heartburn, etc.

 

  • I re-learned where Presenter Notes are in the slides. I had given a presentation many months ago internally to my company. It was on finding good resources for training and during that process, had to look up presenter’s notes for PowerPoint. So much time had gone by since then, that I had to look it up again. In the lower right corner of PowerPoint there is an icon with an ^ with four dashes under it and the word “Notes” next to it. Click that and you get a small window below your slide that you can type your talking points into.

Rather than fill up your slides with text that you end up reading, use this Presenter’s Notes area to make short notes about the ideas you want to explain. Or you could type out what you’re going to say into paragraphs in this area. When you run the slideshow, you will see the slide and your Presenter’s Notes on one screen and your audience will see just the slides.

PowerPoint Presenters Notes

 

  •  I learned there is a Presentation Coach feature in PowerPoint that lets you practice your presentation and provides a feedback report. To use this feature, inside PowerPoint select the Slide Show menu at the top and then “Rehearse With Coach”. This feature gives you feedback about things like your use of filler words, how fast you’re speaking and the tone of your voice.

 

  • I learned there is a built-in Design suggestions feature for your slides in PowerPoint. Once I had my content and Presenter’s Notes mostly the way I wanted it, I realized that I needed to do something about the drab looking slides. I hadn’t started with a template of any kind and now I needed help making it visually interesting.

At the top of the Power Point menu ribbon I clicked on the word Design. Then off to the far right I saw a button called “Design Ideas”. I left clicked that and a new set of slide options opened up down the right side. I clicked on the ones I thought would look ok, keeping in mind certain key things. First, when designing slides you will need to avoid bright or pastel type colors that hurt the eyes. Second, these color choices also often make the text hard to read. As I left clicked on options down the right side, the slides changed and I could see what my presentation would look like.

PowerPoint Presentation Design Ideas Menu

 

  • I learned new information about SQL Server as I researched some topics I was less familiar with. I was less familiar with some topics I was planning to cover than others. As a result, I had to do some research to make sure I understood how the feature worked. In this way, the experience of presenting forced me to learn more about SQL Server, and that’s a good thing!

So, that’s what I learned from giving my first public presentation. I hope this encourages you to start presenting and makes things a little easier for you when you decide to give it a try.

 

Next Steps to Take

  1. Consider some problems you have solved at work and give some thought to formalizing what you learned by sharing that information with others.
  2. Look up a SQL Server Meetup group and join one. Start attending and think about how your experience can help others.
  3. If you would like help with anything in this post, or with something else related to SQL Server, reach out to me here, or on Twitter, and I’ll be glad to offer assistance.

What I’ve Been Doing Recently

Dear readers, it’s been a little while since my last blog post. Life is busy and so I’ve been away for a little bit. I am hoping to get back into the swing of things with more blogging though.

Here’s what I’ve been up to.

  1. Keeping a SQL Server Meetup going. I’ve been working with my co-organizer, Anthony Fortner, to discuss group details and get speakers lined up. I’ve had some difficulty with that recently and I need to go back to the community on Twitter and probably review Sessionize to find some additional people who regularly present.
  2. Finding and transitioning to new employment. Finding new work feels like a full time job. There is a lot of energy that goes into the process. I found a new home about six weeks ago.
  3. Because of the new job I landed, I’ve been figuring out a plan of attack for learning the necessary things about SQL Server in AWS. I’ll likely provide a list of those resources and my plan in a separate blog post.
  4. Watching the 6 part series on Azure SQL from Anna Hoffman and Bob Ward. Coming soon there will be a blog series on their videos. I may use these videos and the Azure SQL learning paths on Microsoft Learn to pursue the DP-300 certification.
  5. Continuing to take care of myself.  I’ve stuck to my routine of taking Saturdays off as described here.  I’ve been watching parts of the NBA Finals with my family and that has been fun and relaxing.
  6. I built and gave my first recorded, public presentation on SQL Server. You can find that here.

I would love to hear from you. Reach out to me via email, leem@leemarkum.com, or on Twitter.