Oracle中RMAN的备份与恢复笔记

环境:CentOS6 + Oracle11g


Linux下先确认输入rman时,执行是那个bash

find / -name rman (根目录下rman名称的文件)

su -oracle

echo $PATH

vi ./.bash_profile (调整顺序为 PATH=$PATH:$HOME/bin)


确认为归档模式

sqlplus /nolog

conn /as sysdba

archive log list

alter system set log_archive_start=true scope=spfile; (设置自动归档)


nocatalog非目录模式下的备份(备份信息存放在控制文件)

[oracle@CentOS6 ~]$ rman nocatalog

RMAN> connect target /(或者RMAN> connect target sys/samis.com@poli)

RMAN> list backupset;

RMAN> backup database;(全备份)

----------------------------------------------------------------

[oracle@CentOS6 ~]$ cd /u01/oracle/oradata/poli/

[oracle@CentOS6 ~]$ strings control01.ctl (查看控制文件内容)


RMAN> backup incremental level=0 database; (0级备份)

RMAN> backup incremental level 1 database; (1级备份)

RMAN> list backupset;

----------------------------------------------------------------


备份归档日记archivelog

RMAN> backup database plus archivelog delete input;(备份了所有文件,参数/控制/数据/归档,并删除已备份的归档日记文件)

RMAN> show all;

RMAN> report schema; (查看表空间)

RMAN> backup tablespace tbs01


备份控制文件,两种方式:

RMAN> backup current controlfile (单独备份)

RMAN> backup database include current controlfile (备份数据库的时候包含控制文件)


备份集: backupset

镜像备份:image copies


RMAN> report schema; (查看表空间对应的编号)

RMAN> copy datafile 5 to '/u01/oracle/oradata/poli/tbs01.dbf';

RMAN> list copy; (查看镜像备份)


----------------------------------------------------------------

单命令

RMAN> backup database;

批命令

RMAN> run {

allocate channel cha1 type disk;

backup format "/u01/rmanbak/full_%t"

tag full_backup_bak

database;

release channel cha1;

}


%t:备份集时间戳

%T:年月日格式(YYYYMMDD)

%d:数据库的名称

%D:位于该月中的天数(DD)

%M:位于该月中的月份(MM)


----------------------------------------------------------------

自动备份:备份脚本+crontab

创建脚本

[oracle@CentOS6 ~]$ vi bakl0

RMAN> run {

allocate channel c1 type disk;

backup incremental level 0

format "/u01/rmanbak/inc0_%u_%T"

tag monday_inc0

database;

release channel cha1;

}

[oracle@CentOS6 ~]$ cp bakl0 bakl1

[oracle@CentOS6 ~]$ cp bakl0 bakl2

[oracle@CentOS6 ~]$ vi bakl1

[oracle@CentOS6 ~]$ rman target / msglog=/u01/rmanbak/bakl0.log cmdfile=/u01/rmanbak/script/bakl0 (可以先测试下)

[root@CentOS6 ~]$ crontab -e -u oracle

45 23 * * 0 rman target / msglog=/u01/rmanbak/bakl0.log cmdfile=/u01/rmanbak/script/bakl0 (从右到左,星期/月/日/时/分 23:45分)

45 23 * * 1 rman target / msglog=/u01/rmanbak/bakl2.log cmdfile=/u01/rmanbak/script/bakl2

45 23 * * 2 rman target / msglog=/u01/rmanbak/bakl2.log cmdfile=/u01/rmanbak/script/bakl2

45 23 * * 3 rman target / msglog=/u01/rmanbak/bakl1.log cmdfile=/u01/rmanbak/script/bakl1 (1级)

45 23 * * 4 rman target / msglog=/u01/rmanbak/bakl2.log cmdfile=/u01/rmanbak/script/bakl2 (2级)

45 23 * * 5 rman target / msglog=/u01/rmanbak/bakl2.log cmdfile=/u01/rmanbak/script/bakl2

45 23 * * 6 rman target / msglog=/u01/rmanbak/bakl2.log cmdfile=/u01/rmanbak/script/bakl2


[root@CentOS6 ~]$ service crond restart (编辑完重启这个服务)


----------------------------------------------------------------

# connected to target database: POLI (DBID=3742422830)

RMAN> rman target / (记下dbid,以后恢复spfile或者controlfile时候需要)

RMAN> configure controlfile autobackup on; (自动备份controlfile)

RMAN> show all;

RMAN> list backup;

RMAN> delete backupset 24; (删除备份编号)

RMAN> backup format '/u01/oracle/rmanbak/full_%T_%u.bak' database plus archivelog;



以下为相关oracle文件丢失后的恢复(基于以上的备份):


口令文件丢失($ORACLE_HOME/dbs目录(/u01/oracle/dbs)):

[oracle@CentOS6 dbs]$ orapwd file=orapwpoli password=samis.com entries=5


----------------------------------------------------------------

spfile丢失(/u01/oracle/dbs),改名模拟丢失:

[root@CentOS6 ~]$ mv spfilepoli sppoli

[root@CentOS6 ~]$ rman target /

RMAN> shutdown immediate;

RMAN> startup nomount;

RMAN> set dbid 3742422830;

RMAN> restore spfile from autobackup;

(如果不行,请手动指定路径

RMAN> restore spfile from '/u01/flash_recovery_area/POLI/autobackup/2014_01_03/o1_mf_s_835894765_9ddyzfkj_.bkp')

RMAN> shutdown immediate;

RMAN> startup; (如果启动失败,请在nomount下指定dbid)


----------------------------------------------------------------

controlfile控制文件丢失(/u01/oradata/poli/):

[root@CentOS6 ~]$ ls -l

[root@CentOS6 ~]$ rm *.ctl

[root@CentOS6 ~]$ rman target /

[root@CentOS6 ~]$ sqlplus /nolog;

SQL> conn /as sysdba;

SQL> shutdown abort;

[root@CentOS6 ~]$ rman target /;

RMAN> startup nomount;

RMAN> restore controlfile from autobackup;

RMAN> quit

[root@CentOS6 ~]$ ls -l

[root@CentOS6 ~]$ rman target /;

RMAN> alter database mount;

RMAN> recover database;

RMAN> alter database open resetlogs; (redo log 的scn是记录在controlfile里的)

# alter database open resetlogs; (重要:这句执行后,必须重新完整备份一次数据库)


----------------------------------------------------------------

redolog file 丢失(/u01/oradata/poli/):

linux下先删除文件,模拟丢失 (注意在sqlplus下进行恢复)

[root@CentOS6 ~]$ rm *.log

[root@CentOS6 ~]$ sqlplus /nolog;

SQL> conn /as sysdba;

SQL> shutdown immediate;

SQL> startup mount;

SQL> recover database until cancel;

SQL> alter database open resetlogs;


----------------------------------------------------------------

datafile 丢失(/u01/oradata/poli/与表空间恢复相似):

[root@CentOS6 ~]$ rman target /

RMAN> report schema;

RMAN> quit;

[root@CentOS6 ~]$ rm tbs01.dbf

[root@CentOS6 ~]$ rman target /

RMAN> report schema;

RMAN> sql "alter database datafile 5 offline";

RMAN> restore datafile 5;

RMAN> recover datafile 5;

RMAN> sql "alter database datafile 5 online";


----------------------------------------------------------------

表空间 丢失(/u01/oradata/poli/与表空间恢复相似):

[oracle@CentOS6 poli]$ sqlplus /nolog

[oracle@CentOS6 poli]$ conn /as sysdba

SQL> select owner,table_name from all_tables where tablespace_name='TBS1';

SQL> select * from user1.table1;

SQL> quit

[oracle@CentOS6 poli]$ rm tbs01.dbf

[oracle@CentOS6 poli]$ rman target /

RMAN> sql "alter tablespace tbs1 offline immediate"; (immediate为强制脱机)

RMAN> restore tablespace tbs1;

RMAN> recover tablespace tbs1;

RMAN> sql "alter tablespace tbs1 online";

RMAN> quit

[oracle@CentOS6 poli]$ ls


---------------------------------------------------------------------------------

非catalog方式完全恢复(丢失controlfile/datafile/redolog),综合以上单个恢复

[oracle@CentOS6 poli] rm *

[oracle@CentOS6 poli] ls

[oracle@CentOS6 poli] rman target /

[oracle@CentOS6 poli] sqlplus /nolog

SQL> conn /as sysdba;

SQL> shutdown abort;

SQL> quit

[oracle@CentOS6 poli] rman target /

RMAN> startup nomount;

RMAN> restore controlfile from autobackup;

RMAN> alter database mount;

RMAN> restore database;

RMAN> recover database; (有问题)

RMAN> quit

[oracle@CentOS6 poli] ls

[oracle@CentOS6 poli] sqlplus /nolog

SQL> conn /as sysdba

SQL> recover database until cancel;  (有问题,redolog丢失,默认不让recover)

[oracle@CentOS6 poli] sqlplus /nolog

SQL> conn /as sysdba

SQL> create pfile from spfile

SQL> quit

[oracle@CentOS6 poli] vi /u01/oracle/dbs/initpoli.ora

(没有redo log的日记恢复,在initpoli.ora最好添加一行_allow_resetlog_corruption='TRUE')

[oracle@CentOS6 poli] sqlplus /nolog

SQL> conn /as sysdba

SQL> shutdown immediate;

SQL> startup pfile=/u01/oracle/dbs/initpoli.ora mount;

SQL> alter database open resetlogs; (只要有这句,都是不完全回复,需要重新一次全备份)

SQL> quit

[oracle@CentOS6 poli] ls


----------------------------------------------------------------

基于时间点的恢复

run {

set until time "to_date('13/12/30 15:00:00','mm/dd/yy hh24:mi:ss')"

restore database;

recover database;

alter database open resetlogs;

}


----------------------------------------------------------------

基于SCN的恢复

RMAN> list backup;(看SCN,643309)

RMAN> shutdown immediate;

RMAN> startup mount;

RMAN> restore database until scn 643309;

RMAN> recover database until scn 643309;

RMAN> alter database open resetlogs;


----------------------------------------------------------------

基于日记序列的恢复

SQL> select * from v$log; (查看SEQUENCE是多少,current当前在使用的)

RMAN> startup mount;

RMAN> restore database until SEQUENCE 100 thread 1;

RMAN> recover database until SEQUENCE 100 thread 1;

RMAN> alter database open resetlogs;


----------------------------------------------------------------

RMAN 几条常用命令:

RMAN> report shcema; (列出当前数据库信息)

RMAN> list backup; (列出当前已备份信息)

RMAN> crosscheck database; (检查当前备份信息与系统文件是否匹配)

RMAN> delete backupset 11; (删除备份)



-----------------------------------------------------------------------------------------------

catalog mode目录模式,把备份信息放在catalog下(正确的做法)

create tablespace rman_ts datafile '/u01/oracle/poli/rmantbs.dbf' size 32M; (创建表空间)

create user rman identified by rman defalut tablespace rman_ts quota unlimited on rmantbs; (创建用户)

grant recovery_catalog_owner,connect to rman; (授权)


# 查看下相关权限

# select * from dba_sys_privs where grantee='CONNECT';

# select * from dba_sys_privs where grantee='RESOURCE';

# select * from dba_sys_privs where grantee='RECOVERY_CATALOG_OWNER';


RMAN> quit;

rman catalog rman/rman

RMAN> create catalog tablespace rmantbs; (创建恢复目录)

RMAN> register database;

RMAN> connect target / (连接目标数据)


rman target / catalog rman/rman (同时连接目标和恢复目录)

RMAN> backup format '/u01/oracle/bak/full_%T_%t.bak' database; (全备份,备份信息存放在catalog中,而不是放在控制文件里)

RMAN> list backup;






你可能感兴趣的:(oracle,linux,database,rman,System)