一. rman简介
RMAN可以用来备份和还原数据库文件、归档日志和控制文件。它也可以用来执行完全或不完全的数据库恢复。与传统工具相比,RMAN具有独特的优势:跳过未使用的数据块。当备份一个RMAN备份集时,RMAN不会备份从未被写入的数据块,而传统的方式无法获知那些是未被使用的数据块。
[oracle@oracle235 ~]$ rman target /Recovery Manager: Release 11.2.0.4.0 - Production on Wed Aug 8 18:07:35 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database: ORA235 (DBID=2047494122)RMAN>
eg:
RMAN> backup database format '/data/backup/db_full_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00005 name=/data/oracle/data/lottu01.dbfinput datafile file number=00001 name=/u01/app/oradata/ora235/system01.dbfinput datafile file number=00002 name=/u01/app/oradata/ora235/sysaux01.dbfinput datafile file number=00003 name=/u01/app/oradata/ora235/undotbs01.dbfinput datafile file number=00004 name=/u01/app/oradata/ora235/users01.dbfchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/db_full_03ta2cqc_1_1 tag=TAG20180808T180916 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:45channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/db_full_04ta2crp_1_1 tag=TAG20180808T180916 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
2.2 备份表空间
eg: backup tablespace lottu format '/data/backup/tsp_lottu_%U';
RMAN> backup tablespace lottu format '/data/backup/tsp_lottu_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00005 name=/data/oracle/data/lottu01.dbfchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/tsp_lottu_05ta2e01_1_1 tag=TAG20180808T182921 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
eg:
RMAN> backup datafile 5 format '/data/backup/df_5_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00005 name=/data/oracle/data/lottu01.dbfchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/df_5_06ta2e6k_1_1 tag=TAG20180808T183252 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
SQL> select file_id,file_name from dba_data_files; FILE_ID FILE_NAME---------- ---------------------------------------- 1 /u01/app/oradata/ora235/system01.dbf 2 /u01/app/oradata/ora235/sysaux01.dbf 3 /u01/app/oradata/ora235/undotbs01.dbf 4 /u01/app/oradata/ora235/users01.dbf 5 /data/oracle/data/lottu01.dbf
RMAN> backup datafile '/data/oracle/data/lottu01.dbf' format '/data/backup/dfname_5_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00005 name=/data/oracle/data/lottu01.dbfchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/dfname_5_07ta2ei5_1_1 tag=TAG20180808T183901 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
including current control file in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/db_full_04ta2crp_1_1 tag=TAG20180808T180916 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
在查看控制文件的备份集;我们可以看到:说明对控制文件已经有备份了。
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:01 08-AUG-18 BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20180808T180916 Piece Name: /data/backup/db_full_04ta2crp_1_1 Control File Included: Ckp SCN: 915985 Ckp time: 08-AUG-18
eg:
RMAN> backup current controlfile format '/data/backup/ct_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/ct_08ta2f97_1_1 tag=TAG20180808T185119 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
- eg:
- eg:
RMAN> backup current controlfile format '/data/backup/ct_%U' plus archivelog;Starting backup at 08-AUG-18current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=56 RECID=1 STAMP=983577870input archived log thread=1 sequence=57 RECID=2 STAMP=983579059channel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/u01/app/oracle/dbs/09ta2grj_1_1 tag=TAG20180808T191811 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=1 RECID=3 STAMP=983641357input archived log thread=1 sequence=2 RECID=4 STAMP=983647091channel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/u01/app/oracle/dbs/0ata2grk_1_1 tag=TAG20180808T191811 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/ct_0bta2grl_1_1 tag=TAG20180808T191813 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18Starting backup at 08-AUG-18current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=3 RECID=5 STAMP=983647095channel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/u01/app/oracle/dbs/0cta2grn_1_1 tag=TAG20180808T191815 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
使用plus archivelog时备份数据库完成的动作(backup database plus archivelog)
eg:
RMAN> backup spfile format 'spfile_%U';Starting backup at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/u01/app/oracle/dbs/spfile_0dta2h1h_1_1 tag=TAG20180808T192121 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
[oracle@oracle235 ~]$ rman target / Recovery Manager: Release 11.2.0.4.0 - Production on Wed Aug 8 21:39:40 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database: ORA235 (DBID=2047494122)RMAN> backup database format '/data/backup/db_full_%U';Starting backup at 08-AUG-18using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=34 device type=DISKchannel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00005 name=/data/oracle/data/lottu01.dbfinput datafile file number=00001 name=/u01/app/oradata/ora235/system01.dbfinput datafile file number=00002 name=/u01/app/oradata/ora235/sysaux01.dbfinput datafile file number=00003 name=/u01/app/oradata/ora235/undotbs01.dbfinput datafile file number=00004 name=/u01/app/oradata/ora235/users01.dbfchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/db_full_0gta2p56_1_1 tag=TAG20180808T213950 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:45channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 08-AUG-18channel ORA_DISK_1: finished piece 1 at 08-AUG-18piece handle=/data/backup/db_full_0hta2p6j_1_1 tag=TAG20180808T213950 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 08-AUG-18
- 模拟故障;将参数文件、控制文件、数据文件都丢失;
[oracle@oracle235 ora235]$ rm *.dbf[oracle@oracle235 ora235]$ rm *.ctl[oracle@oracle235 ora235]$ rm /data/oracle/data/lottu01.dbf[oracle@oracle235 dbs]$ rm spfile_0dta2h1h_1_1 spfileora235.ora orapwora235 init.ora
[oracle@oracle235 ~]$ rlwrap rman target /Recovery Manager: Release 11.2.0.4.0 - Production on Wed Aug 8 21:48:32 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database (not started)RMAN> set DBID=2047494122;executing command: SET DBIDRMAN> startup nomount;startup failed: ORA-01078: failure in processing system parametersLRM-00109: could not open parameter file '/u01/app/oracle/dbs/initora235.ora'starting Oracle instance without parameter file for retrieval of spfileOracle instance startedTotal System Global Area 1068937216 bytesFixed Size 2260088 bytesVariable Size 281019272 bytesDatabase Buffers 780140544 bytesRedo Buffers 5517312 bytesRMAN> restore spfile from autobackup;Starting restore at 08-AUG-18using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=19 device type=DISKchannel ORA_DISK_1: looking for AUTOBACKUP on day: 20180808channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180807channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180806channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180805channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180804channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180803channel ORA_DISK_1: looking for AUTOBACKUP on day: 20180802channel ORA_DISK_1: no AUTOBACKUP in 7 days foundRMAN-00571: ===========================================================RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============RMAN-00571: ===========================================================RMAN-03002: failure of restore command at 08/08/2018 21:51:16RMAN-06172: no AUTOBACKUP found or specified handle is not a valid copy or pieceRMAN> restore spfile from '/data/backup/db_full_0hta2p6j_1_1';Starting restore at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: restoring spfile from AUTOBACKUP /data/backup/db_full_0hta2p6j_1_1channel ORA_DISK_1: SPFILE restore from AUTOBACKUP completeFinished restore at 08-AUG-18
其中的DBID为2047494122;是从rman备份日志查到的。这样参数文件恢复Ok。
[oracle@oracle235 dbs]$ ll spfileora235.ora -rw-r-----. 1 oracle oinstall 2560 Aug 8 21:52 spfileora235.ora
2. 恢复控制文件
RMAN> shutdown immediate;Oracle instance shut downRMAN> startup nomount;connected to target database (not started)Oracle instance startedTotal System Global Area 780824576 bytesFixed Size 2257312 bytesVariable Size 511708768 bytesDatabase Buffers 264241152 bytesRedo Buffers 2617344 bytesRMAN> restore controlfile to '/u01/app/oradata/ora235/control01.ctl' from '/data/backup/db_full_0hta2p6j_1_1';Starting restore at 08-AUG-18allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=19 device type=DISKchannel ORA_DISK_1: restoring control filechannel ORA_DISK_1: restore complete, elapsed time: 00:00:01Finished restore at 08-AUG-18RMAN> restore controlfile to '/u01/app/oradata/ora235/control02.ctl' from '/data/backup/db_full_0hta2p6j_1_1';Starting restore at 08-AUG-18using channel ORA_DISK_1channel ORA_DISK_1: restoring control filechannel ORA_DISK_1: restore complete, elapsed time: 00:00:01Finished restore at 08-AUG-18
这样控制文件也恢复OK;
3. 恢复数据文件;控制文件恢复成功;现在数据库可以启动到加载状态
RMAN> alter database mount;database mountedreleased channel: ORA_DISK_1RMAN> restore database;Starting restore at 08-AUG-18allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=19 device type=DISKchannel ORA_DISK_1: starting datafile backup set restorechannel ORA_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_DISK_1: restoring datafile 00001 to /u01/app/oradata/ora235/system01.dbfchannel ORA_DISK_1: restoring datafile 00002 to /u01/app/oradata/ora235/sysaux01.dbfchannel ORA_DISK_1: restoring datafile 00003 to /u01/app/oradata/ora235/undotbs01.dbfchannel ORA_DISK_1: restoring datafile 00004 to /u01/app/oradata/ora235/users01.dbfchannel ORA_DISK_1: restoring datafile 00005 to /data/oracle/data/lottu01.dbfchannel ORA_DISK_1: reading from backup piece /data/backup/db_full_0gta2p56_1_1channel ORA_DISK_1: piece handle=/data/backup/db_full_0gta2p56_1_1 tag=TAG20180808T213950channel ORA_DISK_1: restored backup piece 1channel ORA_DISK_1: restore complete, elapsed time: 00:01:26Finished restore at 08-AUG-18RMAN> recover database;Starting recover at 08-AUG-18using channel ORA_DISK_1starting media recoveryarchived log for thread 1 with sequence 4 is already on disk as file /u01/app/oradata/ora235/redo01.logarchived log file name=/u01/app/oradata/ora235/redo01.log thread=1 sequence=4media recovery complete, elapsed time: 00:00:00Finished recover at 08-AUG-18RMAN> alter database open;RMAN-00571: ===========================================================RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============RMAN-00571: ===========================================================RMAN-03002: failure of alter db command at 08/08/2018 22:06:08ORA-01589: must use RESETLOGS or NORESETLOGS option for database openRMAN> alter database open resetlogs;database opened
到了这步数据库算是成功恢复;再重启数据库。
# a、基于TIME 参数不完全恢复run { shutdown immediate; startup mount; set until time "to_date('20180805 10:09:53','yyyymmdd hh24:mi:ss')"; restore database; recover database; alter database open resetlogs;}# b、基于SCN 参数不完全恢复run { shutdown immediate; startup mount; set until scn 3400; restore database; recover database; alter database open resetlogs;} # c、基于SEQUENCE 参数不完全恢复:run { shutdown immediate; startup mount; set until sequence 12903; restore database; recover database; alter database open resetlogs;}
SQL> conn lottu/li0924Connected.SQL> create table t_lottu01 as select level id from dual connect by level <= 5;Table created.SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;TO_CHAR(SYSDATE,'YY-------------------2018-08-08 23:00:22SQL> drop table t_lottu01;Table dropped.SQL> conn system/Oracle235Connected.SQL> alter system switch logfile; System altered.SQL> alter system switch logfile; System altered.
- rman执行不完全恢复
[oracle@oracle235 ~]$ rlwrap rman target /Recovery Manager: Release 11.2.0.4.0 - Production on Wed Aug 8 23:01:26 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database: ORA235 (DBID=2047494122)run { 2> shutdown immediate; startup mount; set until time "to_date('20180808 23:00:22','yyyymmdd hh24:mi:ss')"; restore database; recover database; alter database open resetlogs;8> };using target database control file instead of recovery catalogdatabase closeddatabase dismountedOracle instance shut downconnected to target database (not started)Oracle instance starteddatabase mountedTotal System Global Area 780824576 bytesFixed Size 2257312 bytesVariable Size 511708768 bytesDatabase Buffers 264241152 bytesRedo Buffers 2617344 bytesexecuting command: SET until clauseStarting restore at 08-AUG-18allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=18 device type=DISKchannel ORA_DISK_1: starting datafile backup set restorechannel ORA_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_DISK_1: restoring datafile 00001 to /u01/app/oradata/ora235/system01.dbfchannel ORA_DISK_1: restoring datafile 00002 to /u01/app/oradata/ora235/sysaux01.dbfchannel ORA_DISK_1: restoring datafile 00003 to /u01/app/oradata/ora235/undotbs01.dbfchannel ORA_DISK_1: restoring datafile 00004 to /u01/app/oradata/ora235/users01.dbfchannel ORA_DISK_1: restoring datafile 00005 to /data/oracle/data/lottu01.dbfchannel ORA_DISK_1: reading from backup piece /data/backup/db_full_0gta2p56_1_1channel ORA_DISK_1: piece handle=/data/backup/db_full_0gta2p56_1_1 tag=TAG20180808T213950channel ORA_DISK_1: restored backup piece 1channel ORA_DISK_1: restore complete, elapsed time: 00:01:05Finished restore at 08-AUG-18Starting recover at 08-AUG-18using channel ORA_DISK_1starting media recoveryarchived log for thread 1 with sequence 4 is already on disk as file /data/arch/1_4_983579059.dbfarchived log for thread 1 with sequence 1 is already on disk as file /data/arch/1_1_983657183.dbfarchived log file name=/data/arch/1_4_983579059.dbf thread=1 sequence=4archived log file name=/data/arch/1_1_983657183.dbf thread=1 sequence=1media recovery complete, elapsed time: 00:00:02Finished recover at 08-AUG-18database opene