Franklin,TN 37067
+1 (888) 412-7376
info@theserogroup.com

Script: How Long Until My SQL Server Backup/Restore Completes?

Script: How Long Until My SQL Server Backup/Restore Completes?

Ding!  Your desktop IM client chimes. “Any idea how long my SQL Server backup/restore will take to complete? I think it may be stuck.” 

Someone has started a database restore (or backup) using a T-SQL command. Now he wants to know how long the process will take to complete. He’s gone to the bathroom and even gotten a coffee, yet it’s still running. 

If he had used the WITH STATS option for either the BACKUP DATABASE or RESTORE DATABASE command, the percentage processed would have been piped to the Messages tab of Management Studio a little at a time. 10 percent processed. 20 percent processed. And so on as shown below.

BACKUP DATABASE wideworldimporters TO DISK = 'c:\temp\wwi.bak' WITH STATS;

SQL Server backup/restore information using DMVs

Still, that doesn’t provide an estimate of how long the process is likely to run. Fortunately, the Dynamic Management Views in SQL Server allow us to gather and report backup and restore estimates.

Here’s a script that I’ve found useful. It’s an amalgamation of a script I found on StackOverflow and my own handiwork. As always, this is for your reference only and no guarantees are implied.

SELECT SERVERPROPERTY('ServerName') AS [Instance],
   reqs.session_id,
   sess.login_name,
   reqs.command,
   CAST(reqs.percent_complete AS NUMERIC(10, 2)) AS [Percent Complete],
   CONVERT(VARCHAR(20), DATEADD(ms, reqs.estimated_completion_time, GETDATE()), 20) AS [Estimated Completion Time],
   CAST(reqs.total_elapsed_time / 60000.0 AS NUMERIC(10, 2)) AS [Elapsed Minutes],
   CAST(reqs.estimated_completion_time / 60000.0 AS NUMERIC(10, 2)) AS [Estimated Remaining Time in Minutes],
   CAST(reqs.estimated_completion_time / 3600000.0 AS NUMERIC(10, 2)) AS [Estimated Remaining Time in Hours],
   CAST((
     SELECT SUBSTRING(text, reqs.statement_start_offset/2,
                CASE
                WHEN reqs.statement_end_offset = -1
                THEN 1000
                ELSE(reqs.statement_end_offset-reqs.statement_start_offset)/2
                END)
     FROM sys.dm_exec_sql_text(sql_handle)) AS VARCHAR(1000)) AS [SQL]
FROM sys.dm_exec_requests AS reqs
 JOIN sys.dm_exec_sessions AS sess ON sess.session_id = reqs.session_id
WHERE command IN('RESTORE DATABASE', 'BACKUP DATABASE');

The script returns the following information:

  • Instance Name
  • Session Id 
  • Login Name
  • Command 
  • Percent Complete
  • Estimated Completion Time
  • Elapsed Time in Minutes
  • Estimated Remaining Time in Minutes
  • Estimated Remaining Time in Hours
  • The Complete Command Used to Start Backup/Restore

Since this script uses Dynamic Management Views, it won’t work on ancient versions of SQL Server. I’ve tested it on SQL Server 2008 R2 and forward on the Windows version. I haven’t tried it on SQL Server 2017 running on Linux. 

Hopefully, you’ll find this script handy. Here are a few more that you may find helpful.

Want to work with The Sero Group?

Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? It’s easy and there is no obligation. 

Schedule a call with us to get started.

 

3 Responses

  1. […] The ability to restore a database starts with having a good backup. We prefer native backups and in our minds, there’s no better way to manage your native backups than using Ola Hallegren’s Award winning scripts. It’s our tool of choice when it comes to SQL backups. (Note: when/if you run a SQL Server backup manually, you can use this script to see how much longer the backup will take.) […]

  2. […] the way, the sys.dm_exec_requests can be used for other purposes. For example, in Script: How Long Until My SQL Server Backup/Restore Completes?, I use it to estimate how much longer a SQL backup or restore will […]

  3. […] Script: How Long Until My SQL Server Backup/Restore Completes? […]

Leave a Reply

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