SQL Server ->> 查看SQL Server备份历史

找到最近数据库备份的细节信息,包括备份设备名和地址,最后备份时间

-------------------------------------------------------------------------------------------
--Most Recent Database Backup for Each Database - Detailed
-------------------------------------------------------------------------------------------
SELECT
   A.[Server],
   A.database_name,
   A.last_db_backup_date,
   B.backup_start_date,
   B.expiration_date,
   B.backup_size,
   B.logical_device_name,
   B.physical_device_name,
   B.backupset_name,
   B.description
FROM
   (
   SELECT
       CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
       msdb.dbo.backupset.database_name,
       MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
   FROM    msdb.dbo.backupmediafamily
       INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
   WHERE   msdb..backupset.type = ‘D‘
   GROUP BY
       msdb.dbo.backupset.database_name
   ) AS A 

   LEFT JOIN  

   (
   SELECT
   CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
   msdb.dbo.backupset.database_name,
   msdb.dbo.backupset.backup_start_date,
   msdb.dbo.backupset.backup_finish_date,
   msdb.dbo.backupset.expiration_date,
   msdb.dbo.backupset.backup_size,
   msdb.dbo.backupmediafamily.logical_device_name,
   msdb.dbo.backupmediafamily.physical_device_name,
   msdb.dbo.backupset.name AS backupset_name,
   msdb.dbo.backupset.description
FROM   msdb.dbo.backupmediafamily
   INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE  msdb..backupset.type = ‘D‘
   ) AS B
   ON A.[server] = B.[server] AND A.[database_name] = B.[database_name] AND A.[last_db_backup_date] = B.[backup_finish_date]
ORDER BY
   A.database_name 

--Most Recent Database Backup for Each Database
SELECT CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
       database_name,
       backup_start_date,
       backup_finish_date,
       Backup_duration_in_min,
       CAST(Backup_duration_in_min/60 AS VARCHAR(10))+‘ hour(s) and ‘+CAST(Backup_duration_in_min%60 AS VARCHAR(10))+‘ minute(s)‘ AS [description]
FROM (
SELECT ROW_NUMBER() OVER(PARTITION BY database_name ORDER BY backup_finish_date) AS row_no,
   database_name,
   backup_start_date,
   backup_finish_date,
   DATEDIFF(MINUTE,backup_start_date,backup_finish_date) AS Backup_duration_in_min
FROM msdb.dbo.backupset
WHERE type = ‘D‘ ) t
WHERE row_no = 1
ORDER BY
   database_name

查看上周数据库备份情况

---------------------------------------------------------------------------------
--Database Backups for all databases For Previous Week
---------------------------------------------------------------------------------
SELECT
CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_start_date,
msdb.dbo.backupset.backup_finish_date,
msdb.dbo.backupset.expiration_date,
CASE msdb..backupset.type
WHEN ‘D‘ THEN ‘Database‘
WHEN ‘L‘ THEN ‘Log‘
END AS backup_type,
msdb.dbo.backupset.backup_size,
msdb.dbo.backupmediafamily.logical_device_name,
msdb.dbo.backupmediafamily.physical_device_name,
msdb.dbo.backupset.name AS backupset_name,
msdb.dbo.backupset.description
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7)
ORDER BY
msdb.dbo.backupset.database_name,
msdb.dbo.backupset.backup_finish_date 

过去24小时内没有备份的数据库

-------------------------------------------------------------------------------------------
--Databases Missing a Data (aka Full) Back-Up Within Past 24 Hours
-------------------------------------------------------------------------------------------
--Databases with data backup over 24 hours old
SELECT
   CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
   msdb.dbo.backupset.database_name,
   MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date,
   DATEDIFF(hh, MAX(msdb.dbo.backupset.backup_finish_date), GETDATE()) AS [Backup Age (Hours)]
FROM    msdb.dbo.backupset
WHERE     msdb.dbo.backupset.type = ‘D‘
GROUP BY msdb.dbo.backupset.database_name 

HAVING      (MAX(msdb.dbo.backupset.backup_finish_date) < DATEADD(hh, - 24, GETDATE()))  

每个数据库最后备份的时间和信息

-------------------------------------------------------------------------------------------
--Most Recent Database Backup for Each Database - Detailed
-------------------------------------------------------------------------------------------
SELECT
   A.[Server],
   A.database_name,
   A.last_db_backup_date,
   B.backup_start_date,
   B.expiration_date,
   B.backup_size,
   B.logical_device_name,
   B.physical_device_name,
   B.backupset_name,
   B.description
FROM
   (
   SELECT
       CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
       msdb.dbo.backupset.database_name,
       MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
   FROM    msdb.dbo.backupmediafamily
       INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
   WHERE   msdb..backupset.type = ‘D‘
   GROUP BY
       msdb.dbo.backupset.database_name
   ) AS A 

   LEFT JOIN  

   (
   SELECT
   CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
   msdb.dbo.backupset.database_name,
   msdb.dbo.backupset.backup_start_date,
   msdb.dbo.backupset.backup_finish_date,
   msdb.dbo.backupset.expiration_date,
   msdb.dbo.backupset.backup_size,
   msdb.dbo.backupmediafamily.logical_device_name,
   msdb.dbo.backupmediafamily.physical_device_name,
   msdb.dbo.backupset.name AS backupset_name,
   msdb.dbo.backupset.description
FROM   msdb.dbo.backupmediafamily
   INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE  msdb..backupset.type = ‘D‘
   ) AS B
   ON A.[server] = B.[server] AND A.[database_name] = B.[database_name] AND A.[last_db_backup_date] = B.[backup_finish_date]
ORDER BY
   A.database_name 

从来没有备份过的数据库

--Databases without any backup history
SELECT
   CONVERT(CHAR(100), SERVERPROPERTY(‘Servername‘)) AS Server,
   master.dbo.sysdatabases.NAME AS database_name,
   NULL AS [Last Data Backup Date],
   9999 AS [Backup Age (Hours)]
FROM
   master.dbo.sysdatabases LEFT JOIN msdb.dbo.backupset
       ON master.dbo.sysdatabases.name  = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.database_name IS NULL AND master.dbo.sysdatabases.name <> ‘tempdb‘
ORDER BY  

   msdb.dbo.backupset.database_name 
时间: 2024-10-20 17:14:24

SQL Server ->> 查看SQL Server备份历史的相关文章

SQL SERVER 查看SQL语句IO,时间,索引消耗

1.查看SQL语句IO消耗 set statistics io on     select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set statistics io off 2.查看SQL语句时间消耗 set statistics time on      select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set st

数据库大师成长日记:巧用SQL语句查看SQL Server的结构信息

常用SQLServer的朋友们,绝大部分情况下,我们写SQL脚本主要用来处理我们自己的数据,比如读取.插入数据等.但在有些情况下,我们可能要查看我们定义的一些元素,比如我们定义的表都有哪些.表结构如何.存储过程的源码等等.这时候,我们学习的常规的SQL脚本可能就无法处理了. 比如您要写一个管理数据库的程序,就要知道数据库中有什么表.表结构如何,以及视图.存储过程的脚本源码等.下面我就说说如何获取数据库的结构信息. 获取数据库及配置信息 1.获取数据库配置信息 exec sp_server_inf

设定MS SQL Server 2008定期自动备份

1.说明 SQL Server2008 本身具有定期自动备份功能,我们只需要通过简单的配置就可以实现非常简单高效的自动备份功能. 2.打开SQL Server代理服务 要实现自动备份功能,首先要保证SQL Server的"SQL Server(代理)"服务已经打开. 如果没有看到这个"SQL Server代理"节点,可通过如下方式打开 先找到开始菜单中的"Microsoft SQL Server 2008 R2",再在"配置工具&quo

SQL Server 数据库定时自动备份(转载)

SQL Server 数据库定时自动备份 本文来源于转载:http://www.cnblogs.com/zhangq723/archive/2012/03/13/2394102.html 前提:在使用下面的备份方式之前需要确保你的Sqlserver Agent服务启动,切设置为自动启动.否则当你服务器重启了但是Agent服务没有启动,那么自动备份任务就不会执行 (一)使用TSql代码自动备份 在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本

SQL点滴12—SQL Server备份还原数据库中的小把戏

原文:SQL点滴12-SQL Server备份还原数据库中的小把戏 备份数据库时出现一个不太了解的错误 ,错误信息“is formatted to support  1 media families, but 2 media families are expected according to the backup device specification”.如下图: 解决办法是首先选中击上一次备份的文件,点击Remove,去掉当前的备份文件,然后再点击OK备份.这样才能正确的备份.也在微软帮助

SQL Server 维护计划实现数据库备份(策略实战)

一.背景 之前写过一篇关于备份的文章:SQL Server 维护计划实现数据库备份,上面文章使用完整备份和差异备份基本上能解决数据库备份的问题,但是为了保障数据更加安全,我们需要再次完善我们的备份计划: 下面这篇文章主要加入了日志备份,并对设计备份的频率和设计命名规范等问题进行实战: 二.最佳实践 (一) 备份计划 1) 每周星期日的2:00:00执行数据库的完整备份: 2) 每周星期一至星期六每天的2:00:00执行数据库的差异备份: 3) 每天在8:00:00和23:59:59之间.每1小时

SQL Server查看表结构及视图,适合开发者使用,简单易用

SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.COLUMNS 查看执行结果 SQL Server查看表结构及视图,适合开发者使用,简单易用,布布扣,bubuko.com

Sql Server 查看当前正在执行的Sql 语句

查看Sql Server 当前正在执行的Sql 语句,可以使用 sys.sysprocesses 或 sys.dm_exec_requests,由于sys.sysprocesses是为了向后兼容而保留的,所以,推荐使用DMV:sys.dm_exec_requests 一,使用sys.sysprocesses Contains information about processes that are running on an instance of SQL Server. These proce

快速查看SQL Server 中各表的数据量以及占用空间大小

快速查看SQL Server 中各表的数据量以及占用空间大小. CREATE TABLE #T (NAME nvarchar(100),ROWS char(20),reserved varchar(18) ,Data varchar(18) ,index_size varchar(18) ,Unused varchar(18) ) GO INSERT #T EXEC SP_MSFOREACHTABLE 'EXEC sp_spaceused "?"' SELECT * FROM #T O