SQL-Server: Is there a SQL script that I can use to determine the progress of a SQL Server backup or restore process?

前端 未结 17 1281
独厮守ぢ
独厮守ぢ 2020-12-04 06:16

When I backup or restore a database using MS SQL Server Management Studio, I get a visual indication of how far the process has progressed, and thus how much longer I still

相关标签:
17条回答
  • 2020-12-04 06:42

    Try wih :

    SELECT * FROM sys.dm_exec_requests where command like '%BACKUP%'
    
    SELECT command, percent_complete, start_time FROM sys.dm_exec_requests where command like '%BACKUP%'
    
    SELECT command, percent_complete,total_elapsed_time, estimated_completion_time, start_time
      FROM sys.dm_exec_requests
      WHERE command IN ('RESTORE DATABASE','BACKUP DATABASE')
    
    0 讨论(0)
  • 2020-12-04 06:42

    I had a similar issue when working on Database restore operation on MS SQL Server 2012.

    However, for my own scenario, I just needed to see the progress of the DATABASE RESTORE operation in the script window

    All I had to do was add the STATS option to the script:

    USE master;
    GO
    
    ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    GO
        
    RESTORE DATABASE mydb
        FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\my_db_21-08-2020.bak'
        WITH REPLACE,
             STATS = 10,
             RESTART,
        MOVE 'my_db' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db.mdf',
        MOVE 'my_db_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\mydb_log.ldf'
    GO
        
    ALTER DATABASE mydb SET MULTI_USER;
    GO
    

    And then I switched to the Messages tab of the Script window to see the progress of the DATABASE RESTORE operation:

    If you want to get more information after the DATABASE RESTORE operation you can use this command suggested by eythort:

    SELECT command, percent_complete, start_time FROM sys.dm_exec_requests where command = 'RESTORE DATABASE'
    

    That's all.

    I hope this helps

    0 讨论(0)
  • 2020-12-04 06:47

    SELECT session_id as SPID, command, start_time, percent_complete, dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time, a.text AS Query FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a WHERE r.command in ('BACKUP DATABASE', 'BACKUP LOG', 'RESTORE DATABASE', 'RESTORE LOG')

    0 讨论(0)
  • 2020-12-04 06:49

    To monitor the backup or restore progress completely separate from the session where the backup or restore was initiated. No third party tools required. Tested on Microsoft SQL Server 2012.

    SELECT percent_complete, *
    FROM sys.dm_exec_requests
    WHERE command In ( 'RESTORE DATABASE', 'BACKUP DATABASE' )
    
    0 讨论(0)
  • 2020-12-04 06:50

    Script to check the Backup and Restore progress in SQL Server:

    Many times it happens that your backup (or restore) activity has been started by another Database Administrator or by a job, and you cannot use the GUI anything else to check the progress of that Backup / Restore.

    By combining multiple commands, I have generated below script which can give us a summary of current backups and restores which are happening on the server.

    select 
    r.session_id, 
    r.blocking_session_id, 
    db_name(database_id) as [DatabaseName],
    r.command, 
    [SQL_QUERY_TEXT] = Substring(Query.TEXT, (r.statement_start_offset / 2) + 1, (
                (
                    CASE r.statement_end_offset
                        WHEN - 1
                            THEN Datalength(Query.TEXT)
                        ELSE r.statement_end_offset
                        END - r.statement_start_offset
                    ) / 2
                ) + 1),
                    [SP_Name] =Coalesce(Quotename(Db_name(Query.dbid)) + N'.' + Quotename(Object_schema_name(Query.objectid, Query.dbid)) + N'.' + 
         Quotename(Object_name(Query.objectid, Query.dbid)), ''),
    r.percent_complete,
    start_time,
    CONVERT(VARCHAR(20), DATEADD(ms, [estimated_completion_time],
    GETDATE()), 20) AS [ETA_COMPLETION_TIME],
    CONVERT(NUMERIC(6, 2), r.[total_elapsed_time] / 1000.0 / 60.0) AS [Elapsed_MIN],
    CONVERT(NUMERIC(6, 2), r.[estimated_completion_time] / 1000.0 / 60.0) AS [Remaning_ETA_MIN],
    CONVERT(NUMERIC(6, 2), r.[estimated_completion_time] / 1000.0 / 60.0/ 60.0) AS [ETA_Hours],
    wait_type,
    wait_time/1000 as Wait_Time_Sec, 
    wait_resource
    from sys.dm_exec_requests r 
    cross apply sys.fn_get_sql(r.sql_handle) as Query where r.session_id>50 and command IN ('RESTORE DATABASE','BACKUP DATABASE', 'RESTORE LOG', 'BACKUP LOG')
    
    0 讨论(0)
  • 2020-12-04 06:50

    Use STATS in the BACKUP command if it is just a script.

    Inside code it is a bit more complicated. In ODBC for example, you set SQL_ATTR_ASYNC_ENABLE and then look for SQL_STILL_EXECUTING return code, and do some repeated calls of SQLExecDirect until you get a SQL_SUCCESS (or eqiv).

    0 讨论(0)
提交回复
热议问题