Track database growth by month from backup history in SQL Server
Backup history is a free size time series you already have. This turns it into a growth chart.
The task
SQL Server keeps no history of database size, but msdb.dbo.backupset keeps one row per backup, and full backups record backup_size and compressed_backup_size. Grouped by month, that is a growth timeline going back as far as msdb keeps backup history, with no extra monitoring. It only covers backups taken on this instance; if backups run on a secondary replica or through a tool that bypasses msdb, look there instead.
The snippet
Written for SQL Server.
SELECT database_name,
YEAR(backup_start_date) AS yr,
MONTH(backup_start_date) AS mo,
CAST(MAX(backup_size) / 1048576.0 AS DECIMAL(12,1)) AS max_backup_mb,
CAST(MAX(ISNULL(compressed_backup_size, backup_size)) / 1048576.0 AS DECIMAL(12,1)) AS max_compressed_mb
FROM msdb.dbo.backupset
WHERE type = 'D'
AND is_copy_only = 0
AND backup_start_date >= DATEADD(YEAR, -2, GETDATE())
GROUP BY database_name, YEAR(backup_start_date), MONTH(backup_start_date)
ORDER BY database_name, yr, mo;
Fonctionnement
type = 'D' keeps full database backups; log and differential backups measure change, not size. backupset stores database_name rather than a database id, and backup_start_date is in the server's local time, hence GETDATE. Taking MAX per month gives one figure per period however many backups ran. backup_size counts the bytes of used pages written to the backup, so it tracks the data you hold, not the size of the data files; compressed_backup_size is what the backup takes on disk and equals backup_size when compression is off. Divide the free space on the volume by the monthly growth to get the number of months left.
Variations
Simple total-growth-per-month trend across all databases
SELECT YEAR(backup_start_date) AS yr, MONTH(backup_start_date) AS mo,
CAST(SUM(backup_size) / 1073741824.0 AS DECIMAL(12,2)) AS total_gb
FROM msdb.dbo.backupset
WHERE type = 'D'
GROUP BY YEAR(backup_start_date), MONTH(backup_start_date)
ORDER BY yr, mo;
Questions that come up
How accurate is backup size as a size proxy?
Close enough for trends. A full backup contains the used pages plus the log needed to make it consistent, so it tracks data volume, while data files can be much larger if they were pre-sized or have free space after deletes. Compare months with each other, not with a file-size report, and use backup_size rather than the compressed figure, which moves whenever the compression setting or the data's compressibility changes.
How do I keep backup history from being purged out from under this?
Backup history is removed by msdb.dbo.sp_delete_backuphistory, usually called from a History Cleanup task in a maintenance plan or from an Agent job, with a cutoff date. If that keeps only a few weeks, the trend disappears. Copy the monthly result into a table of your own on a schedule, or lengthen the cutoff.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.