go backarticles

Articles of SQLschool.gr Team

Databases Restore History Report

Antonios Chatzipavlis

Ένας DBA καθημερινά έχει πολλά πράγματα να κάνει και πολλές ερωτήσεις να απαντήσει. Πολλές φορές αυτό που θέλει να κάνει ή να απαντήσει δεν γίνεται με το πάτημα ενός κουμπιού.
Μια ερώτηση που μου τέθηκε χθες ήταν να δω το πότε έχει γίνει restore μια database σε ένα instance.

Η απάντηση εύκολα βγαίνει από το restorehistory table που υπάρχει στην msdb database αλλά πάντα θέλεις να έχεις περισσότερες πληροφορίες που αυτός δεν περιέχει. Έτσι έφτιαξα το παρακάτω query μπορεί να απαντήσει στις ερωτήσεις τους κάθε ένα σχετικά με το πότε έχουν γίνει restores και σε ποιες databases στο συγκεκριμένο instance.

select 
        h.destination_database_name as destination_database_name
,       h.restore_date as date_of_restore
,        b.database_name as source_database_name
,        b.server_name as source_server_name
,        case h.restore_type
            when 'D' then 'Database'
            when 'F' then 'File'
            when 'G' then 'Filegroup'
            when 'I' then 'Differential'
            when 'L' then 'Log'
            when 'V' then 'Verifyonly'
            when 'R' then 'Revert'
            else  h.restore_type
        end as restore_type
,       h.user_name as restored_by_user          
,       h.device_count 
,        b.name as backup_name    
,       b.description as backup_description
,        cast ((select    
                      m.family_sequence_number as device_seqnum
                    ,m.physical_device_name as device_name
                    ,case device_type
                        when 2 then 'DISK'
                        when 5 then 'TAPE'
                        when 7 then 'VIRTUAL'
                        when 105 then 'PERMANENT'
                        else cast (device_type as nvarchar)
                    end as device_type
                from msdb.dbo.backupmediafamily as m
                where m.media_set_id = b.backup_set_id
                for xml raw ('restoredevice'),root('restoredevices')) 
                as xml) as restored_device_info
,        cast ((select 
                     file_number
                    ,destination_phys_name
                    ,filegroup_name 
                from msdb.dbo.restorefile as rf
                    inner join msdb.dbo.restorefilegroup as rg on rf.restore_history_id = rg.restore_history_id
                where rf.restore_history_id=h.restore_history_id
                for xml raw ('restoredfile'),root('restorefiles')) 
                as xml) as restored_files
from msdb.dbo.restorehistory as h
    inner join msdb.dbo.backupset as b on h.backup_set_id = b.backup_set_id
order by restore_date desc

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.


Relative Articles

Leave your comment

COMMENT

FULL NAME

EMAIL ADDRESS

We use Gravatar

WEB SITE



captcha


 

Newsletter

If you want to receive updates from us subscribe below with your email.
Follow us in
PASS chapter logo

The Official PASS Local Group for Greece

About us Contact us Terms of Use Privacy Sing in Register
sql school greece logo
© 2010-2020 All rights reserved

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