这一章节与oracle10g,11g无任何区别,仅转载,不做实践
9.4 Backing Up Your Database
节描述如何使用Oracle Recovery Manager (RMAN)备份数据库。Oracle建议的磁盘备份策略提供了数据库的有效每日备份。这种策略使您能够在过去24小时内的任何时候快速地将数据库返回到其状态
. For more flexible backup options, see Oracle Database Backup and Recovery User's Guide.
This section contains the following topics:
See Also:
"Database Backup and Recovery Concepts"
9.4.1 Additional Backup Concepts
This section includes topics about incrementally updated backups and backup tags.
9.4.1.1 Incrementally Updated Backups: Rolling Forward Image Copies of Data Files
Oracle Recovery Manager (RMAN) enables you to apply level 1 incremental backups to an older image copy of your data files. You can roll forward the copy to the point in time of the most recent level 1 incremental backup. All blocks changed since the image copy was created are overwritten with their new contents as of the time of the last level 1 incremental backup. The effect is to roll forward the file in time, so that its contents are equivalent, for the purposes of database recovery, to an image copy of the data file made at the time of the last incremental level 1 backup.
Incorporating incrementally updated backups into your backup strategy shortens expected recovery times. Media recovery to the present time or to a point in time in the recent past can begin at the time of the last level 1 backup applied, rather than at the time of the last full database backup.
See Also:
"Using the Oracle Suggested Backup Strategy"
9.4.1.2 Backup Tags
A tag is a text string that identifies a backup, either uniquely or as part of a group of backups. All Oracle Recovery Manager (RMAN) backups, including incremental backups, are labeled with a tag. For example, if you performed a full database backup every Saturday, then you could use the tag FULL_SAT
to identify this backup.
You can use tags to refer to specific backups in RMAN commands. For example, you could issue a command to move the latest FULL_SAT
backup to tape. If you do not specify a tag, then RMAN creates a unique tag automatically.
Because you can use tags to refer to different groups of backups, you can create different routines in your backup strategy that do not interfere with each other. When you schedule a backup job and give the job a name, the job name is the tag.
9.4.2 Performing and Scheduling Backups Using RMAN
Oracle Recovery Manager (RMAN) enables you to perform the different types of backups that are required by your backup strategy. This section discusses creating a whole database backup.
You can also individually back up data files, control files, and archived redo log files. You can use some advanced RMAN features such as encrypting backups. For more information about these topics, see Oracle Database Backup and Recovery User’s Guide.
See Also:
Oracle Database Backup and Recovery User’s Guide for information about backing up multitenant conainer databases (CDBs) and pluggable databases (PDBs)
This section contains the following topics:
9.4.2.1 Performing a Whole Database Backup
Whole backups of a database include the complete contents of all data files of the database, plus the control file, archived redo log files, and server parameter file. With these files, you can perform a complete recovery.
While whole database backups can be an important element in your overall backup strategy, they are also a required step in some situations, such as when you enable or disable ARCHIVELOG
mode (see "Configuring Recovery Settings"). This section explains how to make whole database backups, both offline and online, to disk. Typically, you want to perform online backups to maximize database availability.
To perform a whole database backup when the database is open:
-
Connect RMAN to the target database as described in "Connecting to the Target Database Using RMAN."
-
Ensure that your database is in
ARCHIVELOG
mode as described in "Enabling Archiving of Redo Log Files."You can make online backups only if your database is running in
ARCHIVELOG
mode. -
Back up the database, along with archived redo logs by using the following command:
BACKUP DATABASE PLUS ARCHIVELOG;
This backup is created on the default device that you configured for storing backups. If you did not configure a default device, then the backup is created in the fast recovery area. RMAN uses a default format while naming the backup sets that comprise the backup.
To perform a whole database backup when the database is closed:
- Connect RMAN to the target database as described in "Connecting to the Target Database Using RMAN."
- Shut down the database and then mount the database using the following commands.
SHUTDOWN IMMEDIATE; STARTUP MOUNT;
- Back up the database using the following command:
BACKUP DATABASE;
While backing up a database that is closed, you need not back up the archived log files because the database is in a consistent state at the time of backup. Therefore, no media recovery is required if you restore the database from this backup.
- Open the database after the backup is complete.
ALTER DATABASE OPEN;
See Also:
-
Oracle Database Backup and Recovery User’s Guide for information about performing backups of multitenant container databases (CDBs) and pluggable databases (PDBs)
9.4.2.2 Using the Oracle Suggested Backup Strategy
The Oracle suggested backup strategy is a scheduled disk backup strategy that protects your data and provides efficient recoverability to any point in the user-specified recovery window (time period). It leverages the incrementally updated backup features to provide faster backups than whole database backups, and faster recoverability than is possible by applying multiple incremental backups to the last full backup.
After reading the introductory "About the Oracle Suggested Backup Strategy" topic, complete the following tasks described in this section to schedule a daily backup that implements the Oracle suggested backup strategy:
See Also:
"Incrementally Updated Backups: Rolling Forward Image Copies of Data Files"
9.4.2.2.1 About the Oracle Suggested Backup Strategy
The Oracle suggested backup strategy is based on incrementally updated backups. This strategy starts with an image copy of each data file and then rolls forward the image copies each day by applying an incremental level 1 backup.
For each data file, the strategy calls for backups as follows:
-
At the beginning of day 1 of the strategy (the time the first scheduled job actually runs), Oracle Recovery Manager (RMAN) creates an incremental level 0 image copy. It contains the data file contents at the beginning of day 1.
If a recovery is required, then the archived redo log files from day 1 can be used to recover to any point during day 1.
-
At the beginning of day 2, RMAN creates a differential incremental level 1 backup that contains the blocks changed during day 1.
If a recovery is required, then RMAN can apply this incremental level 1 to roll forward the level 0 backup to the beginning of day 2. RMAN can use archived redo log files to recover to any point during day 2.
-
At the beginning of each day n for day 3 and onward, RMAN applies the level 1 backup from the beginning of day n-1 to the level 0 backup. This action brings the data file copy to its state at the beginning of day n-1. Then, RMAN creates a new level 1 backup that contains the blocks changed during day n-1.
If a recovery is required, then RMAN can apply this incremental level 1 backup to the data file rolled forward on day n-1 to the beginning of day n. RMAN can use archived redo log files to recover the database to any point during day n.
In this Oracle suggested backup strategy, the data file image copies and the level 1 incremental backups share the same tag. You can safely implement other backup strategies without interfering with the Oracle suggested backup strategy.
Oracle suggested backup strategies also use tape backups in addition to disk backups, but these are beyond the scope of this section.
9.4.2.2.2 Task 1 - Preparing to Use the Oracle Suggested Backup Strategy
To use the Oracle suggested backup strategy, ensure that:
-
The database is in
ARCHIVELOG
mode. -
The fast recovery area size is configured, or a default device for storing backups is configured.
-
You have added a database host user to the OSBACKUPDBA operating system group, for operating system authentication.
See Also:
9.4.2.2.3 Task 2 - Creating the Backup Script—UNIX and Linux
This backup script implements the Oracle suggested backup strategy, enabling quick recovery to any time in the preceding 24 hours. This script can be used to back up a non-CDB or a whole multitenant container database (CDB).
To create the backup script for UNIX and Linux:
-
Start a text editor and create and save a file with the following contents. Save the file in a directory that is accessible to the Oracle Database software and on which the Oracle software owner has the read permission.
Note:
In the following script, substitute the correct values for your installation for the
ORACLE_HOME
andORACLE_SID
environment variables.#!/bin/sh export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 export ORACLE_SID=orcl PATH=$ORACLE_HOME/bin:$PATH rman <<EOF connect target / RUN { ALLOCATE CHANNEL disk_iub DEVICE TYPE DISK; RECOVER COPY OF DATABASE WITH TAG daily_iub; BACKUP INCREMENTAL LEVEL 1 FOR RECOVER OF COPY WITH TAG daily_iub DATABASE; } exit EOF
9.4.2.2.4 Task 3 - Testing the Backup Script
It is recommended that you run the script manually, to check for errors, before scheduling it. Your manual run of the script will start day one of the strategy, creating an incremental level 0 image copy of all datafiles.
To test the backup script:
- Log in to the database host as a user who is a member of the OSBACKUPDBA operating system group (typically the
backupdba
group). - In a command window, enter the following command:
full-script-path
where
full-script-path
is the full path and file name of the script you created in Task 2.For example, if your script is in the file /u01/app/oracle/rman/daily_backup.sh, then enter this command:
/u01/app/oracle/rman/daily_backup.sh
The script starts Oracle Recovery Manager (RMAN), which starts the backup. The output from RMAN includes warning messages similar to the following:
... no copy of datafile 1 found to recover no copy of datafile 2 found to recover ... no parent backup or copy of datafile 1 found no parent backup or copy of datafile 2 found ...
These messages are normal for the first run of the script.
Note:
For the second run of the script, the output includes only these warning messages:
no copy of datafile 1 found to recover no copy of datafile 2 found to recover ...
Again, these messages are normal. For the third and subsequent script runs, no further warning messages are output.
9.4.2.2.5 Task 4 - Scheduling the Daily Backup—UNIX and Linux
The following procedure uses the cron
utility to schedule daily database backups at 2:00 a.m.
To schedule the Oracle-suggested disk backup strategy:
- Ensure that you are logged in to the database host as a user who is a member of the OSBACKUPDBA operating system group (typically the
backupdba
group).The
cron
job will run as this host user. - Start a text editor, and create and save a file with the following contents into your home directory. Name the file .crontab. (Note the period at the front of the file name.)
[email protected] # MI HH DD MM DAY CMD 00 2 * * * full-script-path
where
full-script-path
is the full path and file name of the script you created in Task 2.For example, if the script is in the file /u01/app/oracle/rman/daily_backup.sh, then the .crontab file must contain:
[email protected] # MI HH DD MM DAY CMD 00 2 * * * /u01/app/oracle/rman/daily_backup.sh
Note:
Supply the desired e-mail address in the
MAILTO
line. This line is optional. Content written to stdout by thecron
job is e-mailed to this address at the completion of the job. - In a command window, change directory to your home directory and enter the following command:
crontab .crontab
This creates a crontab file for this user from the contents of .crontab.
Caution:
The existing crontab file for this user is overwritten. If you want to preserve the contents of this file and add this new job, use this command, which enables you to edit the existing file:
crontab -e
- (Optional) Check the contents of the crontab file for this user with the following command:
crontab -l [email protected] # MI HH DD MM DAY CMD 00 2 * * * /u01/app/oracle/rman/daily_backup.sh
See Also:
Your operating system documentation for a description of the crontab command and crontab files
9.4.2.3 About the Oracle Suggested Backup Strategy and Retention
When using the Oracle suggested backup strategy, the retention is dictated by the recovery and not by the configured retention. In order to get retention beyond 24 hours, you must change the RECOVER statement to something like:
RECOVER COPY OF DATABASE WITH TAG 'ORA_OEM_LEVEL_0' UNTIL TIME "SYSDATE-4";
The configured retention is not honored for either the retention or obsolete settings. So when using the Oracle suggested backup strategy, Oracle recommends that the default setting remains unchanged to avoid confusion:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
9.4.2.4 Scheduling Miscellaneous Backup Tasks
In addition to implementing the Oracle suggested backup strategy described in "Using the Oracle Suggested Backup Strategy," you can use customized backup strategies that back up certain parts of your database. Customized strategies include backing up selected tablespaces, datafiles, and archived redo logs. Create a script that contains the commands required to implement your customized backup task and then schedule this backup task using the cron
utility.
See Also:
Oracle Database Backup and Recovery User’s Guide for information about performing customized backups
9.4.3 Displaying Backups Stored in the RMAN Repository
Use the LIST
command to view information about backups stored in the Oracle Recovery Manager (RMAN) repository. The information includes backups of data files, individual tablespaces, archived redo log files, and control files. You can also use this command to display information about expired and obsolete backups.
See Also:
Oracle Database Backup and Recovery User’s Guide for more information about the LIST
command
The syntax used to display backups of multitenant container databases (CDBs) and pluggable databases (PDBs), which has minor variations from that used for non-CDBs, is described in Oracle Database Backup and Recovery User’s Guide.
To display all backups:
-
Connect RMAN to the target database as described in "Connecting to the Target Database Using RMAN."
-
Use the
LIST
command to display a summary of all the backups, both backup sets and image copies.LIST BACKUP SUMMARY; List of Backups =============== Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag ------- -- -- - ----------- --------------- ------- ------- ---------- --- 12 B F A DISK 28-MAR-12 1 1 NO TAG20120328T051810 13 B F A DISK 28-MAR-12 1 1 NO TAG20120328T051811 14 B F A DISK 28-MAR-12 1 1 NO TAG20120328T051921 15 B F A DISK 28-MAR-12 1 1 NO TAG20120328T051936 16 B F A DISK 28-MAR-12 1 1 NO TAG20120328T052241
To display selected backups:
- Connect RMAN to the target database as described in "Connecting to the Target Database Using RMAN."
- Use the
LIST BACKUP
orLIST COPY
command to display the specified backups, both backup sets and image copies.-
For example, to list the backups of a particular datafile:
LIST BACKUP OF DATAFILE 3; LIST COPY OF DATAFILE '/orcl/oradata/trgt/system01.dbf';
-
To display backups sorted by the type of database file:
LIST BACKUP BY FILE; List of Datafile Backups ======================== File Key TY LV S Ckp SCN Ckp Time #Pieces #Copies Compressed Tag ---- ------- - -- - ---------- --------- ------- ------- ---------- --- 1 14 B F A 723546 28-MAR-12 1 1 NO TAG20120328T051921 2 14 B F A 723546 28-MAR-12 1 1 NO TAG20120328T051921 3 14 B F A 723546 28-MAR-12 1 1 NO TAG20120328T051921 4 14 B F A 723546 28-MAR-12 1 1 NO TAG20120328T051921 5 14 B F A 723546 28-MAR-12 1 1 NO TAG20120328T051921 List of Control File Backups =========================== CF Ckp SCN Ckp Time BS Key S #Pieces #Copies Compressed Tag ---------- --------- ------- - ------- ------- ---------- --- 723835 28-MAR-12 16 A 1 1 NO TAG20120328T052241 723557 28-MAR-12 15 A 1 1 NO TAG20120328T051936 723490 28-MAR-12 13 A 1 1 NO TAG20120328T051811
-
9.4.4 Validating Backups and Testing Your Backup Strategy
As part of your backup strategy, you should periodically check whether your backups are intact and can be used to meet your recoverability objectives. You can validate your backups in the following ways:
You can validate your backups in the following ways:
-
Select specific backup sets or image copies in Oracle Recovery Manager (RMAN) and validate them. This technique indicates if a backup exists and can be restored. For this form of validation, use the steps described in "Validating Selected Backups."
-
Specify database files and let RMAN select backups to use when restoring those files, as it would for an actual restore operation. This technique ensures that your available backups are sufficient to restore the database. For this form of validation, use the steps described in "Validating Backups for Restore Operations."
Note:
Validating backups stored on tape can be time-consuming because the entire backup is read from tape.
You can perform both forms of validation using RMAN. You should incorporate both forms of validation into your backup strategy to ensure that your recoverability goals are met by your available backups.
See Also:
Oracle Database Backup and Recovery User’s Guide for information about validating backups of multitenant container databases (CDBs) and pluggable databases (PDBs)
9.4.4.1 Validating Selected Backups
Validating specific backups checks whether these backups exist and can be restored. It does not test whether the set of available backups meet your recoverability goals. For example, image copies of data files for several tablespaces from your database may exist, each of which can be validated. If there are some tablespaces for which no valid backups exist, however, then you cannot restore and recover the database.
To validate selected backups:
- Connect Oracle Recovery Manager (RMAN) to the target database as described in "Connecting to the Target Database Using RMAN."
- Validate the required backup.
The following
VALIDATE
command validates the datafileusers_02.dbf
.VALIDATE DATAFILE '/ora112/oradata/users_02.dbf'; Starting validate at 27-MAR-12 using channel ORA_DISK_1 channel ORA_DISK_1: starting validation of datafile channel ORA_DISK_1: specifying datafile(s) for validation input datafile file number=00020 name=/ora112/oradata/users_02.dbf channel ORA_DISK_1: validation complete, elapsed time: 00:00:01 List of Datafiles ================= File Status Marked Corrupt Empty Blocks Blocks Examined High SCN ---- ------ -------------- ------------ --------------- ---------- 20 OK 0 248 256 618976 File Name: /ora112/oradata/users_02.dbf Block Type Blocks Failing Blocks Processed ---------- -------------- ---------------- Data 0 0 Index 0 0 Other 0 8 Finished validate at 27-MAR-12
When you suspect that one or more backup pieces in a backup set are missing or have been damaged, use the VALIDATE BACKUPSET
command to validate the backup set.
See Also:
Oracle Database Backup and Recovery User’s Guide for more information about validating backups
9.4.4.2 Validating Backups for Restore Operations
You can test whether a sufficient set of backups exists that can be used to restore the specified database files. After you specify which tablespaces to restore and, possibly, a time as of which to restore them, Oracle Recovery Manager (RMAN) selects a set of backups that contain the needed data. RMAN reads the selected backups in their entirety to confirm that they are not corrupt, but does not produce output files.
Validating the restoration of files tests whether the file can be restored given the available backups, but it does not test whether all backups of the specified object are valid.
To verify whether specified database files can be restored:
- Connect RMAN to the target database as described in "Connecting to the Target Database Using RMAN."
- Run the
RESTORE … VALIDATE
command to determine if the required database files can be restored.-
To determine if the whole database can be restored:
RESTORE VALIDATE DATABASE; Starting restore at 29-MAR-12 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=129 device type=DISK channel ORA_DISK_1: scanning datafile copy /ade/b/191802369/oracle/work/orcva/RDBMS/datafile/o1_mf_tbs_3_7q60nj4y_.dbf channel ORA_DISK_1: starting validation of datafile backup set channel ORA_DISK_1: reading from backup piece /ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120328T051921_7q60g9oc_.bkp channel ORA_DISK_1: piece handle=/ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120328T051921_7q60g9oc_.bkp tag=TAG20120328T051921 channel ORA_DISK_1: restored backup piece 1 channel ORA_DISK_1: validation complete, elapsed time: 00:00:04 Finished restore at 29-MAR-12
-
To determine if a specified tablespace can be restored:
RESTORE TABLESPACE example VALIDATE; Starting restore at 29-MAR-12 using channel ORA_DISK_1 channel ORA_DISK_1: starting validation of datafile backup set channel ORA_DISK_1: reading from backup piece /ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120328T051921_7q60g9oc_.bkp channel ORA_DISK_1: piece handle=/ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120328T051921_7q60g9oc_.bkp tag=TAG20120328T051921 channel ORA_DISK_1: restored backup piece 1 channel ORA_DISK_1: validation complete, elapsed time: 00:00:01 Finished restore at 29-MAR-12
-
To determine if a datafile can be restored to a specified SCN:
RESTORE DATAFILE 1 VALIDATE UNTIL SCN 23456; Starting restore at 29-MAR-12 using channel ORA_DISK_1 channel ORA_DISK_1: starting validation of datafile backup set channel ORA_DISK_1: reading from backup piece /ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120330T044454_7qc75qyd_.bkp channel ORA_DISK_1: piece handle=/ade/b/191802369/oracle/work/orcva/RDBMS/backupset/2012_03_28/o1_mf_nnndf_TAG20120330T044454_7qc75qyd_.bkp tag=TAG20120330T044454 channel ORA_DISK_1: restored backup piece 1 channel ORA_DISK_1: validation complete, elapsed time: 00:00:03 Finished restore at 29-MAR-12
-
See Also:
-
Oracle Database Concepts for datafile recovery concepts
-
Oracle Database Backup and Recovery User’s Guide to learn how to use the
RESTORE
...
VALIDATE
command
9.5 Displaying Backup Reports
Backup reports contain summary and detailed information about past backup jobs run by Oracle Recovery Manager (RMAN). The view V$RMAN_BACKUP_JOB_DETAILS
contains information about backup jobs run by RMAN. This view contains information such as the time taken for the backup, when a job started and finished, and what type of backup was performed, and the status of the backup job.
To display backup reports:
Use the following query to display the backup job history.
SELECT SESSION_KEY,INPUT_TYPE,STATUS,START_TIME,END_TIME,ELAPSED_SECONDS/3600 hrs FROM V$RMAN_BACKUP_JOB_DETAILS; SESSION_KEY INPUT_TYPE STATUS START_TIM END_TIME HRS ----------- ------------- ----------------------- --------- --------- ---------- 8 DB FULL FAILED 27-MAR-12 27-MAR-12 1.64666666 50 DB FULL COMPLETED 28-MAR-12 28-MAR-12 .243055555 69 DB FULL COMPLETED 30-MAR-12 05-APR-12 147.176388
SESSION_KEY
is the unique key for the RMAN session in which the backup job occurred.