当我使用MS SQL Server Management Studio备份或恢复数据库时,我可以直观地看到进程进展的程度,以及我还需要等待多长时间才能完成.如果我用脚本开始备份或恢复,有没有办法监控进度,或者我只是坐下来等待它完成(希望没有出错?)
编辑:我的需要是能够完全独立于启动备份或恢复的会话监视备份或恢复进度.
我在这里发现这个示例脚本似乎工作得很好:
SELECT r.session_id,r.command,CONVERT(NUMERIC(6,2),r.percent_complete) AS [Percent Complete],CONVERT(VARCHAR(20),DATEADD(ms,r.estimated_completion_time,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], CONVERT(VARCHAR(1000),(SELECT SUBSTRING(text,r.statement_start_offset/2, CASE WHEN r.statement_end_offset = -1 THEN 1000 ELSE (r.statement_end_offset-r.statement_start_offset)/2 END) FROM sys.dm_exec_sql_text(sql_handle))) AS [SQL] FROM sys.dm_exec_requests r WHERE command IN ('RESTORE DATABASE','BACKUP DATABASE')
如果您知道sessionID,那么您可以使用以下内容:
SELECT * FROM sys.dm_exec_requests WHERE session_id = 62
或者如果你想缩小范围:
SELECT command, percent_complete, start_time FROM sys.dm_exec_requests WHERE session_id = 62
是.如果已将sp_who2k5安装到master数据库中,则只需运行:
sp_who2k5 1,1
结果集将包括所有活动事务.当前运行的备份将在requestCommand字段中包含字符串"BACKUP" .恰当命名的percentComplete字段将为您提供备份的进度.
注意:sp_who2k5应该是每个人工具包的一部分,它不仅仅是这个.
这是一个简单的脚本,通常可以帮助我:
SELECT command, percent_complete,total_elapsed_time, estimated_completion_time, start_time FROM sys.dm_exec_requests WHERE command IN ('RESTORE DATABASE','BACKUP DATABASE')
SELECT session_id as SPID, command, a.text AS Query, start_time, percent_complete, dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE')
检查SQL Server中备份和还原进度的脚本:
很多时候,您的备份(或还原)活动已由另一个数据库管理员或作业启动,并且您无法使用GUI来检查该备份/还原的进度。
通过组合多个命令,我生成了以下脚本,该脚本可以为我们提供服务器上正在发生的当前备份和还原的摘要。
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')