When was the last time your SQL Server database was restored

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

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

### 无法写入数据的恢复方案 在处理无法写入数据的问题时,通常需要检查几个关键点以确保恢复过程顺利进行。以下是一些可能的原因及解决方案: 1. **检查磁盘空间** 如果目标路径上的磁盘空间不足,可能导致无法创建数据文件。例如,在引用中提到的数据文件 `/u01/system01.dbf` 无法创建,可能是由于磁盘空间不足导致[^1]。可以通过以下命令检查磁盘空间: ```bash df -h /u01 ``` 如果磁盘空间不足,请清理不必要的文件或扩展磁盘。 2. **验证文件系统权限** 确保 RMAN 用于恢复的用户具有对目标路径的写权限。如果权限不足,可能会导致 `ORA-01180` 错误[^1]。可以使用以下命令验证和修改权限: ```bash ls -ld /u01/ chmod 755 /u01/ chown oracle:dba /u01/ ``` 3. **确认备份集的完整性** 在执行恢复操作之前,确保备份集完整且可用。例如,引用中提到存档日志已存在于磁盘上[^5],但若备份损坏或丢失,恢复将失败。可以运行以下命令验证备份集: ```bash RMAN> validate backup of database; ``` 4. **调整 RMAN 参数** 如果恢复过程中出现错误,可以尝试调整 RMAN 参数以适应特定环境。例如,设置 `DB_FILE_NAME_CONVERT` 参数以指定正确的路径[^2]。示例配置如下: ```sql ALTER SYSTEM SET DB_FILE_NAME_CONVERT='/old_path/', '/new_path/' SCOPE=SPFILE; ``` 5. **启用自动备份功能** 默认情况下,数据库管理选项中的自动备份功能是启用的[^3]。确保此功能正常工作,以便在发生问题时能够快速恢复。可以通过以下命令检查当前设置: ```sql SHOW PARAMETER db_recovery_file_dest; ``` 6. **恢复步骤** 如果上述检查均无问题但仍无法写入数据,可以尝试以下恢复流程: - 使用 `RMAN` 进行完全恢复: ```bash RMAN> RESTORE DATABASE; RMAN> RECOVER DATABASE; ``` - 恢复完成后,启动数据库: ```sql SQL> ALTER DATABASE OPEN RESETLOGS; ``` 7. **编译内核相关问题(如果适用)** 如果在编译内核时遇到类似 `ld: block: No such file` 的错误[^4],请确保所有依赖项已正确安装,并检查工具链是否兼容。可以尝试重新安装工具链或修复链接器问题。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值