RMAN备份
不归档 归档
online offline (脱机) online offline
完全 部分 完全 部分 完全 部分 完全 部分
不归档模式下,只有在脱机状态下,进行全部备份
shutdown nomount mount open
RMAN> backup tablespace users; //备份表空间
RMAN> backup database;//全数据库备份
RMAN> backup datafile 4; //部分数据库备份 (备份数据数据文件)
【datafile +数据文件的地址或者数据文件的id】
备份数据文件:
SQL> select file_id, file_name from dba_data_files;
RMAN> backup datafile 4;
RMAN> backup datafile 4, 5;
RMAN> backup datafile '/u01/app/oracle/oradata/orcl/users01.dbf';
RMAN> backup tablespace users;
RMAN> backup tablespace users, example;
RMAN> backup database;
RMAN> list backup; //查看备份
desc v$backup_set,浏览器查看备份
增量备份:
RMAN> backup incremental level 0 tablespace users; //备份初始增量
RMAN> list backup of tablespace users;//查看初始 Incr 0 4.02M
SQL> create table t1(x int) tablespace users; 添加数据
SQL> insert into t1 values (1);
SQL> commit;
RMAN> backup incremental level 1 tablespace users;//添加数据后,进行备份
RMAN> list backup of tablespace users;//查看 Incr 1 120.00K
RMAN> backup incremental level 1 tablespace users;//未添加数据,再次备份
RMAN> list backup of tablespace users;//查看 Incr 1 4.02M
SQL> create table t2(x int) tablespace users;
SQL> insert into t2 values (1);
SQL> commit;
RMAN> backup incremental level 1 cumulative tablespace users;//累计备份(t1+t2)
RMAN> list backup of tablespace users;//查看 Incr 1 176.00K
开启块跟踪:
SQL> alter database enable block change tracking using file '/home/oracle/blk_trk.chg';
需要重新连接会话
SQL> select * from v$block_change_tracking;
SQL> select DATAFILE_BLOCKS, BLOCKS_READ, BLOCKS, USED_CHANGE_TRACKING from v$backup_datafile where INCREMENTAL_LEVEL>0;
SQL> create table t3(x int) tablespace users;
SQL> insert into t3 values (1);
SQL> commit;
RMAN> backup incremental level 1 tablespace users;
SQL> select DATAFILE_BLOCKS, BLOCKS_READ, BLOCKS, USED_CHANGE_TRACKING from v$backup_datafile where INCREMENTAL_LEVEL>0;
SQL> alter database disable block change tracking; 关闭
增量更新:
SQL> create table t1(x int) tablespace users;
SQL> insert into t1 values (1);
SQL> commit;
RMAN> backup incremental level 1 for recover of copy with tag 'update_copy' tablespace users; 第一次创建的是0级备份
RMAN> list copy; 记录time和scn
SQL> insert into t1 values (2);
SQL> commit;
RMAN> backup incremental level 1 for recover of copy with tag 'update_copy' tablespace users; 第2次创建的是1级备份
RMAN> list backup; backupset格式
RMAN> recover copy of tablespace users with tag 'update_copy';
RMAN> list copy; time和scn更新
脚本形式:
RMAN> run {
backup incremental level 1 for recover of copy with tag 'update_copy' tablespace users;
recover copy of tablespace users with tag 'update_copy';
}
备份归档日志:
RMAN> list archivelog all; //列出所有的归档日志
RMAN> backup archivelog all delete all input;
RMAN> list archivelog all;
RMAN> list backup;
备份的维护:
查看:list系列 查看现有备份
RMAN> list backup; //查看备份
RMAN> list copy; //查看镜像复制
RMAN> list backup of tablespace users; //查看指定表空间的备份
RMAN> list backup of datafile 4;//查看指定数据文件的备份
RMAN> list archivelog all;//查看所有的归档日志
检查备份:report系列 检查不足的备份
RMAN> delete backup; //删除备份
RMAN> delete copy;
RMAN> list backup; list copy;
RMAN> report need backup; //检查需要备份的(冗余度<2)
RMAN> backup tablespace users;
RMAN> report need backup;
RMAN> CONFIGURE RETENTION POLICY TO REDUNDANCY 2;//设置冗余度为2
RMAN> report need backup;
RMAN> CONFIGURE RETENTION POLICY clear;
RMAN> report obsolete; //废弃的(冗余度超过设置值1)
删除备份:
RMAN> delete backupset of tablespace users;
RMAN> delete backupset 1234;
RMAN> backup tablespace users;
RMAN> backup tablespace users;
RMAN> show all;
RMAN> delete obsolete;//删除废弃的
crosscheck:
用于核对磁盘和磁带上的备份文件,以确保RMAN资料库与备份文件保持同步。
注意:该命令只会检查RMAN资料库所记载的备份文件。当执行crosscheck命令时,如果资料库记录不匹配于备份文件的物理状态,那么该命令会更新资料库记录的状态信息。备份文件的状态包括:available(可用的)、unavailable、expired (过期的)。
RMAN> delete backup;
RMAN> backup tablespace users;
RMAN> list backup of tablespace users;
$ mv /u01/app/oracle/fast_recovery_area/ORCL/backupset/… 改名
RMAN> restore datafile 4;//恢复 报错
RMAN> crosscheck backup; //检查备份集和实际的文件
RMAN> list backup of tablespace users; 报废状态
RMAN> list expired backup;//过期
$ mv /u01/app/oracle/fast_recovery_area/ORCL/backupset/… 恢复原名
RMAN> crosscheck backup;
RMAN> list backup of tablespace users; 可用状态
RMAN> delete expired backup;
catalog:
$ cp /u01/app/oracle/fast_recovery_area/ORCL/backupset/… 复制
RMAN> delete backup;
RMAN> list backup; backupset消失
$ mv /u01/app/oracle/fast_recovery_area/ORCL/backupset/… 恢复原名
RMAN> catalog recovery area noprompt;
RMAN> list backup; backupset恢复
RMAN恢复
数据文件
在做restore和recover之前指定时间点:如
run{
set until time "to_date('2017-08-16 15:16:29','yyyy-mm-dd hh24')";
restore database;
recover database;
}
数据库恢复思路
Restore(恢复数据文件(即备份文件))
Recover(写日志:用来恢复归档文件和联机重做文件(即提交但未归档的数据))
1.Redo(前滚)
2.Undo(回滚)
RPO/RTO
数据文件:
不归档方式下丢失一个数据文件:
SQL> archive log list
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('friday, before backup');
SQL> commit;
备份:
SQL> 查询v$datafile, v$logfile, v$tempfile, v$controlfile
SQL> shutdown immediate
$ cd $ORACLE_BASE/oradata/
$ cp -r orcl orcl.bak
$ cd $ORACLE_BASE/fast_recovery_area/orcl
$ cp control02.ctl control02.ctl.bak
SQL> startup
备份后工作:
SQL> insert into t1 values ('monday, after backup');
SQL> commit;
故障:
SQL> alter system flush buffer_cache;
$ cd $ORACLE_BASE/oradata/orcl
$ >users01.dbf
SQL> select * from t1; 报错
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
$ dbv file=/u01/app/oracle/oradata/orcl/users01.dbf
恢复:
SQL> shutdown abort
$ cd $ORACLE_BASE/oradata
$ rm -rf orcl
$ mv orcl.bak orcl
$ cd $ORACLE_BASE/fast_recovery_area/orcl
$ mv control02.ctl.bak control02.ctl
SQL> startup
SQL> select * from t1;
归档模式下丢失一个数据文件:
SQL> archive log list //查看是否在归档模式
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('friday, before backup');
SQL> commit;
备份:
RMAN> backup tablespace users tag "tbs_users_weekend_backup";
备份后工作:
SQL> select group#, sequence#, status, archived from v$log; //查看当前要写入的归档日志
SQL> insert into t1 values ('after backup, logseq 42, archived'); // 42是当前要写入归档日志的编号
SQL> commit; //提交
SQL> alter system switch logfile;//切换日志
SQL> select group#, sequence#, status, archived from v$log; //查看当前要写入的归档日志
SQL> insert into t1 values ('after backup, logseq 43, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> select group#, sequence#, status, archived from v$log; //查看当前要写入的归档日志
SQL> insert into t1 values ('after backup, logseq 44, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> select group#, sequence#, status, archived from v$log; //查看当前要写入的归档日志
SQL> insert into t1 values ('after backup, logseq 45, current');
SQL> commit;
SQL> insert into t1 values ('after backup, logseq 45, current, uncommitted');
SQL> select * from t1;
X
--------------------------------------------------
friday, before backup //备份文件
after backup, logseq 42, archived //归档日志
after backup, logseq 43, archived //归档日志
after backup, logseq 44, archived //归档日志
after backup, logseq 45, current //联机重写日志
after backup, logseq 45, current uncommitted //未提交(机器突然故障)
故障:进行破坏
SQL> shutdown abort //不正常关闭
$ rm $ORACLE_BASE/oradata/orcl/users01.dbf //移除数据文件
SQL> startup 报错
SQL> select open_mode from v$database; //查看数据库开启到的阶段
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
恢复:
RMAN> list backup of tablespace users;
RMAN> list archivelog all;
SQL> alter database datafile 4 offline;//将出现错误的数据库离线, system和undo tbs不能offline(离线)
SQL> alter database open;
RMAN> restore datafile 4;// 找回完全备份的数据
RMAN> recover datafile 4;//找回归档日志里的数据+联机重做日志里的数据(提交但未归档)
SQL> alter database datafile 4 online;
SQL> select * from t1;
X
-------------------------------------------------- //恢复后的数据(备份文件+归档日志+联机重写日志)
friday, before backup restore
after backup, logseq 42, archived recove
after backup, logseq 43, archived recove
after backup, logseq 44, archived recove
after backup, logseq 45, current recove
通过不完全恢复解决用户的误操作:
SQL> archive log list
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('before backup');
SQL> commit;
备份:
RMAN> backup database tag 'weekend_DB_full_backup';
备份后:
SQL> insert into t1 values ('after backup, before delete');
SQL> commit;
误操作:
SQL> select sysdate from dual;
SQL> select dbms_flashback.get_system_change_number from dual;
SQL> delete t1;
SQL> commit;
SQL> create table after_delete (x int); 正确的操作
SQL> insert into after_delete values (1);
SQL> commit;
恢复:
RMAN> run {
startup force mount;
set until scn= 1963417;
restore database;
recover database;
alter database open resetlogs;
}
set until time=’2015-10-26 11:13:23’; 基于时间点恢复
SQL> select * from t1;
SQL> select * from after_delete; 丢失
SQL> select group#, sequence#, status, archived from v$log;
通过不完全恢复解决归档日志不连续:
SQL> archive log list
备份前:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('before backup');
SQL> commit;
备份:
RMAN> backup database tag 'weekend_DB_full_backup';
备份后:
SQL> select GROUP#, SEQUENCE#, STATUS, archived from v$log;
SQL> insert into t1 values ('after backup, logseq 1, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 2, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 3, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 4, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 5, current');
SQL> commit;
SQL> alter system checkpoint; //检查点,确定
故障:
SQL> shutdown abort
$ rm /u01/app/oracle/oradata/orcl/users01.dbf //数据文件
$ rm /u01/app/oracle/fast_recovery_area/ORCL/archivelog/2016_03_01/o1_mf_1_5_cfbcxo84_.arc //归档日志
恢复:
SQL> startup
先尝试只恢复一个数据文件失败。 //n-1: 删除的归档日志序列号为n,则恢复到n-1
RMAN> run {
startup force mount;
set until sequence 5;
restore database;
recover database;
alter database open resetlogs;
}
SQL> select * from t1;
控制文件
丢失部分控制文件:
SQL> select * from v$controlfile;
$ >/u01/app/oracle/oradata/orcl/control01.ctl //将控制文件重定向。为了破坏控制文件
SQL> select * from v$tablespace; 报错
SQL> alter system checkpoint; 报错
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log //警告日志,查看错误
SQL> shutdown abort
SQL> startup nomount
SQL> show parameter control_files //查看控制文件的参数,从中找到路径
$ cp /home/oracle/control02.ctl /u01/app/oracle/oradata/orcl/control01.ctl //将之前做的镜像覆盖被破坏的控制文件
SQL> alter database mount;
SQL> alter database open;
丢失全部控制文件(有自动备份):
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON; //开启自动备份
RMAN> backup datafile 4;
$ >/u01/app/oracle/oradata/orcl/control01.ctl
$ >/u01/app/oracle/fast_recovery_area/orcl/control02.ctl
SQL> select * from v$tablespace; 报错
SQL> alter system checkpoint; 报错
SQL> shutdown abort
SQL> startup nomount
RMAN> restore controlfile from autobackup;
RMAN> alter database mount;
RMAN> recover database;
RMAN> alter database open resetlogs;
丢失全部控制文件(没有自动备份):
SQL> alter database backup controlfile to '/home/oracle/control.bak';
SQL> alter database backup controlfile to trace;
SQL> select * from v$diag_info;
spfile
有自动备份:
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> backup datafile 4;
联机恢复:
$ mv $ORACLE_HOME/dbs/spfileorcl.ora $ORACLE_HOME/dbs/spfileorcl.ora.bak
SQL> alter system set resource_limit=true; 报错
SQL> create spfile='/home/oracle/spfile.bak' from memory;
$ mv /home/oracle/spfile.bak $ORACLE_HOME/dbs/spfileorcl.ora
脱机恢复:
SQL> shutdown immediate
$ mv $ORACLE_HOME/dbs/spfileorcl.ora $ORACLE_HOME/dbs/spfileorcl.ora.bak
RMAN> startup
RMAN> restore spfile from '/u01/app/oracle/fast_recovery_area/ORCL/autobackup/2015_10_26/o1_mf_s_894118741_c2vkgo8x_.bkp';
RMAN> startup force
没有备份:
利用alert_orcl.log中的参数值,构造initorcl.ora
SQL> create spfile='/home/oracle/spfile.bak' from pfile;
利用备份init.ora:
$ vi /u01/app/oracle/product/11.2.0/db_1/dbs/init.ora 完善参数
SQL> startup pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/init.ora'
SQL> create spfile from pfile;