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. ;-)

No comments:

Post a Comment