Tracking the progress of a database backup or restore can typically be performed, to some degree, within the GUI of SQL Management Studio by the user who is actually performing the backup/restore. Though the progress indicator in Management Studio is mostly reliable, there are times when another user on a team may perform a backup or restore and, when this is the case, it can be difficult to determine the overall progress without the use of 3rd party software.
I recently ran into this scenario as one of the other Database Administrators on my team kicked off a restore of a large lab database before leaving for the day. After about an hour and a half I began to question whether there was an issue with the restore. Navigating to the database within SQL Management Studio showed that the database was still in the process of restoring, though the time it was taking seemed to exceed my expectations.
Fortunately, Microsoft has provided a couple useful Dynamics Management Views (DMVs) which, with a little modification, can make querying this information relatively easy.
The script below queries the
sys.dm_exec_requests management views to provide useful information regarding any active backup or restore processes.
CONVERT(NUMERIC(6,2),r.percent_complete) AS [Percent Complete],
GETDATE()),20) AS [ETA Completion Time],
CONVERT(NUMERIC(10,2),r.total_elapsed_time/1000.0/60.0) AS [Elapsed Min],
CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0) AS [ETA Min],
CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0/60.0) AS [ETA Hours],
CASE WHEN r.statement_end_offset = -1
ELSE (r.statement_end_offset-r.statement_start_offset)/2 END)
FROM sys.dm_exec_requests r WHERE command IN ('RESTORE DATABASE','BACKUP DATABASE')
Though this information can be captured by simply querying the
sys.dm_exec_requests DMV and adding a
WHERE clause to look for
'RESTORE DATABASE' and
'BACKUP DATABASE' making some changes to the way the data is presented will allow for easier interpretation of the data.
In the script above I am using the
CONVERT function to provide an easier view of the relevant data.
The screenshot below shows that two databases are currently in the process of being restored.
Included in the output is the Session ID (spid), command being executed, percentage complete, ETA Completion Time, Elapsed time in minutes, ETA (min), ETA (hours) and an additional column which includes the SQL script being executed to perform the backup or restore.
The ETA metrics will constantly adjust based on the overall progress of the database restore. Though this is an estimation, I have found that it is close enough to gauge an approximate time frame for when the process will be complete.