Monday, December 23, 2013

Restore master database with Dell NetVault Litespeed for SQL Server

The DBA teams asked if I could create a job in our maintenance routines to do a native backup of the master database instead of a Litespeed backup. Their reason was they weren't able to restore master using Litespeed because the database server had to be in single-user mode to do such a restore and Litespeed required two connections to the server to do the task.

I mentioned that there are instructions in the Litespeed help files for a master database restore.

The team told me that it didn't work for them. So, I took the task to validate the instructions found here

I took a backup of the master database at 9:44 AM.

Next, I created a login on the server after the backup called ThisLoginWillNotBeHereAfterMasterDatabaseRestore to prove the master database restore did in fact happen.
This login will no longer exist after the upcoming master database restore because it was not captured by the backup taken at 9:44 AM.

I stopped the SQL Server services for the instance.  Either SSMS or SQL Configuration Manager can be used to stop the services.  When you stop the database instance, the corresponding SQL Agent will also be stopped.

I started the database instance in single user mode in one command window.

The instance has started in single user mode.

I open a second command window and ran the database restore command.

The restore runs successfully in seconds.

Additional messages are written to the first command window and the command prompt returns.
I restart the database instance using the SQL Configuration Manager.  
I start SQL Server Management Studio to find the login created after the backup no longer exists proving the master database has been restored.

The database restore was completed in a few seconds. The entire process probably takes less than 5 minutes.  
I took a bit longer because I was taking screenshots. ;-)

Tuesday, December 10, 2013

There is more than one way to find database backup files

I was told during the initial implementation of Ola Hallengren's Maintenance Solution at my workplace that it was too hard to find database backup files in the default folder structure his solution creates.  The concern was that it took too long to find the oldest file or the biggest file when trying to resolve a space issue on a database server.

Ola's solution creates a folder tree with these elements. Drive:\root\ServerName\DatabaseName\TypeOfBackup

You specify the backup drive and root folder using the Directory parameter of the DatabaseBackup stored procedure.

When I originally solved this problem, Windows XP was in use.  I wish I had spent more time figuring this out because I had to modify Ola's database backup procedure to dump all the backups into one folder in order for the DBA teams to sign off on the initial deployment.

It only takes a couple of minutes to search using Windows Explorer in Windows XP but finding files in a folder tree has gotten much easier on Windows 7 and higher.  

Given Windows XP's imminent retirement, I will only cover Windows 7 or higher.

Start Windows Explorer and navigate to the backup root folder on the server.

Type *.bak in the Search field in the upper right hand corner of Windows Explorer on Windows 7 or 8 and press Enter.

By default, Windows Explorer in Windows 7 and higher searches sub-folders.

The Search result returns all the database backups in the entire folder tree.
The two requirements given were finding files by size or date.

Both are easily solved by clicking on the appropriate header. 
In the search result, headers are clickable and allow sorting by any column. 
So, click on Date Modified to find files by date or click on Size to find files by size.
Finding files to delete by size or date is done in less than a minute.
You can even save searches now.

Microsoft's GUI interfaces are sufficient if you only have a few servers to check. But GUIs don't scale when you need to do the same task on a few hundred servers. So, you'll need to do a bit of scripting.  
Google answered this question pretty quickly.
Find the ten largest files in a directory.

get-childitem -path  C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object Length -desc | select-object fullname -f 10
gci C:\Backup -r |  ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10

Finding the oldest ten files in a folder tree by date
get-childitem C:\Backup  -recurse | ?{ -not $_.PSIsContainer } | sort-object LastWriteTime | select-object fullname -f 10
gci C:\Backup  -r | ?{ -not $_.PSIsContainer } | sort LastWriteTime | select fullname -f 10
gci \\ServerName\FolderName\dump_data  -r |  ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10

Including ?{ -not $_.PSIsContainer } in the pipeline ensures only files not folders are included in the results. 

If you want to run these commands on multiple servers, wrap them in a foreach loop that reads a list of text files from a server. Check out my PowerShell posts for examples. 

Old School
If you are not comfortable with PowerShell, the venerable dir command from the Windows command line is still available.

Oldest files first in a folder tree
dir /S /OD | more

Files ordered by size largest first in folder tree
dir /S /O-S | more

So, it doesn't take very long to find the largest or oldest database backup using any of these methods.  Which means I didn't need to modify Ola's scripts for the next release. ;-)