Quantcast
Viewing all articles
Browse latest Browse all 3160

Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

Period.

End of Sentence.

Yes, I’m serious. See for yourself.

Get-SqlDatabase -ServerInstance localhost |
Where { $_.Name -ne 'tempdb' } |
Backup-SqlDatabase

With that short command I can backup every database on my SQL Server (to the default backup path). Of course, that’s just one instance of SQL Server. If I wanted to wanted to backup every single database on every single instance of SQL Server in my Registered Servers list (or Central Management Server), I would have to add an entire line of code and these two curly brackets {} to be able to do that.

foreach $RegisteredSQLs IN dir -recurse SQLSERVER:\SQLRegistration\'Database Engine Server Group'\Host\ | where {$_.Mode -ne 'd'} )
{
Get-SqlDatabase -ServerInstance $RegisteredSQLs.Name |
Where { $_.Name -ne 'tempdb' } |
Backup-SqlDatabase
}

Yes, it’s that easy. Now, I’m not telling you this so that you’ll go and completely replace your database backup process with PowerShell, at least not yet; I’m telling you this so that you know it for the one-off occasions when you have to backup some databases manually. For regular backups of your databases, you may not even have to worry about that; those backups might be taken care of by sysadmins or your magical SAN. If it is your job to worry about backing up your SQL Server databases, it’s probably best if you use Ola Hallengren's Maintenance Solution scripts, which, BTW, you can deploy to all your servers using PowerShell. Watch SQL Server MVP Andre Kamman demonstrate that for you in this video.

Probably the Most Important PowerShell Trick Ever

Ok, now back to those one-off occasions when you need to backup your databases. Most times when I need to do that, I don’t need to backup all of my databases, so it would be great to thin down the list of databases being backed up. Thankfully PowerShell has a wonderful cmdlet called Out-GridView which has a -PassThru parameter to allow you to do just that. Just use the command at the top of this article and swap out the WHERE clause on it with Out-GridView -PassThru, but be sure to leave the “|” on the end of the line.

Get-SqlDatabase -ServerInstance localhost |
Out-GridView -PassThru |
Backup-SqlDatabase

To me the -PassThru parameter on the Out-GridView cmdlet is the “killer app” of the PowerShell world. With that one short command and the pipe character, you can modify a process that you have already written and make it do the same thing but just do it only to the “things” that you choose.

‘Things’ in this case could mean Databases if that was what you were working with, but it could also mean Tables, SQL Server Agent Jobs, SSRS Reports and/or DataSets, CSV files, or heck even entire instances of SQL Server.

Quick Tutorial of Out-GridView

Here is an example of what using the Out-GridView cmdlet will look like in this scenario. In this example I have selected three databases and when I click the “OK” button in the bottom-right the command will proceed and only those databases will be backed up.


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

Another wonderful feature of the Out-GridView cmdlet is its Filter capabilities. If I use the Filter bar at the top it will search every field displayed for whatever text I type in there. Instead of using that, I will go ahead click “Add Criteria” and choose Name so that I can ensure that I’m only filtering down my list of databases by their name and not some other field.


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

Next I will add the letters “DW” which will give me back a list of only databases which have the letters DW somewhere in their name. Be sure that all rows are highlighted (selected).


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

Once I click on the “OK” above, I will see that only the 4 databases I selected are being backed up.


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

PowerShell Gets Even Easier

If you weren’t already convinced that PowerShell is easier for backing up SQL Server databases, at least for ad-hoc backups, let’s throw in some backup options. Going back once again to the simple example at the top, just after the Backup-SqlDatabase cmdlet let’s add the -CompressionOption parameter now hit the space bar and you should be presented with a list of available options like this:


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

Note: I am working in the PowerShell ISE, other editors may have different mechanisms for triggering the Parameter Set, hitting Ctrl + Space Bar might trigger this for you in the PowerShell console.

Get-SqlDatabase -ServerInstance localhost |
Out-GridView -PassThru |
Backup-SqlDatabase -CompressionOption On

Now you’ve very easily turned on backup compression and won’t take up as much space on disk as you play around with these commands.

-Script Parameter for Fun & Practice

Before we go much further, it’s probably best that I point out the -Script parameter to you. Adding the -Script parameter to the end of your Backup-SqlDatabase command will allow you to output the command that is being generated without actually running it. This will allow you to play along as I go through these scripts without having to wait for backups to complete or take up a ton of space.

For those of you who are curious about what’s going on behind the scenes: Whenever you run this Powershell cmdlet against SQL Server, all that is run on the server is T-SQL. That’s' it.


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

More Backup Parameters

Let’s look at some other optional parameters for this cmdlet. If you want to be able to poke around yourself while reading through this article use the Get-Help cmdlet, get the ‘ full ’ help and you will find the parameters listed out right out at the top.

Get-Help

You may also find the -ShowWindow option handy. This will pop up that Help file in a separate window so that you don’t have to keep scrolling back up or re-running the command. The “Find” search box at the top can be very helpful here since this particular cmdlet has a rather long Help file.

Get-Help -BackupAction

Using the -BackupAction parameter you can choose to do a Database backup, File backup, or Log backup. (By default, PowerShell assumes you want to do a full database backup, so if you don’t specify otherwise, that’s what it will do.) If you’d like to do a Differential backup, throw the -Incremental parameter (I have no idea what they didn’t just call that parameter -Differential or make it an option on the -BackupAction parameter.


Image may be NSFW.
Clik here to view.
Backing Up SQL Server Databases is Easier in PowerShell than T-SQL

(Note that Differential is not in this list because it is handled by the separate -Incremental parameter.)

-DatabaseFile & -DatabaseFileGroup

The Backup-SqlDatabase cmdlet can also backup just a single data file or file group for you. If you choose Files as the option for your -BackupAction parameter, you will need to add either the -DatabaseFile or the -DatabaseFileGroup parameter, followed by the name of the data file or file group that you want to backup.

-BackupFile Now that you’ve chosen which type of backup you want to do it’s probably good to give the backup a file name. If you don’t, the Backup-SqlDatabase cmdlet will just backup to a file with the name of the

Viewing all articles
Browse latest Browse all 3160

Trending Articles