sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Estimate backup size for each database in a SQL Server instance (sp_EstimateAllDBBackupSize)

Antonios Chatzipavlis
Saturday 19 September 2015

Ένας DBA πρέπει να είναι σε θέση να γνωρίζει και να προβλέπει πολλά πράγματα κάθε μέρα.
Ένα από αυτά είναι να γνωρίζει το μέγεθος που χρειάζεται το backup των databases που έχει στο SQL Server instance του, ιδιαίτερα όταν αυτό είναι disk backup.

Γνωρίζοντας αυτό κάνει την ζωή του ευκολότερη καθώς ξέρει αν και που έχει τους χώρους που απαιτούνται.

Μεγαλύτερος «βραχνάς» είναι το full backup που όπως είναι φυσικό χρειάζεται και το περισσότερο χώρο.

Σαν DBA που καθημερινά διαχειρίζομαι πάνω από 270 instances και 3500 databases η ανάγκη μου να γνωρίζω και αυτό είναι σημαντική.

Για το λόγο αυτό είχα φτιάξει μια stored procedure με την οποία έκανα την δουλειά μου.

Σήμερα την χρειάστηκα ξανά και τις έκανα μερικές αλλαγές και αποφάσισα να την δημοσιεύσω καθώς έχω διαπιστώσει ότι σας αρέσουν αυτά τα μικρά καλούδια.

Ο κώδικας της stored procedure είναι ο παρακάτω και το μόνο που χρειάζεται να κάνετε είναι copy και paste σε ένα query window στο SSMS και να το εκτελέσετε.

Δημιουργεί την stored procedure στην master database για την εκτέλεση της απλά γράφεται το όνομα της σε ένα query window
use master
go

if not exists (select * from information_schema.routines where routine_name = 'sp_EstimateAllDBBackupSize')
    exec ('create proc dbo.sp_EstimateAllDBBackupSize as select ''stub version, to be replaced''')
go



alter proc sp_EstimateAllDBBackupSize
as
/*********************************************************************************************
sp_EstimateAllDBBackupSize (c) 2015 SQLschool.gr - Antonios Chatzipavlis

Version History
1.00    Sep 19, 2015

Feedback: mailto:info@sqlschool.gr

License: 
    sp_EstimateAllDBBackupSize is free to download and use for personal, educational, 
    and internal corporate purposes, provided that this header is preserved. 
*********************************************************************************************/
begin
    set nocount on;

    declare @dbname sysname;
    declare @query nvarchar(1000);
    declare @stm nvarchar(1000);

    set @query = 'use [?]
    insert into #backupsize
    select      db_name() 
            , convert(decimal(15,2), sum(size) * 8192 / 1048576) 
            , convert(decimal(15,2), sum(fileproperty(name,''spaceused'')) * 8192 / 1048576) 
    from sys.database_files;'

    if ( object_id('tempdb..#backupsize') is not null )
    begin
        drop table #backupsize;
    end    

    create table  #backupsize
    (
        database_name sysname
    ,    total_db_size_mb decimal(15, 2)
    ,    estimated_backup_size_mb decimal(15, 2)
    );
    
    declare dblist cursor local fast_forward for 
            select name from sys.databases order by name;

    open dblist;

    fetch next from dblist into @dbname;
    while (@@fetch_status <> -1)
    begin
        if (@@fetch_status <> -2)
        begin
            set @stm =replace(@query,'?',@dbname);
            execute(@stm);
        end
        fetch next from dblist into @dbname;
    end

    close dblist;
    deallocate dblist;

    select * from #backupsize;
end
go
Enjoy
/*antonch*/

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.