如果要说DBA工作中最重要的职责(没有之一),那无疑就是保证客户数据的安全和完整,可以看到几乎任何一本Oracle DBA的技术书籍一定都会把大篇幅来介绍数据库的备份与恢复,从中也可以看到备份和恢复的重要程度。eygle曾经总结了DBA生存之四大守则的第一条就是:备份重于一切 我们必需知道,系统总是要崩溃的,没有有效的备份只是等哪一天死!我经常开玩笑的说,唯一会使DBA在梦中惊醒的就是,没有有效的备份.
RMAN概览 RMAN是Oracle数据库软件自带的备份/恢复工具。RMAN只能用于9i或更高的版本中。它能够备份整个数据库或数据库部件,如表空间、数据文件、控制文件、归档文件以及Spfile参数文件。通过RMAN的方式无论是要备份还是要恢复,都必须先启动实例并加载数据库。
From Wiki:RMAN (Recovery Manager) is a backup and recovery manager supplied for Oracle databases (from version 8) created by the Oracle Corporation. It provides database backup, restore, and recovery capabilities addressing high availability and disaster recovery concerns. Oracle Corporation recommends RMAN as its preferred method for backup and recovery and has written command-line and graphical (via Oracle Enterprise Manager) interfaces for the product. |
RMAN之备份 连接数据库 很简单,进入到命令提示符界面:
C:\Users\TianPan>RMAN TARGET / Recovery Manager: Release 11.2.0.1.0 - Production on Fri Jul 3 11:40:13 2015 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. connected to target database: PTIAN (DBID=1075230356) RMAN> |
你也可以先启动RMAN,然后再通过CONNECT命令来连接目标数据库:
C:\Users\TianPan>rman Recovery Manager: Release 11.2.0.1.0 - Production on Fri Jul 3 11:45:30 2015 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. RMAN> connect target / connected to target database: PTIAN (DBID=1075230356) RMAN> |
使用RMAN来做各种类型的备份 1.整库备份 整库备份的命令很简单,就是backup database
RMAN> backup database; Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF input datafile file number=00002 name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF input datafile file number=00003 name=E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF input datafile file number=00005 name=E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF input datafile file number=00004 name=E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T153438_BSKQJJHS_.BKP tag=TAG20150705T153438 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:56 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current control file in backup set including current SPFILE in backup set channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NCSNF_TAG20150705T153438_BSKQL8JB_.BKP tag=TAG20150705T153438 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
如果是真正的备份操作,你肯定希望能够指定备份集的存储位置,没问题,最简单的方式是在执行BACKUP命令时,指定FORMAT参数来自定义备份片段的路径和命令规则,比如:
RMAN> BACKUP DATABASE FORMAT 'E:\db_bk\bak_%U'; Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF input datafile file number=00002 name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF input datafile file number=00003 name=E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF input datafile file number=00005 name=E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF input datafile file number=00004 name=E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\DB_BK\BAK_03QB9U0V_1_1 tag=TAG20150705T160031 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:55 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current control file in backup set including current SPFILE in backup set channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\DB_BK\BAK_04QB9U2N_1_1 tag=TAG20150705T160031 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
如果想查看创建的全库备份,可以通过LIST命令来查看:
RMAN> LIST BACKUP OF DATABASE; List of Backup Sets =================== BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 1 Full 1.15G DISK 00:00:50 05-JUL-15 BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20150705T153438 Piece Name: E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T153438_BSKQJJHS_.BKP List of Datafiles in backup set 1 File LV Type Ckp SCN Ckp Time Name ---- -- ---- ---------- --------- ---- 1 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF 2 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF 3 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF 4 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF 5 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 3 Full 1.15G DISK 00:00:46 05-JUL-15 BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20150705T160031 Piece Name: E:\DB_BK\BAK_03QB9U0V_1_1 List of Datafiles in backup set 3 File LV Type Ckp SCN Ckp Time Name ---- -- ---- ---------- --------- ---- 1 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF 2 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF 3 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF 4 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF 5 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF RMAN> |
2.表空间的备份 rman还可以针对表空间备份。
RMAN> BACKUP TABLESPACE USERS; Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00004 name=E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T160551_BSKSC02G_.BKP tag=TAG20150705T160551 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
查看表空间的备份:
RMAN> LIST BACKUP OF TABLESPACE USERS; List of Backup Sets =================== BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 1 Full 1.15G DISK 00:00:50 05-JUL-15 BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20150705T153438 Piece Name: E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T153438_BSKQJJHS_.BKP List of Datafiles in backup set 1 File LV Type Ckp SCN Ckp Time Name ---- -- ---- ---------- --------- ---- 4 Full 3406018 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 3 Full 1.15G DISK 00:00:46 05-JUL-15 BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20150705T160031 Piece Name: E:\DB_BK\BAK_03QB9U0V_1_1 List of Datafiles in backup set 3 File LV Type Ckp SCN Ckp Time Name ---- -- ---- ---------- --------- ---- 4 Full 3407567 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 5 Full 4.02M DISK 00:00:01 05-JUL-15 BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20150705T160551 Piece Name: E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T160551_BSKSC02G_.BKP List of Datafiles in backup set 5 File LV Type Ckp SCN Ckp Time Name ---- -- ---- ---------- --------- ---- 4 Full 3408110 05-JUL-15 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF RMAN> |
RMAN> DELETE BACKUPSET 5; using channel ORA_DISK_1 List of Backup Pieces BP Key BS Key Pc# Cp# Status Device Type Piece Name ------- ------- --- --- ----------- ----------- ---------- 5 5 1 1 AVAILABLE DISK E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T160551_BSKSC02G_.BKP Do you really want to delete the above objects (enter YES or NO)? YES deleted backup piece backup piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T160551_BSKSC02G_.BKP RECID=5 STAMP=884275552 Deleted 1 objects RMAN> |
3.数据文件的备份
SQL> select file#,name from v$datafile; FILE# NAME ---------- -------------------------------------------------------------------------------- 1 E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF 2 E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF 3 E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF 4 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF 5 E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF |
RMAN> BACKUP DATAFILE 'E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF'; Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00004 name=E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNNDF_TAG20150705T163203_BSKTW49M_.BKP tag=TAG20150705T163203 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
4.控制文件备份
RMAN> BACKUP CURRENT CONTROLFILE; Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current control file in backup set channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NCNNF_TAG20150705T163456_BSKV1L70_.BKP tag=TAG20150705T163456 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
RMAN> LIST BACKUP OF CONTROLFILE; List of Backup Sets =================== BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 2 Full 9.36M DISK 00:00:02 05-JUL-15 BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20150705T153438 Piece Name: E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NCSNF_TAG20150705T153438_BSKQL8JB_.BKP Control File Included: Ckp SCN: 3406084 Ckp time: 05-JUL-15 BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 4 Full 9.36M DISK 00:00:01 05-JUL-15 BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20150705T160031 Piece Name: E:\DB_BK\BAK_04QB9U2N_1_1 Control File Included: Ckp SCN: 3407825 Ckp time: 05-JUL-15 BS Key Type LV Size Device Type Elapsed Time Completion Time ------- ---- -- ---------- ----------- ------------ --------------- 7 Full 9.33M DISK 00:00:02 05-JUL-15 BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20150705T163456 Piece Name: E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NCNNF_TAG20150705T163456_BSKV1L70_.BKP Control File Included: Ckp SCN: 3409951 Ckp time: 05-JUL-15 RMAN> |
5.归档文件的备份 归档日志对于数据库介质恢复相当关键,只要拥有相应的归档日志文件,就能确保我们将数据库恢复到备份之后的任意时刻。在RMAN中备份归档日志有以下两种方式:
5.1 利用BACKUP ARCHIVELOG命令备份
RMAN> BACKUP ARCHIVELOG ALL; Starting backup at 05-JUL-15 current log archived using channel ORA_DISK_1 channel ORA_DISK_1: starting archived log backup set channel ORA_DISK_1: specifying archived log(s) in backup set input archived log thread=1 sequence=86 RECID=1 STAMP=872864663 input archived log thread=1 sequence=87 RECID=2 STAMP=882545743 input archived log thread=1 sequence=88 RECID=3 STAMP=882551016 input archived log thread=1 sequence=89 RECID=4 STAMP=882725211 input archived log thread=1 sequence=90 RECID=5 STAMP=882810557 input archived log thread=1 sequence=91 RECID=6 STAMP=883318762 input archived log thread=1 sequence=92 RECID=7 STAMP=883346472 input archived log thread=1 sequence=93 RECID=8 STAMP=884017140 input archived log thread=1 sequence=94 RECID=9 STAMP=884086400 input archived log thread=1 sequence=95 RECID=10 STAMP=884180851 input archived log thread=1 sequence=96 RECID=11 STAMP=884264845 input archived log thread=1 sequence=97 RECID=12 STAMP=884277664 channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_ANNNN_TAG20150705T164104_BSKVF1MB_.BKP tag=TAG20150705T164104 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15 Finished backup at 05-JUL-15 RMAN> |
5.2 执行BACKUP命令时指定PLUS ARCHIVELOG子句 执行BACKUP命令时指定PLUS ARCHIVELOG子句,如下所示:
RMAN> BACKUP CURRENT CONTROLFILE PLUS ARCHIVELOG; Starting backup at 05-JUL-15 current log archived using channel ORA_DISK_1 channel ORA_DISK_1: starting archived log backup set channel ORA_DISK_1: specifying archived log(s) in backup set input archived log thread=1 sequence=86 RECID=1 STAMP=872864663 input archived log thread=1 sequence=87 RECID=2 STAMP=882545743 input archived log thread=1 sequence=88 RECID=3 STAMP=882551016 input archived log thread=1 sequence=89 RECID=4 STAMP=882725211 input archived log thread=1 sequence=90 RECID=5 STAMP=882810557 input archived log thread=1 sequence=91 RECID=6 STAMP=883318762 input archived log thread=1 sequence=92 RECID=7 STAMP=883346472 input archived log thread=1 sequence=93 RECID=8 STAMP=884017140 input archived log thread=1 sequence=94 RECID=9 STAMP=884086400 input archived log thread=1 sequence=95 RECID=10 STAMP=884180851 input archived log thread=1 sequence=96 RECID=11 STAMP=884264845 input archived log thread=1 sequence=97 RECID=12 STAMP=884277664 input archived log thread=1 sequence=98 RECID=13 STAMP=884277911 channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_ANNNN_TAG20150705T164511_BSKVNRLQ_.BKP tag=TAG20150705T164511 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15 Finished backup at 05-JUL-15 Starting backup at 05-JUL-15 using channel ORA_DISK_1 channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current control file in backup set channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NCNNF_TAG20150705T164527_BSKVO90S_.BKP tag=TAG20150705T164527 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 Starting backup at 05-JUL-15 current log archived using channel ORA_DISK_1 channel ORA_DISK_1: starting archived log backup set channel ORA_DISK_1: specifying archived log(s) in backup set input archived log thread=1 sequence=99 RECID=14 STAMP=884277931 channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_ANNNN_TAG20150705T164531_BSKVOD0M_.BKP tag=TAG20150705T164531 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02 Finished backup at 05-JUL-15 RMAN> |
完成备份之后,可以通过下列命令查看已备份的归档日志片段:
RMAN> LIST BACKUP OF ARCHIVELOG ALL;
6.初始化参数文件的备份
RMAN> BACKUP SPFILE; Starting backup at 05-JUL-15 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=138 device type=DISK channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set including current SPFILE in backup set channel ORA_DISK_1: starting piece 1 at 05-JUL-15 channel ORA_DISK_1: finished piece 1 at 05-JUL-15 piece handle=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\BACKUPSET\2015_07_05\O1_MF_NNSNF_TAG20150705T164858_BSKVVTPK_.BKP tag=TAG20150705T164858 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 05-JUL-15 RMAN> |
7.对备份集备份 备份所有备份集:
RMAN> BACKUP BACKUPSET ALL;
备份指定的备份集:
RMAN> BACKUP BACKUPSET n;
n=备份集ID,可以同时指定多个,相互间以逗号分隔即可。
参考 RMAN Tips 李丙洋的《涂抹Oracle》