我需要一個sql命令(dbcc或擴展存儲過程?)或可以從sql server存儲過程調用的某個東西來獲取最新的完整備份文件可用於特定數據庫。備份文件的名稱將放置在varchar變量中,我可以在存儲過程中使用RESTORE DATABASE命令。此過程將用於從生產數據庫恢復到沙箱/培訓數據庫,因此在恢復完成後,我需要該過程繼續運行,以便對db進行一些修改。Sql獲取SQL Server中特定數據庫的最新完整備份文件
11
A
回答
11
只是「源」刺服務器上查詢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
甜,我應該能夠從這個查詢中得到我需要的:'從msdb..backupset中選擇最上面的1個名字,其中名稱'TmsePrd_b%'和類型='D'按名稱排序desc' – 2011-05-23 21:42:48
+0
偉大的洞察力 - 謝謝! – 2011-05-24 19:31:17
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
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
0
只是想給一個除從user2378139的出色答卷。我經常需要從1個文件夾中恢復多個數據庫,並且該文件夾中的每個數據庫都會有多個備份副本。我需要獲取最新的信息,並在計劃的任務上運行它。以下是我的編輯/更新來實現這一點。有更多的臨時表比我想要的,但還沒有想出一個更好的方式來做到這一點:
IF OBJECT_ID('tempdb..#TemperedFileList') IS NOT NULL DROP TABLE #TemperedFileList
GO
Declare @FileName varChar(255)
Declare @cmdText varChar(255)
Declare @BKFolder varchar(255)
Declare @DBFolder varchar(255)
set @FileName = null
set @BKFolder = 'E:\SQLBackupFolder\'
set @DBFolder = 'C:\Program Files\Microsoft SQL Server\MSSQL13.COBRASERVER\MSSQL\DATA\'
declare @FileList table (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
create table #TemperedFileList (FileName varchar(255),DBName varchar(255))
insert into #TemperedFileList (FileName,DBName) select FileName, SUBSTRING(FileName,0,CHARINDEX('_',FileName)) from @FileList WHERE Filename like '%.bak'
declare @RowCnt int
declare @MaxRows int
declare @tmpFileName varchar(255)
declare @tmpDBName varchar(255)
declare @sql nvarchar(3000)
select @RowCnt = 1
declare @Import table (rownum int IDENTITY (1, 1) Primary key NOT NULL , FileName varchar(255),DBName varchar(255))
insert into @Import (FileName,DBName) SELECT FileName,DBName FROM (SELECT FileName,DBName,rank() over (partition by DBName order by FileName desc) r FROM #TemperedFileList) ilv where r=1
select @MaxRows=count(*) from @Import
while @RowCnt <= @MaxRows
begin
select @tmpFileName=FileName from @Import where rownum = @RowCnt
select @tmpDBName=DBName from @Import where rownum = @RowCnt
set @sql ='ALTER DATABASE ' + @tmpDBName + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'
print @sql
exec(@sql)
set @sql ='RESTORE DATABASE [' + @tmpDBName + '] FROM DISK=''' + @BKFolder + @tmpFileName + ''' with FILE=1, MOVE N''' + @tmpDBName + ''' TO N''' + @DBFolder + @tmpDBName + '.mdf'', MOVE N''' + @tmpDBName + '_Log'' TO N''' + @DBFolder + @tmpDBName + '_log.ldf'', NOUNLOAD, REPLACE, STATS = 10'
print @sql
exec(@sql)
set @sql ='ALTER DATABASE ' + @tmpDBName + ' SET MULTI_USER WITH ROLLBACK IMMEDIATE'
print @sql
exec(@sql)
Set @RowCnt = @RowCnt + 1
end
相關問題
- 1. 從MySQL完整的.sql備份還原特定數據庫
- 2. 備份SQL Server數據庫
- 3. SQL Server 2005的完整數據庫備份到磁盤
- 4. PHP下載完整的mysql數據庫爲.sql備份文件
- 5. SQL Server 2008中的數據庫備份
- 6. C#:備份SQL Server數據庫到一個新的.bak文件
- 7. 如何獲取SQL Server數據庫的完整元數據
- 8. 在ASP.NET中備份SQL Server數據庫
- 9. 製作SQL Server數據庫的備份
- 10. SQL備份文件,區分部分和完整備份文件
- 11. SQL Server數據庫備份文件(.bak)文件被破壞
- 12. 如何從文件夾獲取數據庫的最新備份?
- 13. SQL Server 2005數據庫備份
- 14. SQL Server數據庫備份錯誤
- 15. 20GB SQL Server數據庫備份resore
- 16. 自動備份SQL Server數據庫
- 17. SQL Server:備份所有數據庫
- 18. 遠程SQL Server Express備份數據庫
- 19. 備份遠程SQL Server數據庫
- 20. 從完整的mysqldump備份中提取特定數據
- 21. SQL Server備份文件
- 22. 如何備份數據庫備份每天在SQL Server 2005中
- 23. SQL Server管理對象從備份文件中讀取數據
- 24. SQL備份和恢復完整備份
- 25. SQL Server數據庫備份文件的MIME類型
- 26. Microsoft SQL Server 2005備份* .sql中的數據庫
- 27. 還原Sql Server 2005數據庫備份到Sql Server 2000
- 28. 如何用最新的備份更新我的本地SQL Server數據庫?
- 29. 備份爲bak文件或附加/分離SQL Server數據庫?
- 30. SQL Server數據庫備份:網絡服務文件訪問
在prod服務器上運行? – gbn 2011-05-23 19:07:22