天天看点

Rman单机备份到恢复详细过程

备份数据库A

备份脚本:

rman target / log=/u01/rec_backup.log<<EOF

run

{

allocate channel ch1 device type disk;  ----开启通道

allocate channel ch2 device type disk;

sql 'alter system archive log current';  ----切归档日志

backup database format '/u01/RMAN/backup/db_%d_%T_%U';  ----备份的数据文件的路径

sql 'alter system archive log current';   ----切归档日志

backup archivelog all format '/u01/RMAN/backup/arch_%t_%s' delete all input;  -----备份归档日志并删除归档归档(当然也可以不删)

backup format '/u01/RMAN/backup/con_%s_%p' current controlfile;   ----备份控制文件

crosscheck backup; ----检查备份

crosscheck archivelog all;  -----检查归档(为什么要检查,因为检查了一下的命令才可以使用)

delete noprompt expired backup; ----删除过期备份

delete noprompt obsolete;   -----删除过期文件

delete noprompt backup of database completed before 'sysdate -15';  -----删除15天前的备份

delete noprompt archivelog all; ----删除过期归档

delete noprompt backup of archivelog all completed before 'sysdate -15';  -----删除15天前的归档

release channel ch1;  ----释放通道

release channel ch2; -----释放通道

}

EOF

移动备份文件

移动备份脚本里的数据文件、控制文件、归档日志文件通过scp命令移动到数据库B指定目录下

创建pfile

在数据库A,mount下,通过spfile创建pfile(create spfile from pfile),到cd $ORACLE_HOME/dbs下找到刚创建的pfile文件,

SCP移动到数据库B上的$ORACLE_HOME/dbs下,数据库恢复的时候启库要用

移动环境变量

把数据库A上的ORACLE的环境变量复制到数据库B上,source .bash_profile 让环境变量生效

检查控制文件

检查控制文件中路径是否都创建,要拉起到nomount了

通过spfile启库

数据库B上通过从数据库A拉过来的spfile启库到nomount,startup nomount pfile =’xx/xx/xxinit.ora’; 需要指定pfile路径

恢复控制文件

恢复从数据库A上SCP过来的控制文件

先进入rman

Rman target /

恢复:restore controlfile from ‘xx/wwe/控制文件名’;

并数据库启动到mount;  alter database mount;

恢复数据文件

注册备份集

在跑RMAN恢复数据文件之前需要先注册一下,注册备份集 ,从数据库A拷贝过来的数据文件,归档日志文件,需要注册到数据库B中,让数据库B知道,这些是恢复文件

catalog start with '/u01/backup/';

会读到你拷贝的数据文件

写rman恢复脚本

rman target / log=/u01/recover/rec_recover.log<<EOF

run

{

allocate channel ch1 device type disk; 

allocate channel ch2 device type disk;

set until time "to_date('2018-09-12 18:50:49', 'YYYY-MM-DD HH24:MI:SS')";

set newname for datafile 1 to "/u01/app/oracle/oradata/arorcl/datafile/system01.dbf";

set newname for datafile 2 to "/u01/app/oracle/oradata/arorcl/datafile/sysaux01.dbf";

set newname for datafile 3 to "/u01/app/oracle/oradata/arorcl/datafile/undotbs01.dbf";

set newname for datafile 4 to "/u01/app/oracle/oradata/arorcl/datafile/users01.dbf";

set newname for tempfile 1 to "/u01/app/oracle/oradata/arorcl/datafile/temp01.dbf";

restore database;

switch datafile all;

switch tempfile all;

recover database;

release channel c1;

release channel c2;

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo03.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo03.log'' ";

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo02.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo02.log'' ";

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo01.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo01.log'' ";

sql 'alter database clear logfile group 1';

sql 'alter database clear logfile group 2';

sql 'alter database clear logfile group 3';

}

quit;

EOF

我是指定时间点恢复,时间点是通过select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual; 这条指令查询出来,后期可能恢复会有问题,归档追不到情况,会解决,看下文

运行rman脚本,nohup sh rman.sh &

Tail –f rec_rman.sh :查看进行恢复的日志

在查看恢复日志过程中结束后看到日志里报错

追不到SCN OF 1155225的日志

查看控制文件里归档日志记录信息

最下面一个是未追到的,那可以试着网上跳一个,选择1155217恢复,指定SCN的恢复方法

恢复脚本:

rman target / log=/u01/recover/rec_recover1.log<<EOF

run

{

allocate channel ch1 device type disk; 

allocate channel ch2 device type disk;

set until scn 1155217;

recover database;

release channel c1;

release channel c2;

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo03.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo03.log'' ";

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo02.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo02.log'' ";

SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo01.log''  to  ''/u01/app/oracle/oradata/arorcl/redo/redo01.log'' ";

sql 'alter database clear logfile group 1';

sql 'alter database clear logfile group 2';

sql 'alter database clear logfile group 3';

}

quit;

EOF

继续报错

从英文字面上翻译是,datafile 1必须恢复比SCN 1155217旧的数据文件,翻过来说,其实已经恢复到1155217了

那接下去可以查下数据表头和数据文件中数据文件的信息

select a.name,a.checkpoint_change# “start_SCN”,

    > b.checkpoint_change# “last_SCN”

    > from v$datafile_header a, v$datafile b

> where a.file#=b.file#;

这里解释下关于oracle动态视图v$datafile和v$datafile_header

v$datafile是从oracle的控制文件中获得的数据文件的信息

v$datafile_header是从数据文件的头部

在正常运行下,两者的检查点SCN值是一致的,但当datafile出现损坏时可以用v$datafile_header确定应用重做的起始SCN,而v$datafile可确定应用重做的结束SCN值

查询结果信息数据一致,可以开库,但我redo路径改了,还是需要先在sql里改下路径

ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo01.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo01.log';

ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo02.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo02.log';

ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo03.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo03.log';

alter database clear logfile group 1;

alter database clear logfile group 2;

alter database clear logfile group 3;

开库

Alter database open resetlogs;

单个恢复

run{

set newname for datafile 1 to "+DATAGR/arorcl/DATAFILE/system01.dbf";

restore datafile 1;

}

run{

set newname for datafile 2 to "+DATAGR/arorcl/DATAFILE/sysaux01.dbf";

restore datafile 2;

}

run{

set newname for datafile 3 to "+DATAGR/arorcl/DATAFILE/undotbs01.dbf";

restore datafile 3;

}

run{

set newname for datafile 4 to "+DATAGR/arorcl/DATAFILE/users01.dbf";

restore datafile 4;

}

run{

set newname for tempfile 1 to "+DATAGR/arorcl/DATAFILE/temp01.dbf";

restore tempfile 1;

}

单个Switch all

run{

set newname for datafile 1 to "+DATAGR/arorcl/DATAFILE/system01.dbf";

switch datafile all;

}