When was the last time your SQL Server database was restored

本文提供了一段T-SQL脚本,用于查询指定数据库或所有数据库在过去一定天数内的恢复记录详情,包括恢复时间、操作者、恢复类型等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When was the last time your SQL Server database was restored

Written By: Thomas LaRock -- 4/7/2009 -- 0 comments

     Stay informed - get the MSSQLTips.com newsletter and win - click here    

Problem
Often times we are asked the question "when was the last time my database was restored, and where was it restored from?"  In this tip, we will look at some of the system tables that capture restore history information and how you can query these system tables to answer this question.

Solution
The restore history information is readily available inside the msdb, making the solution as easy as a few lines of T-SQL.


Returning the details

Here is some T-SQL that will return information about the last time a database has been restored. There are two variables, @dbname and @days, that you can configure. The first (@dbname) would be the name of the database you are searching for and would need to be enclosed in single quotation marks. If you leave it NULL than all databases will be returned. The second variable (@days) would be a negative integer (i.e., -7) which represents how many days previously you want to search. So, -7 would translate to returning the previous week's worth of history. If you leave it NULL then the script will default to searching for only the previous thirty days.

DECLARE @dbname sysname, @days int
SET @dbname = NULL --substitute for whatever database name you want
SET @days = -30 --previous number of days, script will default to 30
SELECT
 rsh.destination_database_name AS [Database],
 rsh.user_name AS [Restored By],
 CASE WHEN rsh.restore_type = 'D' THEN 'Database'
  WHEN rsh.restore_type = 'F' THEN 'File'
  WHEN rsh.restore_type = 'G' THEN 'Filegroup'
  WHEN rsh.restore_type = 'I' THEN 'Differential'
  WHEN rsh.restore_type = 'L' THEN 'Log'
  WHEN rsh.restore_type = 'V' THEN 'Verifyonly'
  WHEN rsh.restore_type = 'R' THEN 'Revert'
  ELSE rsh.restore_type 
 END AS [Restore Type],
 rsh.restore_date AS [Restore Started],
 bmf.physical_device_name AS [Restored From], 
 rf.destination_phys_name AS [Restored To]
FROM msdb.dbo.restorehistory rsh
 INNER JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id
 INNER JOIN msdb.dbo.restorefile rf ON rsh.restore_history_id = rf.restore_history_id
 INNER JOIN msdb.dbo.backupmediafamily bmf ON bmf.media_set_id = bs.media_set_id
WHERE rsh.restore_date >= DATEADD(dd, ISNULL(@days, -30), GETDATE()) --want to search for previous days
AND destination_database_name = ISNULL(@dbname, destination_database_name) --if no dbname, then return all
ORDER BY rsh.restore_history_id DESC
GO

The script will return the following result set:

Here is the definition of each of the result set columns.

Column Name

Description

DatabaseThe name of the target database.
Restored ByThe name of the user that performed the restore.
Restore TypeThe type of restore performed. The possible types include the following:
  • D - Database
  • F - File
  • G - Filegroup
  • I - Differential
  • L - Log
  • V - Verifyonly
  • R - Revert
Restore StartedThe time at which the restore command was started.
Restored FromThe file(s) that the restore used in the RESTORE command.
Restored ToThe database data files restored (or created) as a result of the RESTORE command.

Next Steps

### 使用 Navicat Premium 还原 SQL Server 数据库的操作指南 Navicat Premium 是一款强大的数据库管理工具,支持多种主流数据库的连接和操作。对于 SQL Server 数据库的还原操作,可以通过以下方法实现。 #### 方法一:通过导入 SQL 文件还原 如果已经有一个包含数据库结构和数据的 `.sql` 文件,则可以按照以下步骤将其导入到目标 SQL Server 中: 1. 打开 Navicat Premium 并连接至目标 SQL Server 实例[^3]。 2. 在左侧导航栏中右键点击目标数据库名称(如果没有现有数据库,可以选择创建一个新的数据库),然后选择 **查询** 或者直接打开一个新查询窗口。 3. 点击顶部菜单中的 **文件 -> 导入文件...** ,或者使用快捷方式 Ctrl+I 来启动文件导入向导。 4. 浏览并选择之前准备好的 `.sql` 文件,确认无误后单击 **确定** 开始执行脚本。 5. 如果一切正常,SQL 脚本将会被执行,完成数据库的恢复过程[^4]。 #### 方法二:利用备份文件 (.bak) 恢复 当拥有来自 SQL Server 的完整备份文件时,可通过 Navicat Premium 提供的功能来完成这一任务: 1. 启动 Navicat Premium 应用程序并与相应的 SQL Server 建立链接。 2. 在对象浏览器里找到要恢复的目标位置——这可能是现有的空白数据库或者是计划新建的位置。 3. 右键该节点并选取选项列表里的 “**还原数据库**” 动作项。 4. 接下来会弹出对话框提示指定 .bak 备份档案路径;定位到实际存储处加载进来即可继续下一步骤配置设定比如覆盖模式等参数调整完毕之后按下 OK 键正式实施整个回复流程直至结束为止[^1]。 以上两种主要途径均能有效达成基于 Navicat Premium 对于 SQL Server 类型的数据集合体实行回溯重建的目的。 ```python # 示例 Python 代码片段用于演示如何调用外部命令行工具辅助处理.sql 文件的情况 import subprocess def restore_sql_database(sql_file_path, server_name, database_name): command = f'sqlcmd -S {server_name} -d master -Q "RESTORE DATABASE [{database_name}] FROM DISK=\'{sql_file_path}\' WITH REPLACE"' result = subprocess.run(command, shell=True, capture_output=True, text=True) if result.returncode != 0: raise Exception(f"Failed to restore database: {result.stderr}") print("Database restored successfully.") restore_sql_database('path/to/your/file.sql', 'localhost\\SQLEXPRESS', 'TestDB') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值