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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容