Cea mai recenta restaurare pentru o anumita baza

Configurare noua (How To)

Situatie

Pentru cazurile destul de dese in care avem nevoie sa stim care este cel mai recent restore pentru o anumita baza de date am facut o procedura ce primeste ca si parametru numele bazei de date si pt acea baza returneaza ULTIMA restaurare care a avut loc pe acea baza.

Ca si informatii suplimentare procedura returneaza:

– userul ce a facut restaurarea
– tipul restaurarii: Full, Differential, Log , etc
– data la care s-a facut restaurarea
– calea din care s-a ales backupul pentru restaurare precum si numele backupului folosit
– calea catre care s-au restaurat fisierele bazei de date

Solutie

create procedure BD_CelMaiRecentRestore_prc
@database varchar(max)

as

declare @max datetime
select @max = max(restore_date) from msdb..restorehistory where destination_database_name = @database

SELECT
rsh.destination_database_name AS [Database],
rsh.user_name AS [Restored By],
CASE WHEN rsh.restore_type = ‘D’ THEN ‘Full’
WHEN rsh.restore_type = ‘F’ THEN ‘File’
WHEN rsh.restore_type = ‘G’ THEN ‘Filegroup’
WHEN rsh.restore_type = ‘I’ THEN ‘Differential’
WHEN rsh.restore_type = ‘L’ THEN ‘Log’
WHEN rsh.restore_type = ‘V’ THEN ‘Verifyonly’
WHEN rsh.restore_type = ‘R’ THEN ‘Revert’
ELSE rsh.restore_type
END AS [Restore Type],
rsh.restore_date AS [Restore Started],
bmf.physical_device_name AS [Restored From],
rf.destination_phys_name AS [Restored To]

FROM msdb.dbo.restorehistory rsh
INNER JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id
INNER JOIN msdb.dbo.restorefile rf ON rsh.restore_history_id = rf.restore_history_id
INNER JOIN msdb.dbo.backupmediafamily bmf ON bmf.media_set_id = bs.media_set_id

WHERE
destination_database_name = ISNULL(@database, destination_database_name) AND
rsh.restore_date = @max

ORDER BY rsh.restore_history_id DESC

Tip solutie

Permanent
Etichetare:

Voteaza

(22 din 46 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?