Skip to content

Commit cac0db0

Browse files
authored
Merge pull request #34190 from Stralle/patch-6
Update backup-activity-monitor.md
2 parents bd56b73 + 215f944 commit cac0db0

File tree

1 file changed

+97
-88
lines changed

1 file changed

+97
-88
lines changed
Lines changed: 97 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,99 @@
11
---
2-
title: "Monitor backup activity"
2+
title: "Monitor Backup Activity"
33
titleSuffix: Azure SQL Managed Instance
44
description: Learn how to monitor Azure SQL Managed Instance backup activity by querying the `msdb` database, and by using extended events.
5-
author: Stralle
6-
ms.author: strrodic
7-
ms.reviewer: mathoma, nvraparl
8-
ms.date: 11/16/2022
5+
author: dinethi
6+
ms.author: dinethi
7+
ms.reviewer: mathoma, strrodic, randolphwest
8+
ms.date: 06/24/2025
99
ms.service: azure-sql-managed-instance
1010
ms.subservice: backup-restore
1111
ms.topic: quickstart
12-
ms.custom: mode-other
12+
ms.custom:
13+
- mode-other
1314
---
1415
# Monitor backup activity for Azure SQL Managed Instance
15-
[!INCLUDE[appliesto-sqlmi](../includes/appliesto-sqlmi.md)]
16+
17+
[!INCLUDE [appliesto-sqlmi](../includes/appliesto-sqlmi.md)]
1618

1719
This article teaches you how to monitor backup activity for [Azure SQL Managed Instance](sql-managed-instance-paas-overview.md) by either querying the `msdb` database or by configuring extended event (XEvent) sessions.
1820

1921
## Overview
2022

21-
Azure SQL Managed Instance stores backup information in the [msdb database](backup-transparency.md) and also emits events (also known as [Extended Events or XEvents](../database/xevent-db-diff-from-svr.md)) during backup activity for the purpose of reporting. Configure an XEvent session to track information such as backup status, backup type, size, time, and location within the `msdb` database. This information can be integrated with backup monitoring software and also used for the purpose of Enterprise Audit.
23+
Azure SQL Managed Instance stores backup information in the [msdb database](backup-transparency.md) and also emits events (also known as [Extended Events or XEvents](../database/xevent-db-diff-from-svr.md)) during backup activity, which can be used for reporting. Configure an XEvent session to track information such as backup status, backup type, size, time, and location within the `msdb` database. This information can be integrated with backup monitoring software and also used for Enterprise Audit.
2224

23-
Enterprise Audits may require proof of successful backups, time of backup, and duration of the backup.
25+
Enterprise Audits might require proof of successful backups, time of backup, and duration of the backup.
2426

2527
## Query msdb database
2628

27-
To view backup activity, run the following query from user-defined database:
29+
To view backup activity, run the following query from user-defined database:
2830

2931
```sql
30-
SELECT TOP (30) bs.machine_name, bs.server_name, DB_NAME(DB_ID(bs.database_name)) AS [Database Name], bs.recovery_model,
31-
CONVERT (BIGINT, bs.backup_size / 1048576 ) AS [Uncompressed Backup Size (MB)],
32-
CONVERT (BIGINT, bs.compressed_backup_size / 1048576 ) AS [Compressed Backup Size (MB)],
33-
CONVERT (NUMERIC (20,2), (CONVERT (FLOAT, bs.backup_size) /
34-
CONVERT (FLOAT, bs.compressed_backup_size))) AS [Compression Ratio], bs.has_backup_checksums, bs.is_copy_only, bs.encryptor_type,
35-
DATEDIFF (SECOND, bs.backup_start_date, bs.backup_finish_date) AS [Backup Elapsed Time (sec)],
36-
bs.backup_finish_date AS [Backup Finish Date], bmf.physical_device_name AS [Backup Location], bmf.physical_block_size
32+
SELECT TOP (100)
33+
DB_NAME(DB_ID(bs.database_name)) AS [Database Name],
34+
CONVERT (BIGINT, bs.backup_size / 1048576) AS [Uncompressed Backup Size (MB)],
35+
CONVERT (BIGINT, bs.compressed_backup_size / 1048576) AS [Compressed Backup Size (MB)],
36+
CONVERT (NUMERIC (20, 2),
37+
CASE
38+
WHEN bs.compressed_backup_size > 0
39+
THEN CONVERT (FLOAT, bs.backup_size) / CONVERT (FLOAT, bs.compressed_backup_size)
40+
ELSE NULL
41+
END
42+
) AS [Compression Ratio],
43+
bs.is_copy_only,
44+
-- bs.user_name, -- Applicable only for user-initiated COPY ONLY backups.
45+
bs.has_backup_checksums,
46+
DATEDIFF(SECOND, bs.backup_start_date, bs.backup_finish_date) AS [Backup Elapsed Time (sec)],
47+
bs.backup_finish_date AS [Backup Finish Date],
48+
bmf.physical_block_size
3749
FROM msdb.dbo.backupset AS bs WITH (NOLOCK)
38-
INNER JOIN msdb.dbo.backupmediafamily AS bmf WITH (NOLOCK)
39-
ON bs.media_set_id = bmf.media_set_id
40-
WHERE DB_ID(bs.database_name) = DB_ID()
41-
AND bs.[type] = 'D'
42-
ORDER BY bs.backup_finish_date DESC OPTION (RECOMPILE);
43-
50+
INNER JOIN msdb.dbo.backupmediafamily AS bmf WITH (NOLOCK)
51+
ON bs.media_set_id = bmf.media_set_id
52+
WHERE bs.[type] = 'D'
53+
-- AND bs.[is_copy_only] = 1 -- If you want to filter out for user initiated COPY ONLY backups.
54+
ORDER BY bs.backup_finish_date DESC
55+
OPTION (RECOMPILE); -- Optimize for ad hoc execution
4456
```
4557

46-
## Configure XEvent session
58+
> [!NOTE]
59+
> When querying `msdb` system tables such as `dbo.backupmediaset` or `dbo.backupset`, you see encryption-related fields indicating that backup files aren't encrypted. This status reflects only engine-level encryption. All automatic backups [are encrypted at rest](automated-backups-overview.md#encrypted-backups).
4760
48-
Use the extended event `backup_restore_progress_trace` to record the progress of your SQL Managed Instance back up. Modify the XEvent sessions as needed to track the information you're interested in for your business. These T-SQL snippets store the XEvent sessions in the ring buffer, but it's also possible to write to [Azure Blob Storage](../database/xevent-code-event-file.md). XEvent sessions storing data in the ring buffer have a limit of about 1000 messages so should only be used to track recent activity. Additionally, ring buffer data is lost upon failover. As such, for a historical record of backups, write to an event file instead.
61+
## Configure XEvent session
4962

50-
### Simple tracking
63+
Use the extended event `backup_restore_progress_trace` to record the progress of your SQL Managed Instance back up. Modify the XEvent sessions as needed to track the information you're interested in for your business. These T-SQL snippets store the XEvent sessions in the ring buffer, but it's also possible to write to [Azure Blob Storage](../database/xevent-code-event-file.md). XEvent sessions storing data in the ring buffer have a limit of about 1,000 messages so should only be used to track recent activity. Additionally, ring buffer data is lost upon failover. As such, for a historical record of backups, write to an event file instead.
5164

52-
Configure a simple XEvent session to capture simple events about complete full backups. This script collects the name of the database, the total number of bytes processed, and the time the backup completed.
65+
### Basic tracking
5366

54-
Use Transact-SQL (T-SQL) to configure the simple XEvent session:
67+
Configure a basic XEvent session to capture events about complete full backups. This script collects the name of the database, the total number of bytes processed, and the time the backup completed.
5568

69+
Use [!INCLUDE [tsql-md](../../docs/includes/tsql-md.md)] (T-SQL) to configure the basic XEvent session:
5670

5771
```sql
58-
CREATE EVENT SESSION [Simple backup trace] ON SERVER
59-
ADD EVENT sqlserver.backup_restore_progress_trace(
60-
WHERE operation_type = 0
61-
AND trace_message LIKE '%100 percent%')
62-
ADD TARGET package0.ring_buffer
63-
WITH(STARTUP_STATE=ON)
72+
CREATE EVENT SESSION [Basic backup trace] ON SERVER
73+
ADD EVENT sqlserver.backup_restore_progress_trace
74+
(
75+
WHERE operation_type = 0
76+
AND trace_message LIKE '%100 percent%'
77+
)
78+
ADD TARGET package0.ring_buffer WITH (STARTUP_STATE = ON);
6479
GO
65-
ALTER EVENT SESSION [Simple backup trace] ON SERVER
80+
81+
ALTER EVENT SESSION [Basic backup trace] ON SERVER
6682
STATE = start;
6783
```
6884

69-
70-
7185
### Verbose tracking
7286

73-
Configure a verbose XEvent session to track greater details about your backup activity. This script captures start and finish of both full, differential and log backups. Since this script is more verbose, it fills up the ring buffer faster, so entries may recycle faster than with the simple script.
87+
Configure a verbose XEvent session to track greater details about your backup activity. This script captures start and finish of both full, differential and log backups. Since this script is more verbose, it fills up the ring buffer faster, so entries might recycle faster than with the basic script.
7488

75-
Use Transact-SQL (T-SQL) to configure the verbose XEvent session:
89+
Use T-SQL to configure the verbose XEvent session:
7690

7791
```sql
78-
CREATE EVENT SESSION [Verbose backup trace] ON SERVER
92+
CREATE EVENT SESSION [Verbose backup trace] ON SERVER
7993
ADD EVENT sqlserver.backup_restore_progress_trace(
8094
WHERE (
8195
[operation_type]=(0) AND (
82-
[trace_message] like '%100 percent%' OR
96+
[trace_message] like '%100 percent%' OR
8397
[trace_message] like '%BACKUP DATABASE%' OR [trace_message] like '%BACKUP LOG%'))
8498
)
8599
ADD TARGET package0.ring_buffer
@@ -89,75 +103,70 @@ WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
89103

90104
ALTER EVENT SESSION [Verbose backup trace] ON SERVER
91105
STATE = start;
92-
93106
```
94107

95-
## Monitor backup progress
108+
## Monitor backup progress
96109

97-
After the XEvent session is created, you can use Transact-SQL (T-SQL) to query ring buffer results and monitor the progress of the backup. Once the XEvent starts, it collects all backup events so entries are added to the session roughly every 5-10 minutes.
110+
After the XEvent session is created, you can use T-SQL to query ring buffer results and monitor the progress of the backup. Once the XEvent starts, it collects all backup events so entries are added to the session roughly every 5-10 minutes.
98111

99-
### Simple tracking
112+
### Basic tracking
100113

101-
The following Transact-SQL (T-SQL) code queries the simple XEvent session and returns the name of the database, the total number of bytes processed, and the time the backup completed:
114+
The following T-SQL code queries the basic XEvent session and returns the name of the database, the total number of bytes processed, and the time the backup completed:
102115

103-
```sql
116+
```sql
104117
WITH
105-
a AS (SELECT xed = CAST(xet.target_data AS xml)
106-
FROM sys.dm_xe_session_targets AS xet
107-
JOIN sys.dm_xe_sessions AS xe
108-
ON (xe.address = xet.event_session_address)
109-
WHERE xe.name = 'Backup trace'),
110-
b AS(SELECT
111-
d.n.value('(@timestamp)[1]', 'datetime2') AS [timestamp],
112-
ISNULL(db.name, d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)')) AS database_name,
113-
d.n.value('(data[@name="trace_message"]/value)[1]', 'varchar(4000)') AS trace_message
114-
FROM a
115-
CROSS APPLY xed.nodes('/RingBufferTarget/event') d(n)
116-
LEFT JOIN master.sys.databases db
117-
ON db.physical_database_name = d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)'))
118-
SELECT * FROM b
118+
a AS (SELECT CAST (xet.target_data AS XML) AS xed
119+
FROM sys.dm_xe_session_targets AS xet
120+
INNER JOIN sys.dm_xe_sessions AS xe
121+
ON (xe.address = xet.event_session_address)
122+
WHERE xe.name = 'Backup trace'),
123+
b AS (SELECT d.n.value('(@timestamp)[1]', 'datetime2') AS [timestamp],
124+
ISNULL(db.name, d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)')) AS database_name,
125+
d.n.value('(data[@name="trace_message"]/value)[1]', 'varchar(4000)') AS trace_message
126+
FROM a
127+
CROSS APPLY xed.nodes('/RingBufferTarget/event') AS d(n)
128+
LEFT OUTER JOIN master.sys.databases AS db
129+
ON db.physical_database_name = d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)'))
130+
SELECT * FROM b;
119131
```
120132

121-
The following screenshot shows an example of the output of the above query:
122-
123-
![Screenshot of the XEvent output](./media/backup-activity-monitor/present-xevents-output.png)
133+
The following screenshot shows an example of the output of the previous query:
124134

125-
In this example, five databases were automatically backed up over the course of 2 hours and 30 minutes, and there are 130 entries in the XEvent session.
135+
:::image type="content" source="media/backup-activity-monitor/present-xevents-output.png" alt-text="Screenshot of the XEvent output." lightbox="media/backup-activity-monitor/present-xevents-output.png":::
126136

127-
### Verbose tracking
137+
In this example, five databases were automatically backed up over the course of 2 hours and 30 minutes, and there are 130 entries in the XEvent session.
128138

129-
The following Transact-SQL (T-SQL) code queries the verbose XEvent session and returns the name of the database, as well as the start and finish of both full, differential and log backups.
139+
### Verbose tracking
130140

141+
The following T-SQL code queries the verbose XEvent session and returns the name of the database, as well as the start and finish of both full, differential and log backups.
131142

132143
```sql
133144
WITH
134-
a AS (SELECT xed = CAST(xet.target_data AS xml)
135-
FROM sys.dm_xe_session_targets AS xet
136-
JOIN sys.dm_xe_sessions AS xe
137-
ON (xe.address = xet.event_session_address)
138-
WHERE xe.name = 'Verbose backup trace'),
139-
b AS(SELECT
140-
d.n.value('(@timestamp)[1]', 'datetime2') AS [timestamp],
141-
ISNULL(db.name, d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)')) AS database_name,
142-
d.n.value('(data[@name="trace_message"]/value)[1]', 'varchar(4000)') AS trace_message
143-
FROM a
144-
CROSS APPLY xed.nodes('/RingBufferTarget/event') d(n)
145-
LEFT JOIN master.sys.databases db
146-
ON db.physical_database_name = d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)'))
147-
SELECT * FROM b
145+
a AS (SELECT CAST (xet.target_data AS XML) AS xed
146+
FROM sys.dm_xe_session_targets AS xet
147+
INNER JOIN sys.dm_xe_sessions AS xe
148+
ON (xe.address = xet.event_session_address)
149+
WHERE xe.name = 'Verbose backup trace'),
150+
b AS (SELECT d.n.value('(@timestamp)[1]', 'datetime2') AS [timestamp],
151+
ISNULL(db.name, d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)')) AS database_name,
152+
d.n.value('(data[@name="trace_message"]/value)[1]', 'varchar(4000)') AS trace_message
153+
FROM a
154+
CROSS APPLY xed.nodes('/RingBufferTarget/event') AS d(n)
155+
LEFT OUTER JOIN master.sys.databases AS db
156+
ON db.physical_database_name = d.n.value('(data[@name="database_name"]/value)[1]', 'varchar(200)'))
157+
SELECT * FROM b;
148158
```
149159

150160
The following screenshot shows an example of a full backup in the XEvent session:
151161

152-
:::image type="content" source="media/backup-activity-monitor/output-with-full.png" alt-text="XEvent output showing full backups":::
162+
:::image type="content" source="media/backup-activity-monitor/output-with-full.png" alt-text="Screenshot of XEvent output showing full backups." lightbox="media/backup-activity-monitor/output-with-full.png":::
153163

154164
The following screenshot shows an example of an output of a differential backup in the XEvent session:
155165

156-
:::image type="content" source="media/backup-activity-monitor/output-with-differential.png" alt-text="XEvent output showing differential backups":::
157-
158-
159-
## Next steps
166+
:::image type="content" source="media/backup-activity-monitor/output-with-differential.png" alt-text="Screenshot of XEvent output showing differential backups." lightbox="media/backup-activity-monitor/output-with-differential.png":::
160167

161-
Once your backup has completed, you can then [restore to a point in time](point-in-time-restore.md) or [configure a long-term retention policy](long-term-backup-retention-configure.md).
168+
## Related content
162169

163-
To learn more, see [automated backups](automated-backups-overview.md).
170+
- [Restore a database in Azure SQL Managed Instance to a previous point in time](point-in-time-restore.md)
171+
- [Manage Azure SQL Managed Instance long-term backup retention](long-term-backup-retention-configure.md)
172+
- [Automated backups in Azure SQL Managed Instance](automated-backups-overview.md)

0 commit comments

Comments
 (0)