Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql to get most recent full backup file for a specific database in sql server

I need an sql command (dbcc or extended stored procedure?) or something that can be called from a sql server stored procedure to get the most recent full backup file available for a specific database. The name of the backup file will be placed in a varchar variable that I can use with a RESTORE DATABASE command in the stored procedure. This procedure will be used to restore from production database to a sandbox/training database, so after the restore completes I need the procedure to continue running so I can make a few modifications to the db.

like image 292
Joel Coehoorn Avatar asked May 23 '11 18:05

Joel Coehoorn


3 Answers

Just query msdb..backupset (MSDN) on the "source" prod server

And a working example (SQL Rockstar) too

Edit, 2018

SELECT
    bs.database_name,
    bs.backup_start_date,
    bmf.physical_device_name
FROM
    msdb.dbo.backupmediafamily bmf
    JOIN
    msdb.dbo.backupset bs ON bs.media_set_id = bmf.media_set_id
WHERE
    bs.database_name = 'MyDB'
ORDER BY
    bmf.media_set_id DESC;
like image 106
gbn Avatar answered Oct 22 '22 14:10

gbn


I have a handy script that I use when I restore the most recent backup from a directory to the database you want to restore to. It is great for refreshing a dev or test box by using a nightly job!

/******************************************************
Script: looks at the backup directory and restores the
    most recent backup (bak) file 
    You will have to modify the code
    to match your database names and paths.
    DO NOT USE IN PRODUCTION.  It kicks all users off!

Created By:
    Michael F. Berry
Create Date:
    1/15/2014
******************************************************/


--get the last backup file name and path

Declare @FileName varChar(255)
Declare @cmdText varChar(255)
Declare @BKFolder varchar(255)

set @FileName = null
set @cmdText = null
set @BKFolder = '\\MyBackupStorageShare\server\FULL\'


create table #FileList (
FileName varchar(255),
DepthFlag int,
FileFlag int
)


--get all the files and folders in the backup folder and put them in temporary table
insert into #FileList exec xp_dirtree @BKFolder,0,1
--select * from #filelist

--get the latest backup file name
select top 1 @FileName = @BKFolder + FileName from #FileList where Filename like '%.bak' order by filename desc
select @filename


--kick off current users/processes
ALTER DATABASE DBName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;


--execute the restore
exec('
RESTORE DATABASE [DBNAME] FROM  DISK = ''' + @filename + '''
WITH  MOVE N''DBName_Data'' TO N''E:\SQLData\DBName.mdf'', MOVE N''DBName_Log'' TO N''E:\SQLLogs\DBName_log.ldf'',  NOUNLOAD,  REPLACE,  STATS = 10')


--Let people/processes back in!
ALTER DATABASE DBName
SET MULTI_USER WITH ROLLBACK IMMEDIATE;
go 
like image 12
user2378139 Avatar answered Oct 22 '22 16:10

user2378139


declare @backupfile as Varchar(255)

SELECT TOP 1 @backupfile=mf.physical_device_name from msdb..backupset bk      
join msdb..backupmediafamily mf on bk.media_set_id = mf.media_set_id   
where database_name=N'sourcedatabasename' and bk.type='D' order by  
backup_set_id desc

ALTER DATABASE [databasename] SET  SINGLE_USER  WITH ROLLBACK IMMEDIATE 

RESTORE DATABASE databasename
FROM DISK = @backupfile
WITH MOVE 'datafile' TO 'databasefilepath',
MOVE 'logfilename' TO 'logfilepath', REPLACE

ALTER DATABASE [databasename] SET  MULTI_USER  WITH ROLLBACK IMMEDIATE
like image 3
user5054734 Avatar answered Oct 22 '22 15:10

user5054734