Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Latest Backup file restore

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2014-02-26 : 12:19:32
Hi All,

I Have a problem while creating a automated restore job ..pls find the info below

There are two backup files in a folder"G:\Backup\DataTraceBilling\"

TestDB_Backup_2011_02_08_230002_7317713.bak
TestDB_Backup_2011_02_05_230002_7318761.bak

I need to restore the latest backup to my local machine ones in a every week and will be scheduled. The back up file contains timestamp how do we restore this.

Below is the query for my try but not working correctly


DECLARE @DATE char(8),@Filename varchar(100), @BkpPath varchar(100) ,@DBName varchar(55)
SELECT @DBName = 'DataTraceBilling'
,@Date = Convert(VARCHAR(8),Getdate(),112)
SELECT @FileName = @DBName + '_' + 'Backup' + '_' + LEFT(@Date,4)+ '_' + SUBSTRING(@Date,5,2) + '_'+ RIGHT(@Date,2) + '.BAK'
SELECT @BkpPath = 'G:\Backup\DataTraceBilling\'
SELECT @FileName = @BkpPath + @FileName

DECLARE @SQL varchar(max)

SELECT @SQL =
'RESTORE DATABASE ' + @DBName + ' FROM
DISK ='+ @FileName + ''

--EXEC (@SQL)
PRINT @SQL






Thanks,
Gangadhara MS
SQL Developer and DBA

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2014-02-27 : 14:21:30
There are several approaches you can use - here are a couple:

1) Query the source systems backup history to get the latest backup for that database.
2) Use powershell script to query the directory for the latest available file - then use SQLCMD to restore.

Either way, you need to identify the file to be used - and then include the options REPLACE and MOVE to restore the database.
Go to Top of Page

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2014-02-27 : 23:22:14
Hi Could you please post me any reference link where i can find query to source system backup history to get the latest backup.



Thanks,
Gangadhara MS
SQL Developer and DBA
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2014-03-06 : 13:15:18
The data will be in the msdb database in the tables: backupset, backupmediafamily. Here is a start:

Select b.database_name
, b.backup_finish_date
, mf.physical_device_name
From msdb.dbo.backupset b
Inner Join msdb.dbo.backupmediafamily mf On mf.media_set_id = b.media_set_id
Where b.[type] = 'D'
Go to Top of Page
   

- Advertisement -