Skip to content

Automate and Simplify DBCC CHECKDB

T-SQL Tuesday Logo

As a part of the T-SQL Tuesday run by Rob Sewell this month, I wanted to throw in a little post about using Test-DbaLastBackup which I will also be showing off a this year’s SQL Summit!

 

Come to the Precon

If you like this post, don’t forget to signup for me and Chrissy Lemaire’s precon at the 2017 SQL Summit for more awesome PowerShell <3 SQL Server magic with dbatools!

Test-DbaLastBackup

As data professionals we know that safeguarding our data is one of our most important jobs, and yet a significant amount of us continue to find excuses not to verify the integrity of our data.

I want to show you an uber simple method to automate testing your entire estate worth of backups.

Before we move forward, I want to note this process does perform a restore and DBCC checkdb on the server you specify, so please don’t run this in production and come to me upset that your IO and CPU time shot through the roof 😀

However, if you only have a smallish or sleepy production environment, this will run just fine side by side, otherwise I demonstrate a parameter to run this on a different server.

Lastly, before we begin, this assumes you’ve already installed and imported dbatools, because why wouldn’t you have?

Code

This:

  • Defines a list of two servers (PowerShell lists are as easy as “”,””)
  • Pipes them to the Test-DbaLastBackup command.
  • Which then:
    • Gathers information about the last full backups for all of your databases on that instance.
    • Restores the backups to the Destination with a new name. If no Destination is specified, the originating server will be used.
    • The database is restored as “dbatools-testrestore-$databaseName” by default, which you can change with the -Prefix parameter.
    • The internal file names are also renamed to prevent conflicts with original database.
    • A DBCC CHECKTABLE is then performed.
    • And the test database is finally dropped.

If you are super stuck on the restore portion but would like to do a VERIFYONLY, there is a -VerifyOnly parameter switch.

Some other useful parameters you might need to use in a typical scenario:

-Database Allows you to specify a list of databases to specifically check, useful if you have just one in mind.

-MaxMB Prevents databases over X MB from being restored.

-ExcludeDatabase Exclude specific databases (for that one gigantic one you know you don’t want to touch.)

-DataDirectory Alternate data directory (besides default) you want to restore the database files to test.

-LogDirectory Alternate log directory (besides default) you want to restore the database files to test.

Watch

If you like video, wait no more, here’s a youtube demo of Test-DbaLastBackup

Thanks for your time, and I hope you enjoyed.

Published inUncategorized

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *