首先我们进行一次全库备份
RMAN> run { backup database format '/backup/full_%d_%T_%s' plus archivelog format '/backup/arch_%d_%T_%s' delete all input; } Starting backup at 2015/07/09 09:35:03current 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=28 RECID=27 STAMP=884590414channel ORA_DISK_1: starting piece 1 at 2015/07/09 09:35:03channel ORA_DISK_1: finished piece 1 at 2015/07/09 09:35:04piece handle=/backup/arch_DEVDB_20150709_115 tag=TAG20150709T093503 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01channel ORA_DISK_1: deleting archived log(s)archived log file name=/backup/archivelog/1_28_884357806.dbf RECID=27 STAMP=884590414channel 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=28 STAMP=884597703channel ORA_DISK_1: starting piece 1 at 2015/07/09 09:35:05channel ORA_DISK_1: finished piece 1 at 2015/07/09 09:35:06piece handle=/backup/arch_DEVDB_20150709_116 tag=TAG20150709T093503 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01channel ORA_DISK_1: deleting archived log(s)archived log file name=/backup/archivelog/1_1_884591314.dbf RECID=28 STAMP=884597703Finished backup at 2015/07/09 09:35:06Starting backup at 2015/07/09 09:35:06using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00001 name=/u01/app/oracle/oradata/devdb/system01.dbfinput datafile file number=00002 name=/u01/app/oracle/oradata/devdb/sysaux01.dbfinput datafile file number=00005 name=/u01/app/oracle/oradata/devdb/example01.dbfinput datafile file number=00003 name=/u01/app/oracle/oradata/devdb/undotbs01.dbfinput datafile file number=00004 name=/u01/app/oracle/oradata/devdb/users01.dbfinput datafile file number=00006 name=/u01/app/oracle/oradata/devdb/idx01.dbfchannel ORA_DISK_1: starting piece 1 at 2015/07/09 09:35:06channel ORA_DISK_1: finished piece 1 at 2015/07/09 09:36:41piece handle=/backup/full_DEVDB_20150709_117 tag=TAG20150709T093506 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:01:35channel 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 2015/07/09 09:36:42channel ORA_DISK_1: finished piece 1 at 2015/07/09 09:36:43piece handle=/backup/full_DEVDB_20150709_118 tag=TAG20150709T093506 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 2015/07/09 09:36:43Starting backup at 2015/07/09 09:36:43current 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=2 RECID=29 STAMP=884597804channel ORA_DISK_1: starting piece 1 at 2015/07/09 09:36:44channel ORA_DISK_1: finished piece 1 at 2015/07/09 09:36:45piece handle=/backup/arch_DEVDB_20150709_119 tag=TAG20150709T093644 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01channel ORA_DISK_1: deleting archived log(s)archived log file name=/backup/archivelog/1_2_884591314.dbf RECID=29 STAMP=884597804Finished backup at 2015/07/09 09:36:45RMAN>
2.对数据库中的表进行数据修改。
SQL> conn scott/tigerConnected.SQL> select count(*) from emp_new; COUNT(*)---------- 229376SQL> delete from emp_new where deptno=10;49152 rows deleted.SQL> commit;Commit complete.SQL> select count(*) from emp_new; COUNT(*)---------- 180224
3. 将数据库关闭。
SQL> conn / as sysdbaConnected.SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL>
4. 删除控制文件
SQL> ! rm /u01/app/oracle/oradata/devdb/control01.ctl SQL> ! rm /u01/app/oracle/fast_recovery_area/devdb/control02.ctl
5. 使用rman恢复控制文件
RMAN> restore controlfile from '//backup/full_DEVDB_20150709_118';Starting restore at 2015/07/09 09:46:41using target database control file instead of recovery catalogallocated 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:02output file name=/u01/app/oracle/oradata/devdb/control01.ctloutput file name=/u01/app/oracle/fast_recovery_area/devdb/control02.ctlFinished restore at 2015/07/09 09:46:43RMAN>
6. 将数据库启动到mount状态
RMAN> alter database mount;database mountedreleased channel: ORA_DISK_1
7.还原并恢复数据库
RMAN> restore database;Starting restore at 2015/07/09 09:48:56Starting implicit crosscheck backup at 2015/07/09 09:48:56allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=21 device type=DISKCrosschecked 3 objectsFinished implicit crosscheck backup at 2015/07/09 09:48:56Starting implicit crosscheck copy at 2015/07/09 09:48:56using channel ORA_DISK_1Finished implicit crosscheck copy at 2015/07/09 09:48:56searching for all files in the recovery areacataloging files...no files catalogedusing channel ORA_DISK_1channel 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/oracle/oradata/devdb/system01.dbfchannel ORA_DISK_1: restoring datafile 00002 to /u01/app/oracle/oradata/devdb/sysaux01.dbfchannel ORA_DISK_1: restoring datafile 00003 to /u01/app/oracle/oradata/devdb/undotbs01.dbfchannel ORA_DISK_1: restoring datafile 00004 to /u01/app/oracle/oradata/devdb/users01.dbfchannel ORA_DISK_1: restoring datafile 00005 to /u01/app/oracle/oradata/devdb/example01.dbfchannel ORA_DISK_1: restoring datafile 00006 to /u01/app/oracle/oradata/devdb/idx01.dbfchannel ORA_DISK_1: reading from backup piece /backup/full_DEVDB_20150709_117channel ORA_DISK_1: piece handle=/backup/full_DEVDB_20150709_117 tag=TAG20150709T093506channel ORA_DISK_1: restored backup piece 1channel ORA_DISK_1: restore complete, elapsed time: 00:01:15Finished restore at 2015/07/09 09:50:12RMAN> recover database;Starting recover at 2015/07/09 09:51:57using channel ORA_DISK_1datafile 6 not processed because file is read-onlystarting media recoveryarchived log for thread 1 with sequence 2 is already on disk as file /u01/app/oracle/oradata/devdb/redo02.logarchived log for thread 1 with sequence 3 is already on disk as file /u01/app/oracle/oradata/devdb/redo03.logarchived log file name=/u01/app/oracle/oradata/devdb/redo02.log thread=1 sequence=2archived log file name=/u01/app/oracle/oradata/devdb/redo03.log thread=1 sequence=3Oracle Error: ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error belowORA-01190: control file or data file 6 is from before the last RESETLOGSORA-01110: data file 6: '/u01/app/oracle/oradata/devdb/idx01.dbf'media recovery complete, elapsed time: 00:00:04Finished recover at 2015/07/09 09:52:02
我们可以看到,恢复完成了。但是根据日志可以看出,resetlogs打开数据库时会报错的。
我们尝试打开看看
RMAN> alter database open resetlogs;RMAN-00571: ===========================================================RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============RMAN-00571: ===========================================================RMAN-03002: failure of alter db command at 07/09/2015 09:53:21ORA-01190: control file or data file 6 is from before the last RESETLOGSORA-01110: data file 6: '/u01/app/oracle/oradata/devdb/idx01.dbf'
恢复数据库时,有这么一段信息
datafile 6 not processed because file is read-only
原因找打了,在做数据库备份时,datafile 6是只读的。我们备份完毕后,修改表emp_new中的数据时,将datafile 6改成了读写模式。在做恢复时,并没有去恢复datafile 6. 导致数据文件最终不一致。
所以可以得出结论一旦有表空间由只读变成读写模式后,一定要做一次全量备份
我们将datafile 6 offline。再次打开数据库。
SQL> alter database datafile 6 offline;Database altered.RMAN> alter database open resetlogs;database openedRMAN>
虽说我们是使用resetlogs打开的数据库,但是再做数据库恢复时,也应用了在线日志。那么数据就应该没有丢失。我们做一个检验
SQL> conn scott/tigerConnected.SQL> select count(*) from emp_new; COUNT(*)---------- 180224SQL>
但是我们仔细的想一想,其实是有问题的。我丢失的只是控制文件,不应该真的去恢复我的数据库。如果是手工管理的备份恢复,只需执行如下语句即可完成恢复
SQL> recover database using backup controlfile;
恢复的速度会快很多。为啥RMAN没有提供类似功能呢?