Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Auto Restore Test Database from most recent Production Backup

$
0
0

I was looking at using an 'Auto Restore script', that would allow me to:

->Run the Restore from the Test Server nightly via a SQL job

->Restore this latest Production backup by querying the Production Server via a linked-server from Test Server

I did see the following script from G  and it builds a restore statement at the end.  I was looking for ideas or any changes needed to accomplish the 2 main objectives I have at the top, and add a step to actually execute the created statement.  Note I would need to call the remote backup in this script but not sure the best way (i.e. \\serverprd\h$\backups\ 

Here's the script:

DECLARE @databaseName sysname
DECLARE @backupStartDate datetime
DECLARE @backup_set_id_start INT
DECLARE 
@backup_set_id_end INT

-- set database to be used
SET @databaseName 'enterDatabaseNameHere' 

SELECT @backup_set_id_start = MAX(backup_set_id
FROM  msdb.dbo.backupset 
WHERE database_name @databaseName AND type 'D'

SELECT @backup_set_id_end = MIN(backup_set_id
FROM  msdb.dbo.backupset 
WHERE database_name @databaseName AND type 'D'
AND backup_set_id @backup_set_id_start

IF @backup_set_id_end IS NULL SET @backup_set_id_end 999999999

SELECT backup_set_id'RESTORE DATABASE ' @databaseName ' FROM DISK = ''' 
               
mf.physical_device_name ''' WITH NORECOVERY'
FROM    msdb.dbo.backupset b,
           
msdb.dbo.backupmediafamily mf
WHERE    b.media_set_id mf.media_set_id
           
AND b.database_name @databaseName
          
AND b.backup_set_id @backup_set_id_start
UNION
SELECT 
backup_set_id'RESTORE LOG ' @databaseName ' FROM DISK = ''' 
               
mf.physical_device_name ''' WITH NORECOVERY'
FROM    msdb.dbo.backupset b,
           
msdb.dbo.backupmediafamily mf
WHERE    b.media_set_id mf.media_set_id
           
AND b.database_name @databaseName
          
AND b.backup_set_id >= @backup_set_id_start AND b.backup_set_id @backup_set_id_end
          
AND b.type 'L'
UNION
SELECT 
999999999 AS backup_set_id'RESTORE DATABASE ' @databaseName ' WITH RECOVERY'
ORDER BY backup_set_id

Appreciate replies.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>