12 Days of dbatools – Tenth Day2 min read

powershell logo3


On the first day of Christmas dbatools gave to me: Find-DbaCommand

On the second day of Christmas dbatools gave to me: Get-DbaCmsRegServer

On the third day of Christmas dbatools gave to me: DbaMaxMemory

On the fourth day of Christmas dbatools gave to me: DbaAgentJob

On the fifth day of Christmas dbatools gave to me: DbaLogin

On the sixth day of Christmas dbatools gave to me: DbaTempDbConfig

On the seventh day of Christmas dbatools gave to me: DbaAgentAlert

On the eighth day of Christmas dbatools gave to me: DbaDbCompression

On the ninth day of Christmas dbatools gave to me: Backup-DbaDatabase

On the tenth day of Christmas dbatools gave to me: Restore-DbaDatabase


Command Information:

Today we will work with the big brother of yesterday’s command; Restore-DbaDatabase.  This command has a very wide scope of things it can do for us from taking a backup and restoring everything as is to a new server or we can change the name we are restoring or the data files or location.  The command is very versatile for making fit the output you want.  You can take a directory of backup files and this command will scan them and build the sequence of restores that need to be completed based on your parameter input.

Command Description: 

Restore-DbaDatabase

Upon being passed a list of potential backups files this command will scan the files, select those that contain SQL Server
backup sets. It will then filter those files down to a set that can perform the requested restore, checking that we have a
full restore chain to the point in time requested by the caller.

The function defaults to working on a remote instance. This means that all paths passed in must be relative to the remote instance.
XpDirTree will be used to perform the file scans

Various means can be used to pass in a list of files to be considered. The default is to non recursively scan the folder
passed in.

Command Example:

Easy, peasy!!! Just pass in a SQL Instance and a path to your BAK files and the command does all the work for you.  

Advanced Command Usage:

So lets say you wanted to build a fancy dbatools server and restore all of your databases on to the server, but use different database name and filenames.  Here you go…

With the parameters set for DestinationDataDirectory and DestinationLogDirectory you can move your files to a location of your choosing.  And with DestinationFilePrefix you can append a prefix to the beginning of the filename.  And lastly you can append a prefix to the database name you are assigning to the restored database by passing something to the RestoredDatabaseNamePrefix.  These parameters along with a lot more make it very flexible to customize your database restores.

 

Leave a Reply

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