物理热备:即数据库处于开启时备份,但前提必须要开启归档,因为只有开归档,数据库恢复时才能应用日志将数据库恢复到最新;物理热备可以备份单个数据文件、表空间及整个数据库;物理热备,分普通表空间(非关键数据文件)备份恢复和系统表空间(关键数据文件)备份恢复。
系统表空间:指的是system、sysaux、undo表空间;
一:普通表空间的热备及模拟故障恢复(方法一之关库恢复shutdown)
1. (前提)物理热备必须开启归档,查看:
SYS@ORA11GR2>archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 13
Next log sequence to archive 15
Current log sequence 15
SYS@ORA11GR2>
——确认快速恢复区已设置好:
SYS@ORA11GR2>show parameter recover
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/FRA/
db_recovery_file_dest_size big integer 3G
db_unrecoverable_scn_tracking boolean TRUE
recovery_parallelism integer 0
SYS@ORA11GR2>
2. 查看备份文件绝对路径(重要)
SYS@ORA11GR2>select name from v$controlfile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/control01.ctl
/u01/app/oracle/oradata/ORA11GR2/control02.ctl
/u01/app/FRA/control03.ctl
SYS@ORA11GR2>select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/system01.dbf
/u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
/u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
/u01/app/oracle/oradata/ORA11GR2/users01.dbf
/u01/app/oracle/oradata/ORA11GR2/example01.dbf
/u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
/u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
SYS@ORA11GR2>select member from v$logfile;
MEMBER
----------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/redo03.log
/u01/app/oracle/oradata/ORA11GR2/redo02.log
/u01/app/oracle/oradata/ORA11GR2/redo01.log
/u01/app/oracle/oradata/ORA11GR2/redo01_a.log
/u01/app/oracle/oradata/ORA11GR2/redo02_a.log
/u01/app/oracle/oradata/ORA11GR2/redo03_a.log
/u01/app/FRA/redo01_b.log
/u01/app/FRA/redo02_b.log
/u01/app/FRA/redo03_b.log
9 rows selected.
3. 模拟备份表空间TS_ORA11GR2
——先查看数据库的表空间及对应文件
SYS@ORA11GR2>select tablespace_name,file_name from dba_data_files;
TABLESPACE FILE_NAME
---------- ------------------------------------------------------------
USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
——开始表空间TS_ORA11GR2备份:(没有关库,即热备)
SYS@ORA11GR2>alter tablespace TS_ORA11GR2 begin backup;
Tablespace altered.
4. 回到操作系统层copy 表空间TS_ORA11GR2
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ cp ts_ora11gr2_01.dbf /home/oracle
[oracle@wang ORA11GR2]$
[oracle@wang ~]$ ls
ts_ora11gr2_01.dbf
[oracle@wang ~]$
5. 结束表空间TS_ORA11GR2备份
SYS@ORA11GR2>alter tablespace TS_ORA11GR2 end backup;
Tablespace altered.
验证是否处于备份:
SYS@ORA11GR2>select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- ---------
1 NOT ACTIVE 0
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 NOT ACTIVE 0
6 NOT ACTIVE 1543727 27-SEP-16
7 NOT ACTIVE 0
(说明备份已完成,结束)
7 rows selected.
SYS@ORA11GR2>select FILE_ID,TABLESPACE_NAME,FILE_NAME from dba_data_files order by 1;
FILE_ID TABLESPACE FILE_NAME
---------- ---------- -------------------------------------------------------
1 SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
2 SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
3 UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
4 USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
5 EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
6 TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
7 UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
6. 操作删除TS_ORA11GR2表空间下的数据文件:
[oracle@wang ORA11GR2]$ rm ts_ora11gr2_01.dbf
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
7. 一致性关闭数据库:
SYS@ORA11GR2>shutdow immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@ORA11GR2>
8.开启数据库(显示找不到ts_ora11gr2_01.dbf)
SYS@ORA11GR2>startup
ORACLE instance started.
Total System Global Area 730714112 bytes
Fixed Size 2256832 bytes
Variable Size 457179200 bytes
Database Buffers 268435456 bytes
Redo Buffers 2842624 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf'
9.进行恢复操作:
——将备份的ts_ora11gr2_01.dbf文件cp到/u01/app/oracle/oradata/ORA11GR2/下:
[oracle@wang ~]$ pwd
/home/oracle
[oracle@wang ~]$
[oracle@wang ~]$ ls
ts_ora11gr2_01.dbf
[oracle@wang ~]$
[oracle@wang ~]$ cp ts_ora11gr2_01.dbf /u01/app/oracle/oradata/ORA11GR2/
[oracle@wang ~]$
[oracle@wang ORA11GR2]$ ls ts_ora11gr2_01.dbf
ts_ora11gr2_01.dbf
——恢复表空间:
SYS@ORA11GR2> select status from v$instance;
STATUS
------------
MOUNTED
SYS@ORA11GR2>recover tablespace TS_ORA11GR2
Media recovery complete.
(recover的动作,就是应用日志实现实例恢复到最近时间点)
——将数据库启动OPEN状态:
SYS@ORA11GR2>alter database open;
Database altered.
完成!!!!!!!!!!!!
同类操作后台日志记录内容如下:
alert.txt
二:普通表空间的热备及模拟故障恢复(方法二之在线恢复offline-recover-online)
1.查看热备是否处于归档模式:
SYS@ORA11GR2>archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 22
Next log sequence to archive 24
Current log sequence 24
SYS@ORA11GR2>
2.查看备份文件的绝对路径:
SYS@ORA11GR2>select member from v$logfile;
MEMBER
-----------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/redo03.log
/u01/app/oracle/oradata/ORA11GR2/redo02.log
/u01/app/oracle/oradata/ORA11GR2/redo01.log
/u01/app/oracle/oradata/ORA11GR2/redo01_a.log
/u01/app/oracle/oradata/ORA11GR2/redo02_a.log
/u01/app/oracle/oradata/ORA11GR2/redo03_a.log
6 rows selected.
SYS@ORA11GR2>select name from v$controlfile;
NAME
-----------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/control01.ctl
/u01/app/oracle/oradata/ORA11GR2/control02.ctl
SYS@ORA11GR2>select tablespace_name,file_name from dba_data_files;
TABLESPACE_NAME FILE_NAME
-------------------------------------------------------
USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
SYS@ORA11GR2>select file_id, tablespace_name,file_name from dba_data_files order by 1;
FILE_ID TABLESPACE_NAME FILE_NAME
---------- ---------------
1SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
2SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
3UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
4USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
5EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
6TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
7UNDOTBS2 u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
SYS@ORA11GR2
3.备份数据文件users01.dbf:
——使数据库处于热备状态:(没有关库)
SYS@ORA11GR2>alter tablespace users begin backup;
Tablespace altered.
SYS@ORA11GR2>
——回到操作系统层copy对应文件到备份目录下:
[oracle@wang ORA11GR2]$ ls users01.dbf
users01.dbf
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ cp users01.dbf /home/oracle/
[oracle@wang ORA11GR2]$
[oracle@wang ~]$ ls
users01.dbf
[oracle@wang ~]$ pwd
/home/oracle
[oracle@wang ~]$
查看备份状态:
SYS@ORA11GR2>select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- -------------------
1 NOT ACTIVE 1548675 2016-09-27 16:53:00
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 ACTIVE 1585418 2016-09-27 21:57:23
5 NOT ACTIVE 0
6 NOT ACTIVE 1543727 2016-09-27 16:11:12
7 NOT ACTIVE 0
(表明备份的动作还没有结束)
7 rows selected.
SYS@ORA11GR2>select file_id,tablespace_name,file_name from dba_data_files order by 1;
FILE_ID TABLESPACE_NAME FILE_NAME
---------- ---------------
1SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
2SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
3UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
4USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
5EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
6TS_ORA11GR2 /01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
7UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
结束备份:
SYS@ORA11GR2>alter tablespace users end backup;
Tablespace altered.
查看备份状态:
SYS@ORA11GR2>select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- -------------------
1 NOT ACTIVE 1548675 2016-09-27 16:53:00
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 1585418 2016-09-27 21:57:23
5 NOT ACTIVE 0
6 NOT ACTIVE 1543727 2016-09-27 16:11:12
7 NOT ACTIVE 0
7 rows selected.
4.操作删除原目录下的user01.dbf文件:
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$ rm users01.dbf
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ ls user01.dbf
ls: user01.dbf: No such file or directory
[oracle@wang ORA11GR2]$
5.恢复数据文件:
——将数据文件offline:
SYS@ORA11GR2>alter databasedatafile '/u01/app/oracle/oradata/ORA11GR2/users01.dbf' offline;
Database altered.
——将备份目录下的备份文件copy回原目录:
[oracle@wang ~]$ pwd
/home/oracle
[oracle@wang ~]$ cp users01.dbf /u01/app/oracle/oradata/ORA11GR2/
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ ls users01.dbf
users01.dbf
[oracle@wang ORA11GR2]$
——使用recover命令进行介质恢复
SYS@ORA11GR2>recover datafile4;
Media recovery complete.
SYS@ORA11GR2>
——将表空间修改为online状态(或者可以将数据文件置于online状态)
SYS@ORA11GR2>alter tablespace users online;
Tablespace altered.
恢复成功!!!!!!!!!
alert.txt
三:系统表空间的热备及模拟故障恢复
恢复数据库只能选择关闭恢复,因为系统表空间只能online;
1.(前提)物理热备必须开启归档,查看:
SYS@ORA11GR2>archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 13
Next log sequence to archive 15
Current log sequence 15
SYS@ORA11GR2>
2.查看备份文件绝对路径(重要)
SYS@ORA11GR2>select name from v$controlfile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/control01.ctl
/u01/app/oracle/oradata/ORA11GR2/control02.ctl
/u01/app/FRA/control03.ctl
SYS@ORA11GR2>select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/system01.dbf
/u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
/u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
/u01/app/oracle/oradata/ORA11GR2/users01.dbf
/u01/app/oracle/oradata/ORA11GR2/example01.dbf
/u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
/u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
SYS@ORA11GR2>select member from v$logfile;
MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORA11GR2/redo03.log
/u01/app/oracle/oradata/ORA11GR2/redo02.log
/u01/app/oracle/oradata/ORA11GR2/redo01.log
/u01/app/oracle/oradata/ORA11GR2/redo01_a.log
/u01/app/oracle/oradata/ORA11GR2/redo02_a.log
/u01/app/oracle/oradata/ORA11GR2/redo03_a.log
/u01/app/FRA/redo01_b.log
/u01/app/FRA/redo02_b.log
/u01/app/FRA/redo03_b.log
9 rows selected.
3.查看数据库的表空间及对应文件
SYS@ORA11GR2>select tablespace_name,file_name from dba_data_files;
TABLESPACE FILE_NAME
---------- -------------------------------------------------------
USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
4.备份系统表空间:
SYS@ORA11GR2>alter tablespace SYSTEM begin backup;
Tablespace altered.
SYS@ORA11GR2>
——查看备份状态:
SYS@ORA11GR2>select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- ---------
1 ACTIVE 1548675 27-SEP-16
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 NOT ACTIVE 0
6 NOT ACTIVE 1543727 27-SEP-16
7 NOT ACTIVE 0
7 rows selected.
SYS@ORA11GR2>select FILE_ID,TABLESPACE_NAME,FILE_NAME from dba_data_files order by 1;
FILE_ID TABLESPACE FILE_NAME
---------- ---------- -------------------------------------------------------
1 SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
2 SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
3 UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
4 USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
5 EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
6 TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
7 UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
5.回到操作系统层copy 表空间system:
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$ cp system01.dbf /home/oracle/
[oracle@wang ORA11GR2]$cd
[oracle@wang ~]$ pwd
/home/oracle
[oracle@wang ~]$ ls system01.dbf
system01.dbf
[oracle@wang ~]$
6.结束备份:
SYS@ORA11GR2>alter tablespace system end backup;
Tablespace altered.
——查看备份状态:
SYS@ORA11GR2>select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- ---------
1 NOT ACTIVE 1548675 27-SEP-16
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 NOT ACTIVE 0
6 NOT ACTIVE 1543727 27-SEP-16
7 NOT ACTIVE 0
7 rows selected.
SYS@ORA11GR2>select FILE_ID,TABLESPACE_NAME,FILE_NAME from dba_data_files order by 1;
FILE_ID TABLESPACE FILE_NAME
---------- ---------- -------------------------------------------------------
1 SYSTEM /u01/app/oracle/oradata/ORA11GR2/system01.dbf
2 SYSAUX /u01/app/oracle/oradata/ORA11GR2/sysaux01.dbf
3 UNDOTBS1 /u01/app/oracle/oradata/ORA11GR2/undotbs01.dbf
4 USERS /u01/app/oracle/oradata/ORA11GR2/users01.dbf
5 EXAMPLE /u01/app/oracle/oradata/ORA11GR2/example01.dbf
6 TS_ORA11GR2 /u01/app/oracle/oradata/ORA11GR2/ts_ora11gr2_01.dbf
7 UNDOTBS2 /u01/app/oracle/oradata/ORA11GR2/undotbs2_01.dbf
7 rows selected.
SYS@ORA11GR2>
8. 操作删除system表空间下的数据文件
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$ ls system01.dbf
system01.dbf
[oracle@wang ORA11GR2]$ rm system01.dbf
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ ls system01.dbf
ls: system01.dbf: No such file or directory
[oracle@wang ORA11GR2]$
——关库:(模拟故障)
SYS@ORA11GR2>shutdown immediate;
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.
9. 启动数据库:(显示故障)
SYS@ORA11GR2>startup
ORACLE instance started.
Total System Global Area 730714112 bytes
Fixed Size 2256832 bytes
Variable Size 457179200 bytes
Database Buffers 268435456 bytes
Redo Buffers 2842624 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u01/app/oracle/oradata/ORA11GR2/system01.dbf'
9.将表空间置于offline进行恢复
SQL> select status from v$instance;
STATUS
------------
MOUNTED
SYS@ORA11GR2>alter tablespace system offline;
alter tablespace system offline
*
ERROR at line 1:
ORA-01541: system tablespace cannot be brought offline; shut down if necessary
(system等系统表空间不能置于offline下进行恢复,必须关库后恢复)
SYS@ORA11GR2>shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@ORA11GR2>
——将备份的system01.dbf复制到/u01/app/oracle/oradata/ORA11GR2/下
[oracle@wang ~]$ pwd
/home/oracle
[oracle@wang ~]$ ls
system01.dbf ts_ora11gr2_01.dbf
[oracle@wang ~]$
[oracle@wang ~]$ cp system01.dbf /u01/app/oracle/oradata/ORA11GR2/
验证:
[oracle@wang ORA11GR2]$ pwd
/u01/app/oracle/oradata/ORA11GR2
[oracle@wang ORA11GR2]$
[oracle@wang ORA11GR2]$ ls system01.dbf
system01.dbf
[oracle@wang ORA11GR2]$
——再次启动数据库
SYS@ORA11GR2>startup
ORACLE instance started.
Total System Global Area 730714112 bytes
Fixed Size 2256832 bytes
Variable Size 457179200 bytes
Database Buffers 268435456 bytes
Redo Buffers 2842624 bytes
Database mounted.
ORA-01113: file 1 needs media recovery
ORA-01110: data file 1: '/u01/app/oracle/oradata/ORA11GR2/system01.dbf'
——恢复表空间system;(热备开启了归档,所以可以recover)
SYS@ORA11GR2>recover tablespace system
Media recovery complete.
SYS@ORA11GR2>select status from v$instance;
STATUS
------------
MOUNTED
SYS@ORA11GR2>alter database open;
Database altered.
SYS@ORA11GR2>select status from v$instance;
STATUS
------------
OPEN
完成!!!!!!!!!!!!!!!!
alert.txt