backup3:backup devices 的查询和验证

Backup database 命令执行成功之后,SQL Server 会创建相应的Backup set,media set 和 backup media family,在msdb中,存在相应的table来查询metadata。

BACKUP DATABASE [TestSite]
TO disk = ‘D:\TestDBBackupFolder\Site1_db_bak1.bak‘,
   disk = ‘D:\TestDBBackupFolder\Site1_db_bak2.bak‘
MIRROR TO disk = ‘D:\TestDBBackupFolder\Site1_db_bak3.bak‘,
          disk = ‘D:\TestDBBackupFolder\Site1_db_bak4.bak‘
WITH
--backup set optionsCompression
,DESCRIPTION =N‘backup set description-site‘
,NAME =N‘backup set name-site‘
--,RETAINDAYS =28
--Media set options
,INIT
,SKIP
,FORMAT
,MediaDescription=N‘Mdiea Description-site‘
,MEDIANAME = ‘Meida_name-site‘;

一,查看backup的操作记录

1,查看Mediaset 信息

media_family_count:Number of media families in the media set.

mirror_count:Number of mirrors in the media set.

select media_set_id,
    media_family_count,
    name,
    description,
    mirror_count,
    isnull(is_compressed,0) as is_compressed
from msdb.dbo.backupmediaset

上述脚本创建的media set用于 2个mirror(两个backup),每个mirror拥有2个meida family,压缩备份。

2,查看backup media family

backupmediafamily Contains one row for each media family. If a media family resides in a mirrored media set, the family has a separate row for each mirror in the media set.

family_sequence_number:Position of this media family in the media set.

mirror:Mirror number (0-3), 0 表示 backup media family 是 To Clause 指定的。

select media_set_id,
    family_sequence_number,
    media_family_id ,
    --logical_device_name ,
    physical_device_name,
    case device_type when 2 then N‘Disk‘ when 5 then N‘Tape‘ end as device_type,
    mirror
from msdb.dbo.backupmediafamily
where media_set_id=1092
order by mirror,family_sequence_number

在每个mirror上,都有两个media family,media family都有一个序号 family sequence number,这个序号表示media family 在 To clause 或 Mirror to Clause中 的位置,基数是1,从左向右依次加1。

在To Clause 和 Mirror To Clause 相同位置处的两个backup device(序号相同的两个media family),其 media family id是相同的。

3,查看Backupset 信息

backupset Contains a row for each backup set.

A backup set contains the backup from a single, successful backup operation. RESTORE, RESTORE FILELISTONLY, RESTORE HEADERONLY, and RESTORE VERIFYONLY statements operate on a single backup set within the media set on the specified backup device or devices.

select backup_set_id,
    name,
    description,
    media_set_id,
    first_family_number,
    last_family_number,
    position,
    type,
    --expiration_date,
    --backup_start_date,
    --backup_finish_date,
    --backup_size,
    --compressed_backup_size,
    DATEDIFF(SECOND,backup_start_date,backup_finish_date) as backup_duration,
    cast((1-compressed_backup_size/backup_size)*100 as decimal(10,2)) as compression_ratio
from msdb.dbo.backupset
where media_set_id=1092

二,从单个backup device中查看backup set header

1,使用Restore HeaderOnly,查看单个Backup device(media family)的header info

Returns a result set containing all the backup header information for all backup sets on a particular backup device

RESTORE HEADERONLY
FROM <backup_device>
[ WITH
 {
--Backup Set Options
   FILE =  backup_set_file_number
]
[;]

<backup_device> ::=
{
   { logical_backup_device_name | DISK =  ‘physical_backup_device_name‘  }
} 

FILE = backup_set_file_number

Identifies the backup set to be restored. For example, a backup_set_file_number of 1 indicates the first backup set on the backup medium and a backup_set_file_number of 2 indicates the second backup set. You can obtain the backup_set_file_number of a backup set by using the RESTORE HEADERONLY statement.

When not specified, the default is 1, except for RESTORE HEADERONLY in which case all backup sets in the media set are processed.

File Option 指定 backup set 在backup devices中的位置,默认情况下,Restore HeaderOnly 会将Media Set中的所有backup set的信息都呈现。

restore headeronly
from disk = ‘D:\TestDBBackupFolder\Sitedb_bak2.bak‘
--with file=1;

返回的return set中,有几个关键的字段

BackupType/BackupTypeDescription1 = Database(Full backup), 2 = Transaction log, 5 = Differential database

Position:Position of the backup set in the volume (for use with the FILE = option).

ServerName:Name of the server that wrote the backup set.

DatabaseName:Name of the database that was backed up.

BackupSize:Size of the backup, in bytes.

BackupStartDate/BackupFinishDate :Date and time that the backup operation began / finished.

IsDamaged:1 = Database was damaged when backed up, but the backup operation was requested to continue despite errors.

2,从单个Backup Device中查看 Media Header Info

Executing RESTORE LABELONLY is a quick way to find out what the backup media contains. Because RESTORE LABELONLY reads only the media header, this statement finishes quickly even when using high-capacity tape devices.

RESTORE LABELONLY
FROM <backup_device> [;]

<backup_device> ::=
{ logical_backup_device_name   | DISK ‘physical_backup_device_name‘ } 

返回的return set中,有几个关键的字段

MediaName/MediaSetId/MediaDescription: Mdeia Info

FamilyCount:Number of media families in the media set.

FamilySequenceNumber:Sequence number of this family.

MediaSequenceNumber:Sequence number of this media in the media family.

Mirror_Count:Number of mirrors in the set (1-4).

restore LabelOnly
from disk = ‘D:\TestDBBackupFolder\Sitedb_bak2.bak‘;

三,查看backup中data files 和 log files 列表

RESTORE FILELISTONLY
FROM <backup_device>
[ WITH
 {
--Backup Set Options
   FILE = backup_set_file_number
 }
]
[;]

<backup_device> ::=
{  logical_backup_device_name|  DISK  ‘physical_backup_device_name‘ } 

FILE = backup_set_file_number

Identifies the backup set to be restored. For example, a backup_set_file_number of 1 indicates the first backup set on the backup medium and a backup_set_file_number of 2 indicates the second backup set. You can obtain the backup_set_file_number of a backup set by using the RESTORE HEADERONLY statement.

When not specified, the default is 1, except for RESTORE HEADERONLY in which case all backup sets in the media set are processed.

RESTORE FILELISTONLY
FROM disk = ‘D:\TestDBBackupFolder\Sitedb_bak4.bak‘
with file=1;

四,验证backup,在 From Cluase中必须包含所有的Backup devices

Verifies the backup but does not restore it, and checks to see that the backup set is complete and the entire backup is readable. However, RESTORE VERIFYONLY does not attempt to verify the structure of the data contained in the backup volumes. In Microsoft SQL Server, RESTORE VERIFYONLY has been enhanced to do additional checking on the data to increase the probability of detecting errors. The goal is to be as close to an actual restore operation as practical. For more information, see the Remarks.

If the backup is valid, the SQL Server Database Engine returns a success message.

RESTORE VERIFYONLY
FROM <backup_device> [ ,...n ]
[ WITH
 {
   LOADHISTORY
--Restore Operation Option
 | MOVE ‘logical_file_name_in_backup‘ TO ‘operating_system_file_name‘
          [ ,...n ]
--Backup Set Options
 | FILE = { backup_set_file_number | @backup_set_file_number }
]
[;]

<backup_device> ::=
{ logical_backup_device_name | DISK = ‘physical_backup_device_name‘ } 

1,File Option

FILE = backup_set_file_number

Identifies the backup set to be restored. For example, a backup_set_file_number of 1 indicates the first backup set on the backup medium and a backup_set_file_number of 2 indicates the second backup set. You can obtain the backup_set_file_number of a backup set by using the RESTORE HEADERONLY statement.

When not specified, the default is 1, except for RESTORE HEADERONLY in which case all backup sets in the media set are processed.

2,LOADHISTORY option

RESTORE VERIFYONLY FROM backup_device WITH LOADHISTORY populates the columns of the backupmediaset table with the appropriate values from the media-set header.

restore VERIFYONLY
from disk = ‘D:\TestDBBackupFolder\Sitedb_bak2.bak‘
with LoadHistory;

执行命令时报错,提示必须将所有的Media families(backup devices)都包含在From Clause中。

The media set has 2 media families but only 1 are provided. All members must be provided.

restore VERIFYONLY
from  disk = ‘D:\TestDBBackupFolder\Sitedb_bak2.bak‘,
      disk = ‘D:\TestDBBackupFolder\Sitedb_bak1.bak‘
with LoadHistory;

如果验证通过,SQL 会打印出验证 valid 的消息,因为没有指定File Option,默认值是File=1;

The backup set on file 1 is valid.

3,Move Option,用于验证目标Disk的Free Space是否能够容纳指定的Database files 和 Log
MOVE ‘logical_file_name_in_backup‘ TO ‘operating_system_file_name‘ [ ...n ]   

Specifies that the data or log file whose logical name is specified by logical_file_name_in_backup should be moved by restoring it to the location specified by operating_system_file_name. The logical file name of a data or log file in a backup set matches its logical name in the database when the backup set was created.

n is a placeholder indicating that you can specify additional MOVE statements. Specify a MOVE statement for every logical file you want to restore from the backup set to a new location. By default, the logical_file_name_in_backup file is restored to its original location. To obtain a list of the logical files from the backup set, use RESTORE FILELISTONLY.

If a RESTORE statement is used to relocate a database on the same server or copy it to a different server, the MOVE option might be necessary to relocate the database files and to avoid collisions with existing files.

When used with RESTORE LOG, the MOVE option can be used only to relocate files that were added during the interval covered by the log being restored. For example, if the log backup contains an add file operation for file file23, this file may be relocated using the MOVE option on RESTORE LOG.

If a RESTORE VERIFYONLY statement is used when you plan to relocate a database on the same server or copy it to a different server, the MOVE option might be necessary to verify that sufficient space is available in the target and to identify potential collisions with existing files.

restore VERIFYONLY
from  disk = ‘D:\TestDBBackupFolder\Sitedb_bak4.bak‘,
      disk = ‘D:\TestDBBackupFolder\Sitedb_bak3.bak‘
with
    LoadHistory
    ,move ‘Site_TestDB‘ to ‘D:\TestDBBackupFolder\Site_TestDB.mdf‘
    ,move ‘Site_TestDB‘ to ‘D:\TestDBBackupFolder\Site_TestDB.mdf‘
    ,file=2;

参考文档:

backupmediaset (Transact-SQL)

backupset (Transact-SQL)

backupmediafamily (Transact-SQL)

RESTORE HEADERONLY (Transact-SQL)

RESTORE VERIFYONLY (Transact-SQL)

RESTORE LABELONLY (Transact-SQL)

RESTORE FILELISTONLY (Transact-SQL)

时间: 2024-12-10 13:11:56

backup3:backup devices 的查询和验证的相关文章

backup2:backup devices

Backup Devices 是存储SQL Server 数据和Trasaction Log备份的文件,分为两种:Physical backup device 和 logical backup device.physical backup device 是指Windows OS的file,其 physical name 类似:"D:\FolderPaths\FileName.bak",也可以是远程共享网络上一个file,其physical name 类似:"\\ServerN

DG备库,实时应用如何判断,MR进程,及MRP应用归档,三种情况的查询及验证

本篇文档学习,DG备库,实时应用如何判断,MR进程,及MRP应用归档,三种情况的查询及验证 1.取消MRP进程 备库查询进程状态select process,client_process,sequence#,status,BLOCK#,BLOCKS from v$managed_standby;PROCESS CLIENT_P SEQUENCE# STATUS BLOCK# BLOCKS--------- -------- ---------- ------------ ---------- -

spring security使用hibernate进行查询数据库验证

前面查询数据库采用的都是jdbc方式,如果系统使用的是hibernate,该如何进行呢,下面就是实现步骤,关键还是实现自定义的UserDetailsService 项目结构如下: 使用hibernate,pom.xml文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLo

sql 多表查询结果验证

1.笛卡尔积 定义: 设A,B为集合,用A中元素为第一元素,B中元素为第二元素构成的有序对,所有这样的有序对组成的集合 ,叫做A与B的笛卡尔积,记作AxB. 上面有一个很关键的词为"有序",因此,我们来看一个例子: 例如,A={a,b},B={0,1,2},则 AxB={<a,o>,<a,1>,<a,2>,<b,0>,<b,1>,<b,2>,} BxA={<0,a>,<0,b>,<1,

mysql开启慢查询日志及验证过程

mysql慢查询 1.慢查询作用? 它能记录下所有执行超过long_query_time时间的sql语句,帮你找到执行慢的sql,方便我们对这些sql语句进行优化. 2.如何开启慢查询? 首先我们先查看mysql服务器的慢查询状态是否开启. 执行如下指令: 我们可以看到log_slow_queries状态为ON,说明当前已经开启慢查询.如果没有开启,进行如下操作: 方法一:找到mysql的配置文件my.cnf(/etc/my.cnf),在mysqld下方加入慢查询的配置语句(注意一定要在[mys

DbContext 查询

使用LINQ to Entities来写查询语句 Entity Framework查询是使用的.NET Framework功能Language Integrated Query,AKA LINQ.LINQ与.NET的编程体验是紧密集成在一起的,它提供了强类型的查询,何谓强类型,各位自行补脑.与弱类型的查询相比,它提供了编译时检查来保证你的查询通过验证以及IntelliSense. LINQ是一个通用的查询框架,并不仅仅是针对Entity Framework或者数据库的,LINQ提供程序负责把LI

查询rman备份信息经常使用指令

查询rman备份信息经常使用指令 ----登陆到rman $rman target / ----以精简的格式查看备份信息 RMAN> list backup of database summary; List of Backups =============== Key     TY LV S Device Type Completion Time#Pieces #Copies Compressed Tag 备份片号 类型 备份级别 设备类型 完毕时间 片数 镜像数 是否压缩 标签 ------

backup4:backup operation

在backup operation 中,必须控制好 Init,NoSkip 和 Format 三个选项. 1,在做Full backup时,使用 Format Option 创建新的Media Set Format选项和NoSkip选项,NoInit选项是冲突的,在使用Format选项时,必须和 Skip, Init选项搭配. Option 'format' conflicts with option(s) 'noskip'. Option 'format' conflicts with opt

DIOCP之DEMO-登陆验证设计

登陆设计原理: (一)client在与服务器建立连接后向服务器发送含有"USER=XXXXX&PWD=XXXXX"或者用JSON字符串. client的代码需要写在OnContextConnected事件中,注{"cmd":"login","User":"admin","Pwd":"admin888"} (二)服务器收到客户端传来的命令串后,首先判断命令是不是