Home > SQL Server Tips > Microsoft SQL Server > Stored procedures: Who is running backups and restores and when
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Stored procedures: Who is running backups and restores and when


Greg Robidoux, Edgewood Solutions
07.27.2006
Rating: -4.75- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Backup and restore commands can be issued by running T-SQL, Enterprise Manager or Maintenance Plans. However, it's still not easy to determine who issued backups and restores -- and when.

Such information is stored in the msdb database; an entry is logged in msdb whenever a backup or restore runs. This data is not easy to access via the GUI tools, but it can be retrieved through queries. The following tables contain backup and restore information whenever one of these commands is issued.

Backup tables

  • backupfile -- contains one row for each data file or log file backed up
  • backupmediafamily -- contains one row for each media family
  • backupmediaset -- contains one row for each backup media set
  • backupset -- contains one row for each backup set

Restore tables

  • restorehistory -- contains one row for each restore run
  • restorefilegroup -- contains one row for each filegroup restored
  • restorefile -- contains one row for each physical file restored

You can query these tables individually, but to fully make sense of the data you must query the tables together. To simplify the process, the following stored procedures can be created in the msdb database or in one of your user databases. The first stored procedure queries the backup history and the second queries the restore history.

Backup History Examples
List all backups for database "master" between 7/15/2006 and 7/25/2006 dbo.uspGetDBBackupInfo '2006-07-15', '2006-07-25', 'master'
List all backups that were run between 7/15/2006 and 7/25/2006 dbo.uspGetDBBackupInfo '2006-07-15', '2006-07-25'
CREATE PROC dbo.uspGetDBBackupInfo 
           @startDate DATETIME,
           @endDate   DATETIME,
           @database  SYSNAME  = NULL
AS
  SELECT   b.database_name,
           b.backup_start_date,
           b.backup_finish_date,
           b.user_name,
           f.logical_name,
           f.physical_name,
           mf.physical_device_name,
           f.file_type,
           f.file_size,
           b.backup_size
  FROM     msdb.dbo.backupfile f,
           msdb.dbo.backupset b,
           msdb.dbo.backupmediafamily mf
  WHERE    f.backup_set_id = b.backup_set_id
           AND b.media_set_id = mf.media_set_id
           AND b.backup_start_date BETWEEN @startDate
                                           AND @endDate
           AND b.database_name = COALESCE
(@database,database_name)
  ORDER BY b.database_name,
           b.backup_start_date

Backup History Output
Output Column Description
b.database_name Name of database that was backed up
b.backup_start_date Start date and time of the backup
b.backup_finish_date End date and time of the backup
b.user_name User that ran the backup
f.logical_name Logical name of the database file
f.physical_name Physical name and location of the database file
mf.physical_device_name Name and location of the physical backup file that was created
f.file_type Type of backup D – Full, L – Transaction , I – differential
f.file_size Size of the physical database files in bytes
b.backup_size Size of the physical backup file in bytes

Restore History Examples
List all restores for database "master" between 7/15/2006 and 7/25/2006. dbo.uspGetDBBackupInfo '2006-07-15', '2006-07-25', 'master'
List all restores that were run between 7/15/2006 and 7/25/2006. dbo.uspGetDBBackupInfo '2006-07-15', '2006-07-25'
CREATE PROC dbo.uspGetDBRestoreInfo
           @startDate DATETIME,
           @endDate   DATETIME,
           @database  SYSNAME  = NULL
AS
  SELECT   h.destination_database_name,
           h.restore_date,
           h.user_name,
           h.restore_type,
           f.destination_phys_name,
           fg.filegroup_name
  FROM     msdb.dbo.restorehistory h,
           msdb.dbo.restorefile f,
           msdb.dbo.restorefilegroup fg
  WHERE    h.restore_history_id = f.restore_history_id
           AND h.restore_history_id = fg.restore_history_id
           AND h.restore_date BETWEEN @startDate
                                      AND @endDate
           AND h.destination_database_name = COALESCE
(@database,destination_database_name)
  ORDER BY h.destination_database_name,
           h.restore_date

Restore History Output
Output Column Description
h.destination_database_name Name of database the backup was restored to
h.restore_date Date and time of the restore
h.user_name User that ran the restore
h.restore_type Type of restore D – Full, L – Transaction , I – differential
f.destination_phys_name Physical name and location of the database file
fg.filegroup_name Name of the filegroup that was restored

These two simple, but effective stored procedures can help shed light on when backups and restores are being issued against your server. You can run the stored procedures every day, so you can be sure backups are running properly. Also, when you need to restore, use the history to see the order of the backups and which backup files exist and where they are physically located.

About the author: Greg Robidoux is the president and founder of Edgewood Solutions LLC, a technology services company delivering professional services and product solutions for Microsoft SQL Server. He has authored numerous articles and has delivered presentations at regional SQL Server users' groups and national SQL Server events. Robidoux, who also serves as the Backup and Recovery expert welcomes your questions.

Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL Server backup and recovery
Solve SQL Server errors and more from the DBA trenches -- part 2
Licensing a standby server for SQL Server replication
Can I encrypt and restore a database backup in SQL Server 2005?
SQL Server errors, failures and other problems fixed from the trenches
Get SQL Server log shipping functionality without Enterprise Edition
SQL Server 2008 backup compression pros and cons
SQL Server backups using SAN database snapshots
Tips for scheduling and testing SQL Server backups
Code to restore SQL Server databases in VB.NET
Tricking SQL Server into making full database backups
SQL Server backup and recovery Research

SQL Server stored procedures
SQL Server Service Broker Tutorial and Reference Guide
SQL Server trigger vs. stored procedure to receive data notification
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Track changes to SQL Server 2000 and 2005 with one simple utility
Troubleshoot SQL Server 2005 temporary table performance problems

Microsoft SQL Server
Avoid cursors in SQL Server with these methods to loop over records
Implementing security audit in SQL Server 2008
What's new in SQL Server 2008 Reporting Services?
SQL Server replication methods: Snapshot, merge or transactional
New security features in SQL Server 2008 leave some work for you
How to disable the shrink database task in SQL Server 2000 and 2005
New datetime data types in SQL Server 2008 offer flexibility
SQL Server out of memory: Troubleshoot and avoid SQL memory problems
SQL Server errors, failures and other problems fixed from the trenches
SQL Server consolidation: Why it's an optimization technique

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
rollback  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts