SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Latest Backup file restore
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gangadhara.ms
Aged Yak Warrior

India
547 Posts

Posted - 02/26/2014 :  12:19:32  Show Profile  Reply with Quote
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

Edited by - gangadhara.ms on 02/27/2014 03:03:15

jeffw8713
Aged Yak Warrior

USA
806 Posts

Posted - 02/27/2014 :  14:21:30  Show Profile  Reply with Quote
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

India
547 Posts

Posted - 02/27/2014 :  23:22:14  Show Profile  Reply with Quote
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

USA
806 Posts

Posted - 03/06/2014 :  13:15:18  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.27 seconds. Powered By: Snitz Forums 2000