码迷,mamicode.com
首页 > 其他好文 > 详细

backup3:backup devices 的查询和验证

时间:2016-07-13 13:30:47      阅读:574      评论:0      收藏:0      [点我收藏+]

标签:

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 options
Compression ,DESCRIPTION =Nbackup set description-site ,NAME =Nbackup set name-site --,RETAINDAYS =28 --Media set options ,INIT ,SKIP ,FORMAT ,MediaDescription=NMdiea 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 NDisk when 5 then NTape 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)

backup3:backup devices 的查询和验证

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5661373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!