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

前端 未结 17 1336
独厮守ぢ
独厮守ぢ 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: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')
    

提交回复
热议问题