天天看點

HOW TO USE RMAN

RMAN的詳細文法查《Database Backup and Recovery Reference》,此文檔未看有時間可以看一遍

如果忘記RMAN文法可以慢慢試出來,如忘記LIST文法則輸入LIST後顯示

RMAN-01009: syntax error: found ";": expecting one of: "all, archivelog, backed, backuppiece, backupset, backup, completed, controlfilecopy, copy, datafilecopy, db_unique_name, device, expired, failure, foreign, global, incarnation, like, proxy, recoverable, restore, script, tag"

這時輸入list backup會顯示備份集,如果後面還想加東西,可以輸入list backup dd, 這時顯示

RMAN-01009: syntax error: found "identifier": expecting one of: "backed, by, completed, controlfile, device, for, like, of, recoverable, summary, tag, ;"

再輸入list backup by

RMAN-01009: syntax error: found ";": expecting one of: "backup, file"

再輸入list backup by file

(一)CONNECTION

連接配接target/auxiliary database需要SYSDBA/SYSBACKUP權限,連接配接catalog database需要RECOVERY_CATALOG_OWNER角色權限. RMAN同SQLPLUS也包括OS認證及密碼檔案認證方式。如果本地登陸連接配接前确認ORACLE_SID環境變量設定

Users connecting with RMAN to a target or auxiliary database require either the SYSDBA or SYSBACKUP system privilege.

Note:To support connecting through the password file with the SYSBACKUP privilege, the password file must be created in or upgraded to the format for Oracle Database 12c Release 1 (12.1) or later.

These privileges are not required when connecting to the recovery catalog. You must grant the RECOVERY_CATALOG_OWNER role to the catalog schema owner. Users can also connect to the recovery catalog using the VPC credentials that have been created by the recovery catalog owner.

The same authentication options that are available with SQL*Plus are available with RMAN. The most common ways to authenticate with the target and auxiliary databases are:

  1. Operating system authentication
  2. Password file authentication

Neither of these methods requires the database to be open. Operating system authentication is used only to connect locally. Password file authentication can be used to connect locally or remotely.

連接配接示例:

$ rman target /       ---預設以SYSDBA登陸

$ rman target '"/ as sysdba"'

$ rman target '"[email protected] as sysbackup"'

$ rman 

RMAN> CONNECT TARGET "[email protected] AS SYSBACKUP"

$ rman target / auxiliary rman/[email protected]

$ rman target sys/[email protected] catalog rman/[email protected]

$ rman target sys/[email protected]

RMAN> connect catalog rman/[email protected]

注釋(#)、日志與腳本使用:

$ rman TARGET / LOG /tmp/msglog.log APPEND  

$ rman | tee rman.log  --同時螢幕及日志中輸出

$ cat /tmp/backup_db.rman

BACKUP TAG &1 COPIES &2 DATABASE;

EXIT;

$ rman TARGET / @backup_db.rman Q106 2

$ rman TARGET /

RMAN> @/tmp/backup_db.rman Q106 2

RMAN> RUN {

2> @backup_db.rman Q106 2  # 此後為注釋

3> ...

5> }

另外也可以用以下形式:$ rman target / cmdfile xxxx log xxxx

檢查文法:$ rman checksyntax @backup_db.rman

@@用于解決文檔套文檔的目錄問題,在檔案中使用@@表示引用文檔在本檔案的目前目錄

$ cat /tmp/cmd1.rman

@@cmd2.rman

$ rman TARGET /

RMAN> @/tmp/cmd1.rman

In this case, the @@ command directs RMAN to search for the file cmd2.rman in the directory /tmp.

(二)RMAN Reporting

The following table lists the techniques used to access metadata from the RMAN repository.

  1. RMAN LIST and REPORT commands:用于RMAN備份恢複
  2. V$ views:用于手動備份恢複

When the database is open, several V$ views provide direct access to RMAN repository records in the control file of each target database. Example: V$BACKUP_PIECE, V$BACKUP_SET

In some cases, V$ views supply information that is not available through use of the LIST and REPORT commands. Some V$ views such as V$DATAFILE_HEADER, V$PROCESS, and V$SESSION contain information not found in the recovery catalog views.

  1. RC_ views:用于使用catalog

If your database is registered in a recovery catalog, then RC_ views provide direct access to the RMAN repository data stored in the recovery catalog. The RC_ views mostly correspond to the V$ views.

  1. RESTORE ... PREVIEW and RESTORE ... VALIDATE HEADER commands

RESTORE ... PREVIEW queries the metadata but does not read the backup files. The RESTORE ... VALIDATE HEADER command performs the same work, but in addition to listing the files needed for restore and recovery operations, the command validates the backup file headers to determine whether the files on disk or in the media management catalog correspond to the metadata in the RMAN repository.

Note: The RMAN repository can sometimes fail to reflect the reality on disk and tape. For example, a user may delete a backup with an operating system utility, so that the RMAN repository incorrectly reports the backup as available. You can use commands such as CHANGE, CROSSCHECK, and DELETE to update the RMAN repository to reflect the actual state of available backups.

Reporting in CDBs and PDBs

在CDB$root使用common user連接配接可以檢視整個CDB下RMAN資訊,方式同non-CDB

The steps to view reporting information for a CDB are similar to the ones used for a non-CDB. The only difference is that you must connect to the root as a common user with the common SYSBACKUP or common SYSDBA privilege.

The LIST and LIST BACKUP OF commands will display backups of the whole CDB. The REPORT NEED BACKUP TABLESPACE command displays information about the tablespaces in the root that need backup.

檢視部分PDB的RMAN資訊有兩種方式

  1. Connect to the root and use the LIST ... PLUGGABLE DATABASE or REPORT PLUGGABLE DATABASE commands. This enables you to display information regarding one or more PDBs.

LIST BACKUP OF PLUGGABLE DATABASE hr_pdb, sales_pdb;

  1. Connect to the PDB and use the LIST BACKUP or REPORT commands. This approach displays information for only one PDB and also uses the same commands that are used for non-CDBs.

Note:

  1. When connected to a PDB, you cannot view reporting information about obsolete backups or delete obsolete backups.
  2. After a PDB is dropped, you cannot perform operations or query data dictionary views by using the PDB name. However, you can obtain information about dropped PDBs by querying using GUID of a PDB.

Use the LIST command with the GUID option to list backups of pluggable databases (PDBs) that have been dropped from a multitenant container database (CDB):

SELECT pdb_name, pdb_guid FROM dba_pdb_history WHERE db_name = 'test_db';

LIST BACKUP GUID 'CDFFD672330A7527D0147204CD0E08D4';

LIST COPY GUID 'CDFFD672330A7527D0147204CD0E08D4';

  1. Listing Backups and Recovery-Related Objects

Table 11-2 LIST Objects

Contents of List Command Description
Backup sets and proxy copies LIST BACKUP You can list all backup sets, copies, and proxy copies of a database, tablespace, data file, archived redo log, control file, or server parameter file.
Image copies LIST COPY You can list data file copies and archived redo log files. By default, LIST COPY displays copies of all database files and archived redo logs. Both usable and unusable image copies are included in the output, even those that cannot be restored or are expired or unavailable.
Archived redo log files LIST ARCHIVELOG You can list archive redo log files. You can list all archive log redo log files or specify individual archive log files through SCN, time, or sequence number ranges. If you specify a range you can further restrict the list returned by specifying an incarnation number.
Database incarnations LIST INCARNATION You can list all incarnations of a database. A new database incarnation is created when you open with the RESETLOGS option.
Databases in a Data Guard environment LIST DB_UNIQUE_NAME A database in a Data Guard environment is distinguished by its DB_UNIQUE_NAME initialization parameter setting. You can list all databases that have the same DBID.
Backups and copies for a primary or standby database in a Data Guard environment LIST ... FOR DB_UNIQUE_NAME

You can list all backups and copies for a specified database in a Data Guard environment or for all databases in the environment.

RMAN restricts the output to files or objects associated exclusively with the database with the specified DB_UNIQUE_NAME. For example, you can use LIST with FOR DB_UNIQUE_NAME to display the list of archived redo log files associated with a particular standby or primary database. Objects that are not owned by any database (SITE_KEY column in the recovery catalog view is null) are not listed.

Restore points LIST RESTORE POINT You can list restore points known to the RMAN repository.
Names of stored scripts LIST SCRIPT NAMES

You can list the names of recovery catalog scripts created with the CREATE SCRIPT or REPLACE SCRIPT command. A recovery catalog is required.

LIST SCRIPT NAMES;   --列出目前target資料庫可用的内部腳本

LIST ALL SCRIPT NAMES;  --列出所有内部腳本

Failures for use with Data Recovery Advisor LIST FAILURE

List failure;  隻列出critical與high級别的故障

List failure all; 列出所有級别的故障

The LIST command supports options that control how output is displayed. Table 11-3 summarizes the most common LIST options.

Table 11-3 Most Common LIST Options

LIST Option Description
LIST EXPIRED Lists backups or copies that are recorded in the RMAN repository but that were not present at the expected location on disk or tape during the most recent crosscheck. Such backups may have been deleted outside of RMAN.
LIST ... BY FILE Lists backups of each data file, archived redo log file, control file, and server parameter file. Each row describes a backup of a file. 按備份檔案類型分類
LIST ... SUMMARY Provides a one-line summary of each backup.

You can specify several different conditions to narrow your LIST output:

LIST BACKUP OF DATABASE;

LIST EXPIRED BACKUP OF DATABASE;

LIST BACKUPSET 213;

LIST DATAFILECOPY '/tmp/tools01.dbf';

LIST BACKUPSET TAG 'weekly_full_db_backup';

LIST COPY OF DATAFILE 'ora_home/oradata/trgt/system01.dbf' DEVICE TYPE sbt;

LIST COPY LIKE '/tmp/%';

LIST COPY OF DATAFILE 2 COMPLETED BETWEEN '10-DEC-2012' AND '17-DEC-2012';

LIST ARCHIVELOG ALL;  ---檢視歸檔日志

LIST BACKUP OF DATAFILE 5;

LIST BACKUP OF DATAFILE 'F:\oracle\oradata\jssbook\scott_tbs01.dbf';

LIST BACKUP OF ARCHIVELOG ALL;   ---檢視歸檔備份

LIST BACKUP OF ARCHIVELOG UNTIL TIME 'SYSDATE - 1';

LIST BACKUP OF ARCHIVELOG UNTIL TIME "TO_DATE('2013-4-1','YYYY-MM-DD')";

LIST DEVICE TYPE DISK BACKUP;

LIST EXPIRED BACKUP OF ARCHIVELOG UNTIL SEQUENCE 3;

LIST ARCHIVELOG ALL BACKED UP 2 TIMES TO DEVICE TYPE sbt;

LIST INCARNATION OF DATABASE prod3;  --使用catalog使用of database限定資料庫

LIST RECOVERABLE BACKUP OF DATABASE;  --列出可用于恢複的備份(狀态為available)

  1. Reporting on Backups and Database Schema

REPORT可以顯示資料庫是否可恢複

  1. REPORT NEED BACKUP

Reports which database files must be backed up to meet a configured or specified retention policy

  1. REPORT UNRECOVERABLE

When a data file has been changed by an unrecoverable operation, such as a direct load insert, normal media recovery cannot be used to recover the file, because an unrecoverable operation does not generate redo. You must perform either a full or incremental backup of affected data files after such operations, to ensure that data blocks affected by the unrecoverable operation can be recovered using RMAN. 注OFFLINE或READONLY不算UNRECOVERABLE

The RMAN repository contains other information that you can access with the REPORT command:

Contents of Report Command Description
Obsolete backups REPORT OBSOLETE Full backups, data file copies, and archived redo logs recorded in the RMAN repository that can be deleted because they are no longer needed
Database schema REPORT SCHEMA

The names of all data files (permanent and temporary) and tablespaces for the target database at the specified point in time.

If you use RMAN in a Data Guard environment, then you can report the schema for a specified DB_UNIQUE_NAME.

示例:

REPORT NEED BACKUP TABLESPACE TBS_3;   # uses configured retention policy

REPORT NEED BACKUP REDUNDANCY 2 DATAFILE 1;

REPORT NEED BACKUP RECOVERY WINDOW OF 2 DAYS DATABASE SKIP TABLESPACE TBS_2;

REPORT NEED BACKUP INCREMENTAL 2;  # 顯示需要應用2個增量備份進行恢複的檔案

REPORT NEED BACKUP DAYS 5;  #5天未備份資料檔案

REPORT NEED BACKUP DEVICE TYPE DISK;   

REPORT UNRECOVERABLE;

REPORT OBSOLETE;

REPORT OBSOLETE RECOVERY WINDOW OF 3 DAYS;

REPORT OBSOLETE REDUNDANCY 1;

REPORT SCHEMA AT TIME 'SYSDATE-14';     # schema 14 days ago

REPORT SCHEMA

下面at子句必須在catalog裡使用

REPORT SCHEMA AT SCN 1000;              # schema at scn 1000

REPORT SCHEMA AT SEQUENCE 100 THREAD 1; # schema at sequence 100

REPORT SCHEMA FOR DB_UNIQUE_NAME standby1; # schema for database standby1

(三)Deleting RMAN Backups and Archived Redo Logs

不論檔案存于DISK還是SBT亦或是否使用閃回恢複區,DELETE均會删除實體檔案。另外RMAN Repository是否使用controlfile控制是否删除repository記錄

Because of the way that control file data is stored, RMAN cannot remove the record from the control file, only update it to DELETED status. Because the recovery catalog tables are ordinary database tables, however, RMAN deletes rows from them in the same way that rows are deleted from any table.

可以使用DELETE或BACKUP...DELETE [ALL] INPUT 進行删除,後者用于備份包括歸檔日志、資料檔案的image copy及backup set檔案時使用,在成功備份後會删除輸入檔案

If you specify DELETE INPUT (without ALL), then RMAN deletes only the specific files that it backs up. If you specify ALL INPUT, then RMAN deletes all copies of the files recorded in the RMAN repository.

在執行CROSSCHECK或DELETE時也可以指定多CHANNEL,它會像backup指令一樣并行進行查找或删除。注意如果多個檔案分布在磁盤及錄音帶時,要全局配置預設裝置為SBT或手動ALLOCATE SBT CHANNEL, 而DISK CHANNEL不配置預設也會有

You can configure or manually allocate multiple channels before issuing CROSSCHECK or DELETE commands. RMAN searches for each backup on all channels that have the same device type as the channel used to create the backup.

RMAN uses all configured channels to perform the deletion. If you use DELETE for files on devices that are not configured for automatic channels, then you must first use ALLOCATE CHANNEL FOR MAINTENANCE command.

For example, if you made a backup with the SBT channel, but only a disk channel is configured, then you must manually allocate an SBT channel for DELETE. An automatic or manually allocated maintenance channel is required when you use DELETE command on a disk-only file.

Deleting Backups of Dropped PDBs

The DELETE BACKUP ... OF PLUGGABLE DATABASE command deletes backups of the specified PDB. However, after a PDB is dropped, you cannot use this command because the a PDB with the specified name no longer exists. In such cases, use the DELETE BACKUP … GUID command to delete backups of dropped PDBs. Each PDB has a globally unique identifier (GUID) which can be used to uniquely identify a PDB. The GUID of dropped PDBs is available in the DBA_PDB_HISTORY view.

SELECT pdb_name, pdb_guid FROM dba_pdb_history

WHERE db_name = 'prod_db';

DELETE BACKUP GUID '100E64EC12445321C0352900AF0FAC93';

DELETE COPY GUID '100E64EC12445321C0352900AF0FAC93';

EXAMPLE:

DELETE BACKUP;      # deletes all backups

DELTE COPY;          # deletes all image copies (including archived logs)

DELETE NOPROMPT ARCHIVELOG ALL;   --不确認直接删除

DELETE FORCE ARCHIVELOG ALL;    

出現I/O錯誤(如檔案不存在)時,直接修改RMAN repository反應此檔案已删除而不是報錯

如果删除的是歸檔日志,預設不會删除低于configure archivelog deletion policy的備援備份配置的歸檔日志,如果此配置為NONE,則不滿足至少1份備份或recover retention配置的歸檔不會被删。這時使用FORCE來忽略上面限制強制删除

DELETE BACKUPPIECE 101;

DELETE BACKUPSET 101;

DELETE CONTROLFILECOPY '/tmp/control01.ctl';

DELETE DATAFILECOPY ALL;

DELETE COPY OF CONTROLFILE LIKE '/tmp/%';

DELETE BACKUP OF TABLESPACE users DEVICE TYPE sbt;

DELETE BACKUP OF SPFILE;

DELETE ARCHIVELOG UNTIL SEQUENCE 300;

DELETE BACKUP TAG 'before_upgrade';

DELETE ARCHIVELOG ALL BACKED UP 3 TIMES TO sbt;

DELETE EXPIRED BACKUP|COPY;

DELETE OBSOLETE;

(四)ALLOCATE CHANNEL

  1. ALLOCATE CHANNEL

隻能用于RUN BLOCK, 使用後不能再使用預設channel

ALLOCATE CHANNEL manually allocates a channel, which is a connection between RMAN and a database instance. The ALLOCATE CHANNEL command must be issued within a RUN block. You cannot use BACKUP DEVICE TYPE or RESTORE DEVICE TYPE to use automatic channels after specifying manual channels with ALLOCATE CHANNEL.

You can allocate up to 255 channels; each channel can read up to 64 files in parallel.

You can control the degree of parallelism within a job by the number of channels that you allocate. Allocating multiple channels simultaneously allows a single job to read or write multiple backup sets or disk copies in parallel, with each channel operating on a separate backup set or copy.

ALLOCATE CHANNEL Syntax:

ALLOCATE [AUXILIARY] CHANNEL channel_id DEVICE TYPE DISK/SBT allocOperandList;

allocOperandList::=

AUXILIARY: Specifies a connection between RMAN and an auxiliary database instance.

An auxiliary instance is used when executing the DUPLICATE or TRANSPORT TABLESPACE command, and when performing TSPITR with RECOVER TABLESPACE. When specifying this option, the auxiliary instance must be started but not mounted.

CHANNEL channel_id

Specifies a connection between RMAN and the target database instance. The channel_id is the case-sensitive name of the channel. The database uses the channel_id to report I/O errors.

Each connection initiates a database server session on the target or auxiliary instance: this session performs the work of backing up, restoring, or recovering RMAN backups. You cannot make a connection to a shared server session.

Whether ALLOCATE CHANNEL allocates operating system resources immediately depends on the operating system. On some platforms, operating system resources are allocated at the time the command is issued. On other platforms, operating system resources are not allocated until you open a file for reading or writing.

Each channel operates on one backup set or image copy at a time. RMAN automatically releases the channel at the end of the job.

Note: You cannot prefix ORA_ to a channel name. RMAN reserves channel names beginning with the ORA_ prefix for its own use.

示例

RUN {

  ALLOCATE CHANNEL disk1 DEVICE TYPE DISK FORMAT '/disk1/%U';

  ALLOCATE CHANNEL disk2 DEVICE TYPE DISK FORMAT '/disk2/%U';

  BACKUP DATABASE PLUS ARCHIVELOG;

}

RUN {

  ALLOCATE CHANNEL t1 DEVICE TYPE sbt

    PARMS 'ENV=(OB_DEVICE_1=stape1,OB_DEVICE_2=stape3)';

  ALLOCATE CHANNEL t2 DEVICE TYPE sbt

    PARMS 'ENV=(OB_DEVICE_1=stape2,OB_DEVICE_2=stape4)';

  SET BACKUP COPIES 2;

  BACKUP DATABASE;

}

RUN

{

  ALLOCATE AUXILIARY CHANNEL c1 DEVICE TYPE sbt;

  DUPLICATE TARGET DATABASE TO dupdb   

    DB_FILE_NAME_CONVERT '/disk2/dbs/','/disk1/'

    SPFILE

    PARAMETER_VALUE_CONVERT '/disk2/dbs/', '/disk1/'

    SET LOG_FILE_NAME_CONVERT '/disk2/dbs/', '/disk1/';

}

  1. ALLOCATE CHANNEL FOR MAINTENANCE

Use the ALLOCATE CHANNEL FOR MAINTENANCE command to manually allocate a channel in preparation for issuing a CHANGE, DELETE, or CROSSCHECK command. You can use the RELEASE CHANNEL command to unallocate the channel.

Oracle不建議單配置設定maintenance channel而是使用通用channel, Maintenance channel類似預設channel,在整個RMAN session中有效,不能用于RUN BLOCK. 可以用預設channel替代,是以不用看了

If you CONFIGURE at least one channel for each device type in your configuration, then you do not need to use ALLOCATE CHANNEL FOR MAINTENANCE.

Oracle recommends that you use configured channels instead of maintenance channels. You can use configured channels for all RMAN I/O to the specified device, not just the maintenance tasks supported by maintenance channels. The configured channels persist across RMAN sessions.

Execute this command only at the RMAN prompt, not within a RUN block. The target instance must be started. You cannot allocate a maintenance channel to a shared session.

  1. RELEASE CHANNEL

用于關閉ALLOCATE CHANNEL與ALLOCATE CHANNEL FOR MAINTENANCE的channel

To release a normal channel:

在run中執行:RELEASE CHANNEL channel_id;

To release a maintenance channel:

在RMAN提示符下執行:RELEASE CHANNEL;

此指令也不用了,因為RUN BLOCK中的ALLOCATE CHANNEL會在run塊執行完自動釋放,而Maintenance Channel也不用了

Using RELEASE CHANNEL to release channels within RUN is optional, because RMAN automatically releases all normal channels when a RUN command terminates.

(五)SET

使用SET配置目前session環境,可以在RMAN提示符下或RUN BLOCK中使用

Use the SET command to control RMAN behavior within a job or session. Use CONFIGURE to configure options that persist across sessions.

You can use the SET command either at the RMAN prompt or within a RUN block. When used at the RMAN prompt, changes made by SET persist until you exit the RMAN client. When used inside of a RUN block, changes made by SET persist until the end of the RUN block or the next SET command that changes the value of the same attribute.

Set Syntax

  1. setRmanOption::=
  1. setRunOption::=

3. setRmanOrRunOption::=

具體選項說明用時再查,舉幾個例子

RUN {

  ALLOCATE CHANNEL dev1 DEVICE TYPE sbt

    PARMS 'ENV=(OB_DEVICE_1=stape1,OB_DEVICE_2=stape2)';

  SET BACKUP COPIES 2;

  BACKUP DATABASE PLUS ARCHIVELOG;

}

RUN {

  SET CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/disk2/cf_%F.bak';

  RESTORE CONTROLFILE FROM AUTOBACKUP MAXSEQ 100;

}

預設RMAN将歸檔恢複到初始參數LOG_ARCHIVE_DEST_1下

RUN {

SET ARCHIVELOG DESTINATION TO 'F:\oracle\backup\arclog';

RESTORE ARCHIVELOG SEQUENCE BETWEEN 35 AND 40;

}

Assume that you want to duplicate a database without a connection to the target database and that you want to duplicate to an incarnation that is not in the current incarnation path (451). The following example uses the various SET NEWNAME commands, sets the DBID and duplicates the database to NEWDB:

SET DATABASE PROD

SET DBID 22398754

SET INCARNATION 451

RUN {

SET NEWNAME FOR TABLESPACE system TO '/test/oradata/system/%d_%f';

SET NEWNAME FOR TABLESPACE users to '/test/oradata/users/%b';

SET NEWNAME FOR DATAFILE 35 to '/test/oradata/special/%N_%b_%f';

SET NEWNAME FOR DATAFILE 50 to '/test/oradata/special/%N_%b_%f';

SET NEWNAME FOR DATABASE to NEW;

DUPLICATE DATABASE TO newdb

SKIP READONLY

LOGFILE

GROUP 1 ('/test/onlinelogs/redo01_1.f',

'?/test/onlinelogs/redo01_2.f') SIZE 4M,

GROUP 2 ('?/test/onlinelongs/redo02_1.f',

'?/test/onlinelongs/redo02_2.f') SIZE 4M,

GROUP 3 ('?/test/onlinelogs/redo03_1.f',

'?/test/onlinelogs/redo03_2.f') SIZE 4M REUSE;

}

(六)SWITCH

SWITCH指令會更改控制檔案中記錄的資料檔案位置資訊,主要用于原媒體損壞情況

A SWITCH is equivalent to the SQL statement ALTER DATABASE RENAME FILE: the names of the files in the RMAN repository are updated, but the database does not rename the files at the operating system level.

Use the SWITCH command to perform either of the following operations:

  1. Update the file names for a database, tablespace, or data file to the latest image copies available for the specified files
  2. Update the file names for data files and temp files for which you have issued a SET NEWNAME command

在執行SWITCH時它會從RMAN repository中删除使用的image cope

The SWITCH command deletes the RMAN repository records for the data file copy from the recovery catalog and updates the control file records to status DELETED.

If RMAN is connected to a recovery catalog, and if the database is using a control file restored from backup, then SWITCH updates the control file with records of any data files known to the recovery catalog but missing from the control file.

在RMAN提示符下要用TO COPY,在RUN塊下不要用TO COPY

Execute SWITCH ... TO COPY only at the RMAN prompt.

Use SWITCH without TO COPY only within a RUN block.

  1. SWITCH .. TO COPY

此方式無法設定具體switch到哪個copy備份,而在run塊中可以

This subclause switches file names for a database, tablespace, or data file to the latest image copies available for the specified files. By executing this command, you avoid restoring data files from backups.

  1. SWITCH without TO COPY

This subclause updates the names for data files and temp files for which you have issued a SET NEWNAME command. Use this clause only within a RUN block.

Examples

Example 3-74 Switching to Image Copies to Avoid Restoring from Backup

ALTER TABLESPACE users OFFLINE IMMEDIATE;

SWITCH TABLESPACE users TO COPY;

RECOVER TABLESPACE users;

ALTER TABLESPACE users ONLINE;

Example 3-75 Switching Data File Names After a Restore to a New Location

RUN {

  ALLOCATE CHANNEL dev1 DEVICE TYPE DISK;

  ALLOCATE CHANNEL dev2 DEVICE TYPE sbt;

  ALTER TABLESPACE users OFFLINE IMMEDIATE;

  SET NEWNAME FOR DATAFILE '/disk1/oradata/prod/users01.dbf' TO '/disk2/users01.dbf';

  RESTORE TABLESPACE users;

  SWITCH DATAFILE ALL;

  RECOVER TABLESPACE users;

  ALTER TABLESPACE users ONLINE;

}

Example 3-76 Renaming Tempfiles Using SET NEWNAME and SWITCH TEMPFILE ALL

The temp files are re-created at the new locations when the database is opened.

CONNECT TARGET /

STARTUP FORCE MOUNT

RUN {  

  SET NEWNAME FOR TEMPFILE 1 TO '/disk2/temp01.dbf';

  SET NEWNAME FOR TEMPFILE 2 TO '/disk2/temp02.dbf';

  SET NEWNAME FOR TEMPFILE 3 TO '/disk2/temp03.dbf';

  SWITCH TEMPFILE ALL;  

  RESTORE DATABASE;  

  RECOVER DATABASE;  

  ALTER DATABASE OPEN;

}

Example 3-77 Switching to a Data File Copy

RUN {

  ALTER TABLESPACE tools OFFLINE IMMEDIATE;

  SWITCH DATAFILE '/disk1/oradata/prod/tools01.dbf' TO DATAFILECOPY '/disk2/tools.copy';

  RECOVER TABLESPACE tools;

  ALTER TABLESPACE tools ONLINE;

}

(七)BACKUP Command

1. BACKUP Syntax

BACKUP <LEVEL> (<BACKUP TYPE> <OPTION>);

LEVEL: FULL or INCREMENTAL, INCREMENTAL有0~4增量級(0級為全備)

BACKUP TYPE:常用如DATABASE/TABLESPACE/DATAFILE/ARCHIVELOG[ALL]/ CURRENT CONTROLFILE/DATAFILECOPY[TAGE]/CONTROLFILECOPY/BACKUPSET[ALL]

OPTION:

  1. Tag :辨別一系列備份,如一系列的增量備份政策。是以不同備份可以有相同TAG

Tags do not need to be unique. Assume that you specify that a data file be restored from backups that have a specific tag. If multiple backups of the requested file have the desired tag, then RMAN restores the most recent backup that has the desired tag, within any constraints on the RESTORE command.

In practice, tags are often used to distinguish a series of backups created as part of a single strategy, such as an incremental backup strategy.

BACKUP中設定TAG标記,在RESTORE或RECOVER指令中使用TAG

Many forms of the BACKUP command let you associate a tag with a backup, and many RESTORE and RECOVER commands let you specify a tag to restrict which backups to use in the RESTORE or RECOVER operation.

When you tag a backup set, the tag is an attribute of each backup piece in a given copy of a backup set. If you create a multiplexed backup set, then each copy of the backup set is assigned the same tag.

TAG的預設格式為TAGYYYYMMDDTHHMMSS

If you do not explicitly specify a tag with the TAG parameter of the BACKUP command, then RMAN implicitly creates a default tag for backups (except for control file autobackups). The format of the tag is TAGYYYYMMDDTHHMMSS, where YYYY is the year, MM is the month, DD is the day, HH is the hour (in 24-hour format), MM is the minutes, and SS is the seconds.

For example, a backup of data file 1 may get the tag TAG20070208T133437. The date and time refer to when RMAN started the backup in the time zone of the instance performing the backup. If multiple backup sets are created by one BACKUP command, then each backup piece has the same default tag.

Tags are stored in uppercase, regardless of the case used when entering them. The maximum length of a backup tag is 30 bytes. Tags cannot use operating system environment variables or use special formats such as %T or %D.

在ASM下不能使用(-)作為TAG名

The characters in a tag must be limited to the characters that are legal in file names on the target database file system. For example, Automatic Storage Management (ASM) does not support the use of the hyphen (-) in the file names it uses internally, so a tag including a hyphen (such as weekly-incr) is not a legal tag name for backups in ASM disk groups.

You can use FROM TAG to copy an image copy with a specific tag, and then use TAG to assign the output copy a different tag.

BACKUP AS COPY COPY OF DATABASE

  FROM TAG full_cold_copy TAG new_full_cold_copy;

  1. Format:生成備份檔案的路徑與名稱

Use the formatSpec subclause to specify a file name format or an Automatic Storage Management disk group for a backup piece or image copy. If you do not specify a value for the FORMAT parameter, then RMAN either creates the backup in the fast recovery area if it is enabled, or in a platform-specific directory (for example, ?/dbs on UNIX) if a fast recovery area is not enabled. In either case, RMAN uses the variable %U to name the backup.

The following table lists RMAN substitution variables that are valid in format strings.

Syntax Element Description
%a Specifies the activation ID of the database.
%b Specifies the file name stripped of directory paths. It is only valid for SET NEWNAME and backup when producing image copies It yields errors if used as a format specification for a backup that produces backup pieces.
%c Specifies the copy number of the backup piece within a set of duplexed backup pieces. If you did not duplex a backup, then this variable is 1 for backup sets and 0 for proxy copies. If a command is enabled, then the variable shows the copy number. The maximum value for %c is 256.
%d Specifies the name of the database (see Example 4-23).
%D Specifies the current day of the month from the Gregorian calendar in format DD.
%e Specifies the archived log sequence number.
%f Specifies the absolute file number (see Example 4-23).
%F

Combines the DBID, day, month, year, and sequence into a unique and repeatable generated name. This variable translates into c-IIIIIIIIII-YYYYMMDD-QQ, where:

IIIIIIIIII stands for the DBID. The DBID is printed in decimal so that it can be easily associated with the target database.

YYYYMMDD is a time stamp in the Gregorian calendar of the day the backup is generated

QQ is the sequence in hexadecimal number that starts with 00 and has a maximum of 'FF' (256)

Note: %F is valid only in the CONFIGURE CONTROLFILE AUTOBACKUP FORMAT command.

%h Specifies the archived redo log thread number.
%I Specifies the DBID.
%M Specifies the month in the Gregorian calendar in format MM.
%N Specifies the tablespace name. This substitution variable is only valid when backing up data files as image copies.
%n Specifies the name of the database, padded on the right with x characters to a total length of eight characters. For example, if prod1 is the database name, then the padded name is prod1xxx.
%p

Specifies the piece number within the backup set. This value starts at 1 for each backup set and is incremented by 1 as each backup piece is created.

Note: If you specify PROXY, then the %p variable must be included in the FORMAT string either explicitly or implicitly within %U.

%s Specifies the backup set number. This number is a counter in the control file that is incremented for each backup set. The counter value starts at 1 and is unique for the lifetime of the control file. If you restore a backup control file, then duplicate values can result. Also, CREATE CONTROLFILE initializes the counter back to 1.
%t Specifies the backup set time stamp, which is a 4-byte value derived as the number of seconds elapsed since a fixed reference time. You can use a combination of %s and %t to form a unique name for the backup set.
%T Specifies the year, month, and day in the Gregorian calendar in this format: YYYYMMDD.
%u Specifies an 8-character name constituted by compressed representations of the backup set or image copy number and the time the backup set or image copy was created.
%U

Specifies a system-generated unique file name (default).

The meaning of %U is different for image copies and backup pieces. For a backup piece, %U specifies a convenient shorthand for %u_%p_%c that guarantees uniqueness in generated backup file names. For an image copy of a data file, %U means the following:

data-D-%d_id-%I_TS-%N_FNO-%f_%u

For an image copy of an archived redo log, %U means the following:

arch-D_%d-id-%I_S-%e_T-%h_A-%a_%u

For an image copy of a control file, %U means the following:

cf-D_%d-id-%I_%u

%Y Specifies the year in this format: YYYY.
%% Specifies the percent (%) character. For example, %%Y translates to the string %Y.
  1. FILESPERSET integer

設定備份集包含備份的資料檔案個數

Specifies the maximum number of input files to include in each output backup set. This parameter is only relevant when BACKUP generates backup sets.

When the number of files in each backupSpec exceeds the FILESPERSET setting, then RMAN splits the files into multiple backup sets accordingly. The default value for FILESPERSET is 64.

  1. CHANNEL channel_id

Specifies the case-sensitive name of a channel to use when creating backups. The database uses the channel ID to report I/O errors. If you do not set this parameter, then RMAN dynamically assigns the backup sets to any available channels during execution.

RUN{

  ALLOCATE CHANNEL ch1 DEVICE TYPE sbt PARMS 'ENV=(OB_DEVICE_1=stape1)';

  ALLOCATE CHANNEL ch2 DEVICE TYPE sbt PARMS 'ENV=(OB_DEVICE_1=stape2)';

  BACKUP (DATABASE CHANNEL ch1)

(ARCHIVELOG ALL CHANNEL ch2);  }

  1. DELETE [ALL] INPUT

在對歸檔、鏡像及備份集進行備份時使用,成功備份後會删除這些檔案

Deletes the input files after successfully backing them up.

Specify this option only when backing up archived redo log files, data file copies (COPY OF or DATAFILECOPY), or backup sets. The BACKUP ARCHIVELOG command only backs up one copy of each distinct log sequence number, so if the DELETE INPUT option is used without the ALL keyword, RMAN only deletes the copy of the file that it backs up.

When backing up archived redo log files, RMAN uses the configured settings to determine whether an archived redo log can be deleted (CONFIGURE ARCHIVELOG DELETION POLICY TO BACKED UP).

ALL選項隻用于歸檔,指删除備份時所用的歸檔還是删除所有路徑下的歸檔

The ALL option applies only to archived redo log files. With DELETE ALL INPUT, RMAN deletes each backed-up archived redo log file from all log archiving destinations.

For the following procedure, assume that you archive to /arc_dest1, /arc_dest2, and the fast recovery area.Assume that you run the following BACKUP command:

BACKUP DEVICE TYPE sbt ARCHIVELOG ALL DELETE ALL INPUT;

In this case, RMAN backs up only one copy of each log sequence number in these archiving locations. RMAN deletes all copies of any log that it backed up from both the fast recovery area and the other archiving destinations.

If you specify DELETE INPUT rather than DELETE ALL INPUT, then RMAN only deletes the specific archived redo log files that it backed up. For example, RMAN deletes the logs in /arc_dest1 if these files were used as the source of the backup, but leave the contents of the /arc_dest2 intact.

歸檔日志删除首先會滿足archived redo log deletion policy

The BACKUP ... DELETE INPUT and DELETE ARCHIVELOG commands obey the archived redo log deletion policy for logs in all archiving locations. For example, if you specify that logs be deleted only when backed up at least twice to tape, then BACKUP ... DELETE honors this policy.

Note: You do not need to specify BACKUP DELETE INPUT when backing up the recovery area because the database automatically deletes log files based on the archived redo log deletion policy and other fast recovery area rules.

  1. MAXSETSIZE: Specifies a maximum size for a backup set. 超過會報錯

Note: This option results in an error message if used with BACKUP AS COPY. If you run BACKUP AS COPY on a channel that has MAXSETSIZE set, then MAXSETSIZE is silently ignored.

  1. SKIP [OFFLINE, READONLY, INACCESSIBLE]

SKIP INACCESSIBLE:Data files that RMAN cannot read.

SKIP OFFLINE:Offline data files. Some offline data files can still be read because they exist on disk. Others have been deleted or moved and so cannot be read, making them inaccessible.

SKIP READONLY:Data files in read-only tablespaces.

  1. COPIES integer

BACKUP AS BACKUPSET DEVICE TYPE DISK COPIES 3 INCREMENTAL LEVEL 0 DATABASE;

備援備份隻能用于BACKUPSET, 是在相同備份集下生成備援備份片而不是産生備援備份集。備援最多4份,且隻能備援到同一媒體

A copy of a duplexed backup set is a copy of each backup piece in the backup set, with each copy getting a unique copy number (for example, 0tcm8u2s_1_1 and 0tcm8u2s_1_2). It is not possible to duplex backup sets to the fast recovery area.

RMAN can make up to four copies of a backup set simultaneously, each an exact duplicate of the others.

You can use BACKUP ... COPIES or CONFIGURE ... BACKUP COPIES to duplex backup sets. RMAN can duplex backups to either disk or tape, but cannot duplex backups to tape and disk simultaneously.

Duplexing applies only to backup sets, not image copies.

  1. KEEP {FOREVER|UNTIL TIME 'date_expt'} [RESOTRE POINT rename]

可以在BACKUP中使用KEEP來豁免retention policy的限制

You can exempt a backup from the retention policy by using the KEEP option with the BACKUP command. You can also use the KEEP and NOKEEP options of the CHANGE command to change the status of an existing backup.

使用KEEP的BACKUP會有所不同

  1. It automatically backs up the data files, control file (even if the control file autobackup is disabled), and the server parameter file.
  2. It automatically generates an archived redo log backup to ensure that the database backup can be recovered to a consistent state.

The log backup contains just those archived logs needed to restore this backup to a consistent state. The database performs an online redo log switch to archive the redo that is in the current online logs and is necessary to make this new backup consistent.

  1. If the FORMAT, POOL, or TAG parameters are specified, then they are used for all backups. For this reason, the FORMAT string must allow for the creation of multiple backup pieces. Specifying the %U substitution variable is the easiest way to meet this requirement.
  2. It supports an optional RESTORE POINT clause that creates a normal restore point, which is a label for an SCN to which the backup must be recovered to be made consistent. The SCN is captured just after the data file backups complete. RMAN resynchronizes restore points with the recovery catalog and maintains the restore points while the backup exists.

指定的一般還原點名不能已存在

KEEP FOREVER需要使用catalog而其它KEEP選項不需要

A recovery catalog is required for KEEP FOREVER, but is not required for any other KEEP option.

示例

BACKUP DATABASE TAG quarterly KEEP FOREVER RESTORE POINT FY06Q4;

BACKUP DATABASE TAG quarterly KEEP UNTIL TIME 'SYSDATE+365' RESTORE POINT FY06Q4;

RMAN> list restore point all;

2. BACKUP Examples

較全的BACKUP語句:

BACKUP INCREMENTAL LEVEL 1 CUMULATIVE AS COMPRESSED BACKUPSET DATABASE INCLUDE CURRENT CONTROLFILE PLUS ARCHIVELOG ALL DELETE ALL INPUT SKIP INACCESSIBLE TAG='full_backup' FORMAT '/oracle/full20181111';

BACKUP AS COPY TAGE '071117_imagebackup' DATABASE;

--不能以映像檔案備份SPFILE, 會把SPFIE備份成BACKUPSET

BACKUP AS COMPRESSED BACKUPSET DATABASE;  compressed隻用于backupset

BACKUP DATABASE;    備份資料檔案、控制檔案、SPFILE,不會備份歸檔

BACKUP DATAFILE 5;

BACKUP TABLESPACE USERS;

Backup tablespace實質是轉為backup datafile操作

You can back up one or more tablespaces with the BACKUP TABLESPACE command or one or more data files with the BACKUP DATAFILE command.When you specify tablespaces, RMAN translates the tablespace name internally into a list of data files.

BACKUP DEVICE TYPE sbt SPFILE;

使用BACKUP ARCHIVELOG或BACKUP ... PLUS ARCHIVELOG指令備份歸檔

BACKUP ARCHIVELOG ALL DELETE [ALL] INPUT;

BACKUP DATABASE PLUS ARCHIVELOG;

BACKUP ARCHIVELOG FROM SEQUENCE 121 UNTIL SEQUENCE 125;

BACKUP ARCHIVELOG FROM TIME  'SYSDATE-30' UNTIL TIME 'SYSDATE-7';

BACKUP ARCHIVELOG ALL NOT BACKED UP 2 TIMES;  

在預設備份裝置上沒有2份備份的歸檔,這裡裝置隻能DISK或SBT一種:To determine the number of backups for a file, RMAN only considers backups created on the same device type as the current backup.

BACKUP AS COPY CURRENT CONTROLFILE FORMAT '/tmp/control01.ctl';

BACKUP DEVICE TYPE SBT CONTROLFILECOPY '/tmp/control01.ctl'; --備份之前的鏡像到sbt

備份控制檔案使用INCLUDE CURRENT CONTROLFILE或BACKUP CURRENT CONTROLFILE,可以備份為backup set或image copy, 前者會在備份前建立一緻性快照而後者不會. 如果手動備份控制檔案時同時開啟了控制檔案的自動備份則會備份兩份

To make a manual backup, you can either specify INCLUDE CURRENT CONTROLFILE when backing up other files or specify BACKUP CURRENT CONTROLFILE.

You can make a manual backup of the current control file either as a backup set or as an image copy.

For a backup set, RMAN first creates a snapshot control file for read consistency. A snapshot control file is not needed for an image copy.

BACKUP AS BACKUPSET (DATAFILE 3, 4, 5, 6, 7) (DATAFILE 8, 9);

BACKUP AS BACKUPSET DATAFILE 3, 4, 5, 6, 7, 8, 9;

BACKUP AS BACKUPSET DATAFILE 3, ... 72;

BACKUP FULL DATABASE;

BACKUP INCREMENTAL LEVEL 0 AS COMPRESSED BACKUPSET DATABASE;

BACKUP INCREMENTAL LEVEL 0 AS COPY DATABASE;

BACKUP INCREMENTAL LEVEL 1 AS COMPRESSED BACKUPSET DATABASE;

BACKUP INCREMENTAL LEVEL 1 CUMULATIVE AS COMPRESSED BACKUPSET DATABASE;

在執行1級備份時如果未發現有0級備份,則此1級備份将進行全備隻是LEVEL為1

0級備份可以為BACKUPSET或COPY

增量備份不能以FULL作0級備份

BACKUP DATABASE SECTION SIZE 10M;      --将備份datafile分多個section,提高并行度

ALLOCATE CHANNEL c1 DEVICE TYPE DISK MAXPIECESIZE 300M;   

--備份片的最大大小,超過将再産生新的備份片,隻能在channel中設定

BACKUP DATABASE MAXSETSIZE  2M ;        --每個備份集最大大小,超過将報錯

BACKUP DATABASE FILEPERSET 2;    --每個備份集包含2個要備份的檔案

BACKUP COPIES 2 DATAFILE 4 FORMAT '/u01/1','/u01/2';

備份BACKUPSET為BACKUPSET

BACKUP AS BACKUPSET BACKUPSET 12;

BACKUP DEVICE TYPE SBT BACKUPSET ALL DELETE INPUT; 将備份轉到錄音帶

BACKUP DEVICE TYPE SBT BACKUPSET COMPLETED BEFORE 'SYSDATE-7' DELETE INPUT;

BACKUP BACKUPSET備份BACKUPSET, 此指令不建立新的備份集而是在原來備份集下增加原備份片的複制。對于基于備援的retention policy,效果為不增加此備份備援(仍算1份)

You can use the BACKUP BACKUPSET command to back up backup sets produced by other backup jobs. You can also use BACKUP RECOVERY AREA to back up recovery files created in the current and all previous fast recovery area destinations.

The BACKUP BACKUPSET command creates additional copies of backup pieces in a backup set, but does not create a new backup set. Thus, BACKUP BACKUPSET is similar to using the DUPLEX or MAXCOPIES option of BACKUP. The extra copy of a backup set created by BACKUP BACKUPSET is not a new backup set, just as copies of a backup set produced by other forms of the BACKUP command are not separate backup sets.

For a backup retention policy based on redundancy, a backup set is counted as one instance of a backup. This statement is true even if there are multiple copies of the backup pieces that form the backup set, such as when a backup set has been backed up from disk to tape.

DELETE INPUT here is equivalent to DELETE ALL INPUT: RMAN deletes all existing copies of the backup set. If you duplexed a backup to four locations, then RMAN deletes all four copies of the pieces in the backup set.

備份鏡像檔案為BACKUPSET或IMAGE COPY

BACKUP DATAFILECOPY FROM TAG DBCopy;   --備份tag為dbcopy的image copy

BACKUP DEVICE TYPE sbt COPY OF DATABASE DELETE INPUT;  

--将最新的資料庫image copy轉移到錄音帶

BACKUP AS COPY COPY OF DATABASE;   --對資料庫鏡像備份進行鏡像備份  

BACKUP AS BACKUPSET COPY OF DATABASE;  --對資料庫鏡像備份進行備份集備份 

BACKUP COPY OF或BACKUP DATAFILECOPE備份資料檔案,控制檔案及歸檔日志的image copy,可轉備份為backupset

You can also use the BACKUP COPY OF command to back up image copies of data files, control files, and archived redo logs. The output of this command can be either backup sets or image copies, so you can generate backup sets from image copies. This form of backup is used to back up a database backup created as image copies on disk to tape. If there are multiple copies of a data file, then the latest one is used.

Note: In a multitenant environment, you cannot back up backup sets and image copies of PDBs that have been dropped. RMAN skips backing up these backups.

BACKUP RECOVERY AREA;

BACKUP RECOVERY AREA備份RECOVERY AREA中recovery files(full and incremental backup sets, control file autobackups, data file copies, and archived redo logs), 如果備份到DISK需要使用TO DESTINATION指定位置

Recovery files are full and incremental backup sets, control file autobackups, data file copies, and archived redo logs. SBT and disk backups are supported for BACKUP RECOVERY AREA. For disk backups of the recovery files, you must use the TO DESTINATION option.

重新開機備份任務

BACKUP NOT BACKED UP SINCE TIME 'SYSDATE-14' DATABASE PLUS ARCHIVELOG;

在備份時出現任何I/O問題都會中止備份

Any I/O errors that RMAN encounters when reading files or writing to the backup pieces or image copies cause RMAN to terminate the backup job in progress. For example, if RMAN tries to back up a data file but the data file is not on disk, then RMAN terminates the backup. If multiple channels are being used or redundant copies of backups are being created, however, then RMAN may be able to continue the backup without user intervention.

通過SINCE TIME可重新啟動未完成備份而不用重新備份,最小可重新開機備份機關是DATAFILE,但如果某個備份片包含多個DATAFILE,則将重新備份此備份片

The minimum unit of restartability is a data file. However, if a backup set contains one backup piece, and if this piece contains blocks from multiple data files, then the unit of restartability is the backup piece. The unit of restartability for image copies is a data file.

You can restart a backup by specifying the SINCE TIME clause on the BACKUP command.

If the SINCE TIME is later than the completion time, then RMAN backs up the file. If you use BACKUP DATABASE NOT BACKED UP without the SINCE TIME parameter, then RMAN only backs up files that have never been backed up.

裝置備份時間視窗

BACKUP DURATION 4:00 TABLESPACE users;   --4小時内完成

BACKUP DURATION 4:00 PARTIAL TABLESPACE users FILESPERSET 1;

BACKUP DURATION 4:00 PARTIAL MINIMIZE TIME DATABASE FILESPERSET 1;--盡快完成

BACKUP DURATION 4:00 MINIMIZE LOAD DATABASE FILESPERSET 1; --會用盡設定時間

In this example, RMAN monitors the progress of the running backup, and periodically estimates how long the backup takes to complete at its present rate. If RMAN estimates that the backup will finish before the end of the backup window, then it slows down the rate of backup so that the full available duration is used. This reduces the overhead on the database associated with the backup.

A backup window is a period of time during which a backup must complete.

RMAN預設優先備份最近最少備份的檔案

設定備份視窗并不會增加備份速度,RMAN預設就是全力備份

RMAN backs up the least recently backed up files first. By default, RMAN backs up the files at the maximum possible speed. Specifying a window does not mean that RMAN backs up data faster than normal to ensure that the backup completes before the window ends.

如果備份在指定視窗未完成并報錯,已備份完成的BACKUPSET可以使用,未完成的BACKUPSET将被自動丢棄(以BACKUPSET為機關)

如果BACKUP DURATION是在run塊中,則之後的指令也不會執行

By default, if the backup is not complete within the DURATION time, then RMAN interrupts the backup and reports an error. If the BACKUP command is in a RUN command, then the RUN command terminates. Any completed backup sets are retained and can be used in restore operations, even if the entire backup is not complete. Thus, if you retry a job that was interrupted when the available duration expired, each successive attempt covers more of the files needing backup. Any incomplete backup sets are discarded.

當使用PARTIAL時逾時不會報錯,而是顯示還有哪些檔案未備份。如果在RUN塊中執行BACKUP之後指令仍然執行。建議同時設定FILESPERSET為1減少lost work

When you specify PARTIAL, RMAN does not report an error when a backup is interrupted because of the end of the backup window. Instead, RMAN displays a message showing which files are not backed up.

If the BACKUP command is part of a RUN block, then the remaining commands in the RUN block continue to execute.

If you specify FILESPERSET 1, then RMAN puts each file into its own backup set. When a backup is interrupted at the end of the backup window, only the backup of the file currently being backed up is lost. All backup sets completed during the window are saved, minimizing the lost work caused by the end of the backup window.

(八)RESTORE and RECOVER Command

1. RECOVER TEST

模拟recover, 在記憶體中模拟媒體恢複那樣應用redo log,但不寫入磁盤,且總是復原。

A simulated recovery initiated with the RECOVER ... TEST command in RMAN or SQL*Plus. A trial recovery applies redo in a way similar to normal media recovery, but it never writes its changes to disk and it always rolls back its changes. Trial recovery occurs only in memory.

Trial recovery lets you foresee what problems might occur if you were to continue with normal recovery. For problems caused by ongoing memory corruption, trial recovery and normal recovery can encounter different errors.

示例:

RECOVER DATABASE TEST

RECOVER DATABASE USING BACKUP CONTROLFILE UNTIL CANCEL TEST

RECOVER TABLESPACE users TEST

RECOVER DATABASE UNTIL CANCEL TEST

2. Incrementally Updating Backups

增量更新備份特性可以将image copy不斷更新,減少了備份成本及恢複時間

By incrementally updating backups, you can avoid the overhead of making full image copy backups of data files, while also minimizing time required for media recovery of your database.

To incrementally update data file backups:

  1. Create a full image copy backup of a data file with a specified tag.
  2. At regular intervals (such as daily), make a level 1 differential incremental backup of the data file and use the same tag as the base data file copy.
  3. Apply the incremental backup to the most recent backup with the same tag.

If you run RECOVER COPY daily without specifying an UNTIL TIME, then a continuously updated image copy cannot satisfy a recovery window of more than a day.(詳見下面示例說明) 

Basic Example

RUN

{

  RECOVER COPY OF DATABASE WITH TAG 'incr_update';

  BACKUP

    INCREMENTAL LEVEL 1

    FOR RECOVER OF COPY WITH TAG 'incr_update'

    DATABASE;

}

Table 9-2 shows the effect of the script when it is run once per day starting on Monday.

Command Monday Tuesday Wednesday Thursday Onward
RECOVER Because no incremental backup or data file copy exists, the command generates a message (but not an error). That is, the command has no effect. A database copy now exists, but no incremental level 1 backup exists with which to recover it. Thus, the RECOVER command has no effect. The level 1 incremental backup made on Tuesday is applied to the database copy, bringing the copy up to the checkpoint SCN of the level 1 incremental backup. The level 1 incremental backup made yesterday is applied to the database copy, bringing the copy up to the checkpoint SCN of the level 1 incremental backup.
BACKUP

No level 0 image copy exists, so the command creates an image copy of the database and applies the tag incr_update.

Note: If the script sets DEVICE TYPE sbt, then the first run creates the copy on disk, not on tape. Subsequent runs make level 1 backups on tape.

The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between Monday and Tuesday. The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between Tuesday and Wednesday. The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between now and the most recent backup with the tag incr_update.

After the third run of the script, the following files are available for a point-in-time recovery:

  1. An image copy of the database, as of the checkpoint SCN of the preceding run of the script, 24 hours earlier
  2. An incremental backup for the changes after the checkpoint SCN of the preceding run
  3. Archived redo logs including all changes between the checkpoint SCN of the image copy and the current time

此例子隻能保證recovery window為1天以内

If you must restore and recover your database during the following 24 hours, then you can restore the data files from the incrementally updated data file copies. You can then apply changes from the most recent incremental level 1 and the redo logs to reach the desired SCN. At most, you have 24 hours of redo to apply, which limits how long point-in-time recovery takes to finish.

Advanced Example

Example 9-16 shows how to maintain a window of 7 days by specifying the beginning time of your window of recoverability in the RECOVER command.

Example 9-16 Advanced Incremental Update Script

RUN

{

  RECOVER COPY OF DATABASE

    WITH TAG 'incr_update'

    UNTIL TIME 'SYSDATE - 7';

  BACKUP

    INCREMENTAL LEVEL 1

    FOR RECOVER OF COPY WITH TAG 'incr_update'

    DATABASE;

}

Table 9-3 shows the effect of the script when it is run once per day starting on Monday, January 1.

Command Monday 1/1 Tuesday 1/2 - Monday 1/8 Tuesday 1/9 Wednesday 1/10 Onward
RECOVER Because no incremental backup or data file copy exists, the command generates a message (but not an error). That is, the command has no effect. A database copy exists, but SYSDATE-7 specifies a time before the base copy was created. For example, on Wednesday SYSDATE-7 specifies the Wednesday before Monday 1/1. Thus, the RECOVER command has no effect. SYSDATE-7 now specifies a date after the base copy was created. The database copy made on Monday 1/1 is updated with the incremental backup made on Tuesday 1/2, bringing the copy up to the checkpoint SCN of the level 1 incremental backup. The database copy is updated with the incremental backup made 7 days ago, bringing the copy up to the checkpoint SCN of the level 1 incremental backup.
BACKUP

No level 0 image copy exists, so the command creates an image copy of the database and applies the tag incr_update. This copy is needed to begin the cycle of incremental updates.

Note: If the script sets DEVICE TYPE sbt, then the first run creates the copy on disk, not on tape. Subsequent runs make level 1 backups on tape.

The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between yesterday and today. The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between Monday 1/8 and Tuesday 1/9. The command makes an incremental level 1 backup and assigns it the tag incr_update. This backup contains blocks that changed between yesterday and today.

As with the basic script in Example 9-15, you have fast recoverability to any point in time between the SCN of the data file copies and the present. RMAN can use both block changes from the incremental backups and individual changes from the redo logs. Because you have the daily level 1 incremental backups, you never need to apply more than 1 day of redo.

(九)Maintaining RMAN Backups and Repository Records

Following are the RMAN maintenance commands:

  1. The CATALOG command enables you to add records about RMAN and user-managed backups that are currently not recorded in the RMAN repository, or to remove records for backups that are recorded.
  2. The CHANGE command enables you to update the status of records in the RMAN repository.
  3. The CROSSCHECK command enables you to synchronize the logical backup records with the physical reality of files in backup storage.
  4. The DELETE command enables you to delete backups from the operating system.
  1. Crosschecking the RMAN Repository

用于檢查RMAN repository中檔案是否确實存在于DISK或SBT, 不存在會辨別為EXPIRED,被标記為EXPIRED備份又存在了也會再标記回AVAILABLE。

CROSSCHECK并不删除repository records,使用DELETE EXPIRED來删除

在執行CROSSCHECK或DELETE時也可以指定多CHANNEL,它會像backup指令一樣并行進行查找或删除。注意如果多個檔案分布在磁盤及錄音帶時,要全局配置預設裝置為SBT或手動ALLOCATE SBT CHANNEL, 而DISK CHANNEL不配置預設也會有

You can configure or manually allocate multiple channels before issuing CROSSCHECK or DELETE commands. RMAN searches for each backup on all channels that have the same device type as the channel used to create the backup.

For example, assume that you have an SBT channel configured as follows:

CONFIGURE DEVICE TYPE sbt PARALLELISM 1;

CONFIGURE DEFAULT DEVICE TYPE sbt;

In this case you can run the following commands to crosscheck both disk and SBT:

CROSSCHECK BACKUP;

CROSSCHECK COPY;

這時其實使用了兩個channel:ORA_SBT_TAPE_1與ORA_DISK_1

allocated channel: ORA_SBT_TAPE_1

channel ORA_SBT_TAPE_1: sid=12 devtype=SBT_TAPE

channel ORA_SBT_TAPE_1: WARNING: Oracle Test Disk API

using channel ORA_DISK_1

crosschecked backup piece: found to be 'AVAILABLE'

...    

You do not have to manually allocate a disk channel because RMAN uses the preconfigured disk channel.

示例:

CROSSCHECK BACKUP;  

CROSSCHECK COPY OF DATABASE;

CROSSCHECK BACKUPSET 1338, 1339, 1340;

CROSSCHECK BACKUPPIECE TAG 'nightly_backup';

CROSSCHECK BACKUP OF ARCHIVELOG ALL SPFILE;

CROSSCHECK BACKUP OF DATAFILE "?/oradata/trgt/system01.dbf"

COMPLETED AFTER 'SYSDATE-14';

CROSSCHECK CONTROLFILECOPY '/tmp/control01.ctl';

CROSSCHECK DATAFILECOPY 113, 114, 115;

CROSSCHECK PROXY 789;

  1. Changing the Repository Status of Backups and Copies

RMAN provides multiple methods of changing the repository status of backups and copies.

檢視備份狀态可通過LIST指令或V$BACKUP_FILES或catalog視圖RC_DATAFILE_COPY 或RC_ARCHIVED_LOG

  1. AVAILABLE&UNAVAILABLE

RMAN does not use files with status UNAVAILABLE in RESTORE or RECOVER commands

CHANGE DATAFILECOPY '/tmp/control01.ctl' UNAVAILABLE;

CHANGE ARCHIVELOG LOGSE=n UNAVALIABLE;

CHANGE COPY OF ARCHIVELOG SEQUENCE BETWEEN 1000 AND 1012 UNAVAILABLE;

CHANGE BACKUPSET 12 UNAVAILABLE;

CHANGE BACKUP OF SPFILE TAG "TAG20120208T154556" UNAVAILABLE;

CHANGE DATAFILECOPY '/tmp/system01.dbf' AVAILABLE;

  1. KEEP&NOKEEP

You cannot set KEEP attributes for backup sets or files stored in the fast recovery area.

CHANGE BACKUPSET 231 NOKEEP;

CHANGE DATAFILECOPY '/tmp/system01.dbf' KEEP UNTIL TIME 'SYSDATE+180';

  1. Changing the Status of Backups for Dropped PDBs

After you drop a PDB, you cannot use the PDB name to change the status of backups associated with the dropped PDB. Instead, use the GUID to identify the dropped PDB.

  1. Connect to the root as a common user with the SYSDBA or SYSBACKUP privilege.
  2. Query the DBA_PDB_HISTORY view to determine the GUID of the PDB that was dropped.

SELECT pdb_name, pdb_guid FROM dba_pdb_history WHERE db_name = 'test_db';

  1. The following commands remove RMAN repository records of backup pieces and image copies associated with a dropped PDB that is identified using its GUID.

CHANGE BACKUPPIECE GUID 'DFCE8C3A437F214EB4230070EC0D294E' UNCATALOG;

CHANGE COPY GUID 'DFCE8C3A437F214EB4230070EC0D294E' UNCATALOG;

  1. Adding/Removing  Backup Records to the RMAN Repository

Whenever you make a user-managed copy, for example, by using the UNIX cp command to copy a data file, be sure to catalog it. When making user-managed copies, you can use the ALTER TABLESPACE...BEGIN/END BACKUP statement to make data file copies off an online tablespace. Although RMAN does not create such data file copies, you can use the CATALOG command to add them to the recovery catalog so that RMAN is aware of them.

CATALOG DATAFILECOPY '/tmp/users01.dbf';    --image copy

CATALOG BACKUPPIECE '/disk2/09dtq55d_1_2', '/disk2/0bdtqdou_1_1';  --backup piece

CATALOG RECOVERY AREA;

CATALOG START WITH '+disk';

CATALOG START WITH '/fs1/datafiles/';   

Note: CATALOG START WITH

此指令指定的字元串為目錄字首而不是目錄本身,一定在結束加個目錄辨別/

If the command can catalog a file, then it does so. If it cannot catalog the file, then it makes its best guess about the contents of the skipped file.

RMAN scans all paths for all files on disk that begin with the specified prefix. The prefix is not just a directory name. Using the wrong prefix can cause the cataloging of the wrong set of files.

For example, assume that a group of directories /disk1/backups, /disk1/backups-year2003, /disk1/backupsets, /disk1/backupsets/test and so on, all contain backup files. The following command catalogs all files in all of these directories, because /disk1/backups is a prefix for the paths for all of these directories:

CATALOG START WITH '/disk1/backups';

To catalog only backups in the /disk1/backups directory, the correct command is as follows:

CATALOG START WITH '/disk1/backups/';

CHANGE...UNCATALOG會将record狀态辨別為DELETED,RMAN Repository是否使用controlfile控制關系到是否删除RMAN repository記錄,使用控制檔案不會删除記錄,而使用catalog會删除,DELETE指令也相同性質,但此指令不同于DELETE它不删除實體檔案

Because of the way that control file data is stored, RMAN cannot remove the record from the control file, only update it to DELETED status. Because the recovery catalog tables are ordinary database tables, however, RMAN deletes rows from them in the same way that rows are deleted from any table.

EXAMPLE:

CHANGE CONTROLFILECOPY '/tmp/control01.ctl' UNCATALOG;

CHANGE DATAFILECOPY '/tmp/system01.dbf' UNCATALOG;

(十)Dropping a Database

To remove a database from the operating system, you can use the DROP DATABASE command in RMAN. RMAN removes the server parameter file, all data files, online redo logs, and control files belonging to the target database.

DROP DATABASE requires that RMAN be connected to the target database, and that the target database be mounted. The command does not require connection to the recovery catalog. If RMAN is connected to the recovery catalog, and if you specify the option INCLUDE COPIES AND BACKUPS, then RMAN also unregisters the database.

RMAN> DELETE BACKUPSET; # deletes all backups

RMAN> DELETE COPY; # deletes all image copies (including archived logs)

RMAN> DROP DATABASE;

(十一)OTHERS

  1. FLASHBACK DATABASE

RMAN中可使用FLASHBACK DATABASE

  1. STARTUP and SHUTDOWN

同SQLPLUS,用于啟動或關閉TARGET DATABASE,不能對CATALOG DATABASE操作

STARTUP DBA|FORCE|MOUNT|NOMOUNT|PFILE filename;

SHUTDOWN ABORT|IMMEDIATE|NORMAL|TRANSACTIONAL;

NOTE:

DBA:Restricts access to users with the RESTRICTED SESSION privilege.

FORCE:If the database is open, then FORCE shuts down the database with a SHUTDOWN ABORT statement before re-opening it. If the database is closed, then FORCE opens the database.

NOMOUNT: If no parameter file exists, then RMAN starts the instance with a temporary parameter file. You can then run RESTORE SPFILE to restore a backup server parameter file.

  1. SPOOL LOG

SPOOL LOG TO filename [APPEND];

SPOOL LOG OFF;

  1. SQL

11.2及之前在RMAN中執行SQL需要把SQL用單引号引起并在之前用SQL指令,現在已不用

Use the SQL command to execute SQL commands and PL/SQL procedures. This command is easier to use than in Oracle Database 11.2 and earlier releases, because the SQL command does not need to be enclosed in quotation marks and does not need to be prefixed with "SQL".

11.2及之前文法:SQL [CHANNEL channel_id] 'command'; 另外還不能使用SELECT語句

11.2及之後文法:[SQL] [CATALOG| CHANNEL channel_id| TARGET] command;

SQL CATALOG:Executes the SQL command in the catalog database.

SQL CHANNEL 'channel_id':Executes the SQL command over the named channel.

SQL TARGET:Executes the SQL command in the target database.

以下指令要特别注意:

  1. DELETE FROM

删除表記錄要加from,否則會認為是RMAN DELETE指令,也可以在delete前加SQL差別

Requires the FROM keyword to execute the SQL DELETE command; otherwise, executes the RMAN DELETE command.

  1. DROP DATABASE

執行的是RMAN DROP DATABASE command. 是以要在指令前前加SQL差別

  1. FLASHBACK

執行的是RMAN FLASHBACK DATABASE command. 是以要在指令前前加SQL差別