Sql获取sql server中特定数据库的最新完整备份文件

时间:2011-05-23 18:31:14

标签: sql sql-server backup

我需要一个sql命令(dbcc或扩展存储过程?)或者可以从sql server存储过程调用的东西,以获取特定数据库可用的最新完整备份文件。备份文件的名称将放在varchar变量中,我可以在存储过程中使用RESTORE DATABASE命令。此过程将用于从生产数据库还原到沙箱/培训数据库,因此在还原完成后,我需要继续运行该过程,以便对数据库进行一些修改。

4 个答案:

答案 0 :(得分:16)

只需查询“source”prod服务器上的msdb..backupset (MSDN)

还有一个working example (SQL Rockstar)

编辑,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;

答案 1 :(得分:10)

当我将最新的备份从目录还原到要还原到的数据库时,我使用了一个方便的脚本。使用夜间作业刷新开发或测试盒非常棒!

/******************************************************
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 

答案 2 :(得分:1)

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

答案 3 :(得分:0)

只是想补充一下user2378139的优秀答案。我经常需要从1个文件夹中恢复多个数据库,并且该文件夹中的每个数据库都有多个备份副本。我需要获得最新的,并在计划的任务上运行它。以下是我的编辑/更新,以实现这一目标。临时表比我想要的多,但还没有想出更好的方法:

 import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
  Alert
} from 'react-native';