备份语法
backup database
backup 'datafile' //或者bacakup 4,5(数字为编号)select file_id, file_name from dba_data_files; 查看
backup tabalespace XX
RMAN备份
不归档 归档
online offline online offline
完全部分完全部分 完全部分完全部分
不归档下只能脱机状态进行完全备份,其他情况都可能丢失数据
归档状态下任何备份都不会出现问题
不归档情况下完全备份只能在数据库mount下才能备份
shutdown nomount mount open
备份数据文件:
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,浏览器查看备份
增量备份:
1级别备份分为1c和1d 其中1c为记录上次0级别全备份之后到当前所有的备份,1d为记录上次1d到当前1d备份
RMAN> backup incremental level 0 tablespace users;
RMAN> list backup of tablespace users;
SQL> create table t1(x int) tablespace users;
SQL> insert into t1 values (1);
SQL> commit;
RMAN> backup incremental level 1 tablespace users;//level 1 默认为1d
RMAN> backup incremental level 1 tablespace users;//两次1d之间没有操作 则不记录,只存本身的头文件
SQL> create table t2(x int) tablespace users;
SQL> insert into t2 values (1);
SQL> commit;
RMAN> backup incremental level 1 cumulative tablespace users;//level 1 cumulative 为1c
RMAN> list backup of tablespace users;
开启块跟踪://频繁开启增量备份时 建议开启块跟踪,只扫描增量变化的数据
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级备份//第一次即为拷贝镜像文件 系统会监测以前有没有0级别备份,如果没有系统会强制改成0,所以写1即可 'update为标签 以后再创建的备份需要一致
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级备份 生成1d类备份集
RMAN> list backup; backupset格式
RMAN> recover copy of tablespace users with tag 'update_copy';//将1d备份回复到0级别镜像拷贝文件上
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;
备份的维护:
查看:
RMAN> list backup;
RMAN> list copy;
RMAN> list backup of tablespace users;
RMAN> list backup of datafile 4;
RMAN> list archivelog all;
检查备份:
RMAN> delete backup; 删除所有备份
RMAN> delete copy;
RMAN> list backup; list copy;
RMAN> report need backup; 根据策略检查
RMAN> backup tablespace users;
RMAN> report need backup;
RMAN> CONFIGURE RETENTION POLICY TO REDUNDANCY 2;
RMAN> report need backup;//查看
RMAN> CONFIGURE RETENTION POLICY clear;
CONFIGURE RETENTION POLICY 2//默认1 修改冗余命令
RMAN> report obsolete;
list copy //查看镜像拷贝
删除备份:
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> 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;//恢复backupset
RMAN> list backup; backupset恢复