RMAN笔记

1. 连接RMAN

SQL> create user rman identified by jansondors;
SQL> grant resource,connect,dba to rman;
$ rman target rman/jansondors

2. 配置RMAN参数

RMAN> show all;

using target database control file instead of recovery catalog
RMAN configuration parameters for database with db_unique_name ORACENT are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # 保留备份副本的数量
CONFIGURE BACKUP OPTIMIZATION OFF; # 配置备份优化,如果已经备份了某个文件的相同版本,则不会再备份该文件
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # 备份的数据文件保留至服务器磁盘上
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # 配置是否启用控制文件的自动备份
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # 配置控制文件自动备份的格式
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # 备份并行度,通道数量越多,任务执行时间越短;备份文件类型为备份集
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/data/oracle/product/11.2.0/db_1/dbs/snapcf_oracent.f'; # default

RMAN> configure default device type to sbt;
RMAN> configure default device type to disk;
RMAN> configure backup optimization on;
RMAN> configure controlfile autobackup on;

-> 备份路径遵循的原则
statement format ->configure format ->FLASH_RECOVER_ARER ->$ORACLE_HOME/dbs

3. 配置RMAN脱机备份

$ sqlplus rman/jansondors as sysdba
SQL> select file_type,percent_space_used,percent_space_reclaimable,number_of_files from v$flash_recovery_area_usage;
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter system set db_recovery_file_dest_size=10g;  
SQL> show parameter db_recovery_file_dest;

RMAN> backup database;
RMAN> backup as compressed backupset database;
RMAN> sql 'alter database open';

4. 配置RMAN联机备份

  • 开启归档
$ sqlplus rman/jansondors as sysdba
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database archivelog/noarchivelog;
SQL> alter database open;
SQL> archive log list;
  • 备份数据库
RMAN> backup as compressed backupset database plus archivelog delete all input;

RMAN> run{
          allocate channel ch1 device type disk format '/home/oracle/Downloads/backup_ctl_file/ch1_%U';
          allocate channel ch2 device type disk format '/home/oracle/Downloads/backup_ctl_file/ch2_%U';
          backup as backupset
          (datafile 1,4 channel ch1)
          (datafile 2,3,5 channel ch2);
          sql 'alter system archive log current';}
          
RMAN> run{
          allocate channel ch1 device type disk;
          allocate channel ch2 device type disk;
          backup as backupset format '/home/oracle/Downloads/backup_ctl_file/%U'
          (datafile 1,4 channel ch1)
          (datafile 2,3,5 channel ch2);
          sql 'alter system archive log current';}
  • 备份表空间
RMAN> backup tablespace users;
RMAN> backup as compressed backupset tablespace users;
  • 备份数据文件
RMAN>  backup as backupset datafile 1 format '/home/oracle/Downloads/backup_ctl_file/datafile_1_%U';


  • 备份控制文件
->手工备份
RMAN> backup current controlfile format '/home/oracle/Downloads/backup_ctl_file_%U.dbf';
RMAN> backup current controlfile ;

->自动备份
RMAN> configure controlfile autobackup format for device type disk to '/home/oracle/Downloads/backup_ctl_file/%F';
RMAN> configure controlfile autobackup on ;
  • 备份坏块处理
默认情况下,RMAN会检查数据库是否发生物理损坏,不会检查逻辑损坏
->关闭物理损坏检查
RMAN> backup nochecksum tablespace users tag='weekly backup';
->启用逻辑损坏检查
RMAN> backup check logical tablespace users;
->设置maxcorrupt,当坏块个数超过指定数量时,backup进程会停止
RMAN-> run{
           set maxcorrupt for datafile 2,4 to 10;
           backup database;}

5. 配置RMAN增量备份

  • Level 0增量备份
RMAN> backup incremental level 0 database;
  • Level 1增量备份
RMAN> backup incremental level 1 database;
  • 快速增量备份
SQL> shutdown immediate;
SQL> startup mount;

SQL> select filename,status,bytes from v$block_change_tracking;
SQL> alter database enable block change tracking using file '/data/oracle/product/11.2.0/oradata/chtrack.log';
SQL> alter database disable block change tracking;
SQL> alter database rename file '/data/oracle/product/11.2.0/oradata/chtrack.log' to '/data/oracle/product/11.2.0/oradata/test/chtrack.log';
  • 应用增量备份
# 将增量备份添加到镜像副本上

RMAN> run{
          backup incremental level 1 for recover of copy with tag 'incr_copy_backup' database;
          recover copy of database with tag 'incr_copy_backup';}

6. 配置恢复目录

SQL> create tablespace rcat_tbs datafile '/data/oracle/oradata/ORACENT/rcat_tbs01.dbf' size 100m;
SQL> create user rcat_owner identified by jansondors default tablespace rcat_tbs temporary tablespace temp;
SQL> grant recovery_catalog_owner to rcat_owner;
SQL> grant connect,resource to rcat_owner;

RMAN> rman catalog rman/jansondors target systemn/jansondors@targetOrcl

7. RMAN脚本

create script rman_backup{
 sql 'alter system checkpoint';
 backup database format '/home/oracle/Downloads/offline_backup/backup_$u.dbf';
 backup current controlfile format '/home/oracle/Downloads/offline_backup/backup_ctl_$u.dbf';
}

8. 非归档完全恢复

联机重做日志是循环使用的,一个日志写满之后会切换到下一个,新的循环会覆盖掉部分变化的数据,非归档恢复是一种不完全恢复

  • 数据文件、控制文件以及重做日志文件全部丢失
# 数据库处于非归档模式
$ sqlplus rman/jansondors as sysdba
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database noarchivelog;
SQL> alter database open;
SQL> archive log list;

# 备份文件存储目录为默认的快闪恢复区
RMAN> configure controlfile autobackup format for device type disk clear;
RMAN> show all;

# 开启控制文件自动备份
RMAN> configure controlfile autobackup on;
RMAN> show all;

# 控制文件、数据文件、重做日志默认位置
SQL> show parameter control_files;
SQL> col name for a30
SQL> select file#,name,status from v$datafile;
SQL> select group#, status, member from v$logfile;

-> 1. 数据库开启挂载模式
SQL> startup mount;

-> 2. 新增测试数据
SQL> alter database open;
SQL> create table test123 as select * from dba_segments;

-> 3. 模拟文件丢失
SQL> shutdown immediate;
$ pwd
$ ls    
control01.ctl  redo02.log  sysaux01.dbf  temp01.dbf     users01.dbf
redo01.log     redo03.log  system01.dbf  undotbs01.dbf
$ rm -rf *.*

-> 4. 恢复数据
SQL> startup
ORACLE instance started.
Total System Global Area  759943168 bytes
Fixed Size          2217224 bytes
Variable Size         503319288 bytes
Database Buffers      251658240 bytes
Redo Buffers            2748416 bytes
ORA-00205: error in identifying control file, check alert log for more info
RMAN> restore controlfile from '/data/oracle/flash_recovery_area/ORACENT/autobackup/2017_05_17/o1_mf_s_944236355_dkr0s40f_.bkp';
SQL> alter database mount;
Database altered.
RMAN> restore database;     
RMAN> recover database noredo;
SQL> alter database open resetlogs;
SQL> select group#,sequence#,status from v$log;
SQL> select count(*) from test123;
  • 数据文件丢失
-> 1. 模拟USERS表空间丢失
SQL> shutdown immediate
$ pwd
/data/oracle/oracent
$ ls
control01.ctl  redo01.log  redo02.log  redo03.log  sysaux01.dbf  system01.dbf  temp01.dbf  undotbs01.dbf  users01.dbf
$ rm -rf users01.dbf
-> 2. RMAN恢复
SQL> select file#,name from v$datafile;
RMAN> restore datafile 4;
RMAN> recover datafile 4;
#重做日志被覆盖
RMAN> recover datafile 4 until cancel;
  • 重做日志文件丢失
-> 1. 模拟文件丢失
SQL> shutdown immediate;
$ pwd
/data/oracle/oracent
$ ls
control01.ctl  redo01.log  redo02.log  redo03.log  sysaux01.dbf  system01.dbf  temp01.dbf  undotbs01.dbf  users01.dbf
$ rm -rf *.dbf
$ rm -rf *.log
-> 2. 恢复
RMAN> restore database;
RMAN> recover database until cancel;
SQL> alter database open;
SQL> alter database open resetlogs;
  • 迁移数据文件
SQL> start mount
RMAN> run{
    set newname for datafile
    'A/system01.dbf' to 'B/system01.dbf';
    set newname for datafile
    'A/users01.dbf' to 'B/users01.dbf';
    restore database from tag=TAG20170517T131491;
    switch datafile all;
}

9. 归档完全恢复

数据库一直处于归档模式下,且归档文件和重做日志文件损坏的情况下,可以在联机状态下恢复数据库文件

  • 非系统表空间损坏
-> 1. 模拟环境
$ pwd
/data/oracle/oracent
$ ls
control01.ctl  redo01.log  redo02.log  redo03.log  sysaux01.dbf  system01.dbf  temp01.dbf  undotbs01.dbf  users01.dbf
$ rm -rf users01.dbf

-> 2. 恢复
SQL> alter database datafile 4 offline;
SQL> alter database open;
RMAN> restore datafile 4;
RMAN> recover datafile 4;
SQL> alter database datafile 4 online;
SQL> select file_name,tablespace_name,status,online_status from dba_data_files;
RMAN> run{
    sql 'alter database datafile 4 offline';
    restore tablespace users;
    recover tablespace users;
    sql 'alter database datafile 4 online';
}

  • 系统表空间损坏
# system表空间损坏时,数据库无法启动;需要把数据库启动至mount状态,使用RMAN进行数据库恢复工作
SQL> startup mount
RMAN> run{
    sql 'alter database datafile 1 offline';
    restore datafile 1 ;
    recover datafile 1;
    sql 'alter database datafile 1 online';
}
SQL> alter database open;

  • 所有数据文件丢失
# 在所有数据文件丢失,但控制文件和重做日志文件都完好的情况下,可以通过RMAN进行数据库恢复工作
RMAN> run{
    restore database;
    recover database;
    sql 'alter database open';
}

10. RMAN恢复数据块

-> 1. 备份整个数据库
RMAN> backup database plus archivelog;

-> 2. 模拟环境
SQL> shutdown immediate;
# 人为修改dbf文件
# 此时startup指令无效,提示报错

-> 3. 恢复
RMAN> backup validate datafile 6;
SQL> select * from v$database_block_corruption;
RMAN> blockrecover datafile 6 block #BLOCK_NUM# from backupset;
RMAN> recover datafile 6;
SQL> alter database open;

11. RMAN备份维护指令

  • validate backupset
RMAN> validate backupset 5;
  • restore...validate
RMAN> restore tablespace users validate;
RMAN> restore datafile '/data/oracle/oracent/system01.dbf' validate;
  • restore preview
RMAN> restore database preview
RMAN> restore tablespace sysaux preview;
RMAN> restore datafile 5 preview;
  • list
RMAN> list;
RMAN> list backupset;
RMAN> list backupset 5;
RMAN> list backup of tablespace users;
RMAN> list backup of datafile 1;
RMAN> list backup of archivelog all;
RMAN> list backup of archivelog from time ='sysdate-2';
RMAN> list backup of controlfile;
RMAN> list backup of spfile;
RMAN> list copy of controlfile;
RMAN> list backup summary;
  • report
RMAN> report schema;
RMAN> report need backup;

你可能感兴趣的:(RMAN笔记)