Monday, September 21, 2015

SQL Server DB Backup/Restore Progress Script

When a database backup is initiated in SQL Server either using the SSMS GUI or a Script in SSMS we see the progress of the database in increments of 5 or 10 percent. Using the script below we can know the exact percentage of the task complete and also get an estimated amount of time remaining. At times when a large database is being restore for it to show a progress till 5% might take time which might not be happening at all and will throw an exception for various reasons. In such cases the below script can be used after the action has been invoked to see the progress immediately.

SQL Server DB Backup progress can be known using the script below

 SELECT A.NAME,B.TOTAL_ELAPSED_TIME/60000 AS [Running Time],  
 B.ESTIMATED_COMPLETION_TIME/60000 AS [Remaining],  
 B.PERCENT_COMPLETE as [%],(SELECT TEXT FROM sys.dm_exec_sql_text(B.SQL_HANDLE))AS COMMAND FROM  
 MASTER..SYSDATABASES A, sys.dm_exec_requests B  
 WHERE A.DBID=B.DATABASE_ID AND B.COMMAND LIKE '%backup%'  
 order by percent_complete desc,B.TOTAL_ELAPSED_TIME/60000 desc  

Similarly, we can also know the SQL Server DB Restore progress using the script below

 SELECT A.NAME,B.TOTAL_ELAPSED_TIME/60000 AS [Running Time],  
 B.ESTIMATED_COMPLETION_TIME/60000 AS [Remaining],  
 B.PERCENT_COMPLETE as [%],(SELECT TEXT FROM sys.dm_exec_sql_text(B.SQL_HANDLE))AS COMMAND FROM  
 MASTER..SYSDATABASES A, sys.dm_exec_requests B  
 WHERE A.DBID=B.DATABASE_ID AND B.COMMAND LIKE '%restore%'  
 order by percent_complete desc,B.TOTAL_ELAPSED_TIME/60000 desc  

No comments: