When you become involved with the SQL community, you see a lot of great tools contributed to make life easier for the rest of us. The function of these tools range greatly, from helping to analyze your system state, collect data about your enterprise, or performing regular maintenance. I’ve used many of these tools over the years and can’t imagine being a DBA without them. Well, now it’s time to give back.
A little over two years ago, I posted a Powershell script for building database restores. It has been very handy and I’ve used it a lot over the years. However, it always felt like it could use some polish. I began work a few months ago to add that polish and was able to debut this output at SQL Saturday Sacramento a few weeks ago. Now I want to share that same output with you here on my blog.
I’ve compiled the previous Powershell script into a module, making it easily transportable and allowing DBAs to use it more effectively as a tool. No messing with script files, all you need to do to use it is download the module file and then import it into your Powershell session. The file is now available on my new GitHub repository and can be accessed via the following links:
To install the module, follow these steps:
- Open one of your default Powershell module locations ($env:PSModulePath). I recommend your user specific one.
- Create a folder named RestoreAutomation in that locatoin
- Place the RestoreAutomation.psm1 file in the new folder.
Full Microsoft install instructions can be found here.
To load the module once you’ve installed it, just open a Powershell session and enter Import-Module RestoreAutomation. After that, you can see the available functions by using Get-Command -Module RestoreAutomation, which will provide the following list:
CommandType Name ModuleName ----------- ---- ---------- Function Get-DBCCCheckDB RestoreAutomation Function Get-Header RestoreAutomation Function Get-RestoreObject RestoreAutomation Function New-Restore RestoreAutomation Function Sync-DBUsers RestoreAutomation
Using the module
While five functions are listed, two of them are internal and used by the core restore process to generate the necessary code. I’ll cover the primary three functions here, but I have made full use of Powershell’s comment based help, so you can use Get-Help on these functions at any time to get information on how to use them.
This is the core functionality that was created from my initial script. When you run this function, it will create a .sql script in your Documents folder to restore the associated files. The function takes a directory path and then search that directory path for all backup files within it, ordering them appropriately (latest full, then latest differential, then the right sequence of transaction logs).
You’ll see that the function has a lot of parameters. The only two required are:
- dir – The directory where the necessary backup files live
- server – A SQL Server instance where the script generation commands will be run (but not necessarily restored to)
I will not cover the optional parameters in detail here, but review Get-Help New-Restore -full for a full listing of the parameters and what they do.
The function makes the following assumptions:
- All the backup files in the directory belong to one database only.
- Backup file types are defined by extension (.bak for full, .dff for differential, and .trn for transaction log).
- No striped backup files, meaning a database backup will be contained on only one file.
- No backup sets, meaning a backup file only contains a single database backup.
To use the function, simply get a directory of backup files and run a command similar to:
New-Restore -dir “X:\backups\db” -server “localhost”
Then, look in your Documents folder for a restore_db.sql file. This will be your database restore script.
If you want to test drive it yourself using a dummy database, I’ve provided a full demo script and files for download.
This function I added to help me with database migrations. Often, I’ll migrate a database via backup and restore. Afterwards I always need to clean up the orphan logins. This function is a wrapper for SQL queries that I use to do this, providing me an easy to use interface for managing the process.
The function accepts only two parameters:
- server – The SQL instance that the database resides on
- database – Database that contains users you wish to correct
To run the function, just call your server and database name:
Sync-DBUsers -server “localhost” -database “restoredemo”
The function will then find all orphaned users and correct them. Any orphans it can not find corresponding logins for, it will return as an output list for review and correction.
When building out my automated restore process, I always wanted to do a DBCC CHECKDB on the restored database to validate everything was ok. So I wrote this function in order to give me a manageable interface for the process. The function will then run a DBCC CHECKDB with PHYSICAL_ONLY on my target database and return the results.
The function has three parameters:
- server – The SQL instance that the database resides on
- database – Database that you want to check
- Full – Switch parameter. If used, the function will execute a full DBCC CHECKDB. If omitted, it will only perform a check with PHYSICAL_ONLY
To use the function, call the server and database name:
Get-DBCCCheckDB -server “localhost” -database “restoredemo”
The output will be a data table of the check results.
Only the beginning
Hopefully this module can help you as much as it has helped me. The point is to use this as a tool to facilitate other tasks. Many of these tasks I will provide examples of over the next month or so, to demonstrate how you can leverage this in your own environment.
I already have a list of enhancements that I plan to add. I expect that as folks out there review it and play with it, I’ll get more. Please do not hesitate to post suggestions and comments to the GitHub repository. The idea, as with many of these other tools, is to build a reliable offering that other DBAs can use in their day to day jobs to make things easier and allow them to work on more important tasks and projects.