冷备-物理备份-在线热备-数据文件

BooBooWei - 2019.11.23

上一节课学习了物理备份-离线冷备,在数据库shutdown的情况下,手动复制物理文件到备份目录:

  • 口令文件
  • 参数文件
  • 控制文件
  • 数据文件
  • 日志文件

今天学习的是在数据库正常提供服务的情况线下,进行的备份。

物理备份-在线热备-数据文件

物理备份-在线热备份:online backup在数据库open状态下进行的备份,需要数据库运行在归档模式下

操作实践

实践01-检查数据库归档模式

Managing Archived Redo Log Files

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination /u01/app/oracle/product/11.2.0.4/dbs/arch
Oldest online log sequence 7
Current log sequence 9

思考:初始化配置数据库归档模式的方式是什么?即如何设置初始数据库归档模式?

  • 可以在CREATE DATABASE语句中将初始归档模式设置为数据库创建的一部分。

  • 建议创建数据库时,使用NOARCHIVELOG也是默认的模式,在创建数据库后,再决定是否更改初始归档模式。

  • 如果创建数据库时,指定ARCHIVELOG模式,则必须设置初始化参数,这些参数指定已归档的重做日志文件的目标位置(请参见“ 为归档目标设置初始化参数 ”)。

实践02-开启数据库归档模式

语法:更改数据库的归档模式,请使用ALTER DATABASE带有ARCHIVELOGor NOARCHIVELOG子句的语句。

权限:必须以管理员权限(AS SYSDBA)连接到数据库。

步骤:

  1. 关闭数据库实例,备份数据库
  2. 启动实例到nomount模式,更改初始化参数文件
  3. 启动实例到mount模式,更改数据库的归档模式
  4. 启动实例到open模式
  5. 关闭数据库实例,备份数据库

更改数据库归档模式将更新控制文件。更改数据库归档模式后,必须备份所有数据库文件和控制文件。以前的任何备份均已在NOARCHIVELOG模式下使用,因此不再可用。

1. 关闭数据库实例,备份数据库

[oracle@oratest ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Sat Nov 23 20:06:25 2019

Copyright (c) 1982, 2013, Oracle. All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination /u01/app/oracle/product/11.2.0.4/dbs/arch
Oldest online log sequence 7
Current log sequence 9
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

备份数据库
--此处省略备份操作,详情见上一节实践内容

2. 启动实例到nomount模式,更改初始化参数文件

修改初始化参数文件 log_archive_dest_1

[oracle@oratest ~]$ mkdir /home/oracle/arc_booboo_dest1

SQL> startup nomount
ORACLE instance started.

Total System Global Area 229683200 bytes
Fixed Size 2251936 bytes
Variable Size 171967328 bytes
Database Buffers 50331648 bytes
Redo Buffers 5132288 bytes

SQL> column name format a30
SQL> column value format a20
SQL> column issys_modifiable format a10
SQL> select name,value,issys_modifiable from v$parameter where name like 'log_archive%';

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_1 IMMEDIATE
log_archive_dest_2 IMMEDIATE
log_archive_dest_3 IMMEDIATE
log_archive_dest_4 IMMEDIATE
log_archive_dest_5 IMMEDIATE
log_archive_dest_6 IMMEDIATE
log_archive_dest_7 IMMEDIATE
log_archive_dest_8 IMMEDIATE
log_archive_dest_9 IMMEDIATE
log_archive_dest_10 IMMEDIATE
log_archive_dest_11 IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_12 IMMEDIATE
log_archive_dest_13 IMMEDIATE
log_archive_dest_14 IMMEDIATE
log_archive_dest_15 IMMEDIATE
log_archive_dest_16 IMMEDIATE
log_archive_dest_17 IMMEDIATE
log_archive_dest_18 IMMEDIATE
log_archive_dest_19 IMMEDIATE
log_archive_dest_20 IMMEDIATE
log_archive_dest_21 IMMEDIATE
log_archive_dest_22 IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_23 IMMEDIATE
log_archive_dest_24 IMMEDIATE
log_archive_dest_25 IMMEDIATE
log_archive_dest_26 IMMEDIATE
log_archive_dest_27 IMMEDIATE
log_archive_dest_28 IMMEDIATE
log_archive_dest_29 IMMEDIATE
log_archive_dest_30 IMMEDIATE
log_archive_dest_31 IMMEDIATE
log_archive_dest_state_1 enable IMMEDIATE
log_archive_dest_state_2 enable IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_state_3 enable IMMEDIATE
log_archive_dest_state_4 enable IMMEDIATE
log_archive_dest_state_5 enable IMMEDIATE
log_archive_dest_state_6 enable IMMEDIATE
log_archive_dest_state_7 enable IMMEDIATE
log_archive_dest_state_8 enable IMMEDIATE
log_archive_dest_state_9 enable IMMEDIATE
log_archive_dest_state_10 enable IMMEDIATE
log_archive_dest_state_11 enable IMMEDIATE
log_archive_dest_state_12 enable IMMEDIATE
log_archive_dest_state_13 enable IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_state_14 enable IMMEDIATE
log_archive_dest_state_15 enable IMMEDIATE
log_archive_dest_state_16 enable IMMEDIATE
log_archive_dest_state_17 enable IMMEDIATE
log_archive_dest_state_18 enable IMMEDIATE
log_archive_dest_state_19 enable IMMEDIATE
log_archive_dest_state_20 enable IMMEDIATE
log_archive_dest_state_21 enable IMMEDIATE
log_archive_dest_state_22 enable IMMEDIATE
log_archive_dest_state_23 enable IMMEDIATE
log_archive_dest_state_24 enable IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_dest_state_25 enable IMMEDIATE
log_archive_dest_state_26 enable IMMEDIATE
log_archive_dest_state_27 enable IMMEDIATE
log_archive_dest_state_28 enable IMMEDIATE
log_archive_dest_state_29 enable IMMEDIATE
log_archive_dest_state_30 enable IMMEDIATE
log_archive_dest_state_31 enable IMMEDIATE
log_archive_start FALSE FALSE
log_archive_dest IMMEDIATE
log_archive_duplex_dest IMMEDIATE
log_archive_min_succeed_dest 1 IMMEDIATE

NAME VALUE ISSYS_MODI
------------------------------ -------------------- ----------
log_archive_trace 0 IMMEDIATE
log_archive_config IMMEDIATE
log_archive_local_first TRUE IMMEDIATE
log_archive_format %t_%s_%r.dbf FALSE
log_archive_max_processes 4 IMMEDIATE

71 rows selected.

SQL> alter system set log_archive_dest_1='location=/home/oracle/orc_booboo_dest1/';

System altered.

3. 启动实例到mount模式,更改数据库的归档模式

SQL> alter database  mount
ORACLE instance started.

Total System Global Area 229683200 bytes
Fixed Size 2251936 bytes
Variable Size 171967328 bytes
Database Buffers 50331648 bytes
Redo Buffers 5132288 bytes
Database mounted.

SQL> select name,LOG_MODE from v$database;

NAME LOG_MODE
--------- ------------
BOOBOO NOARCHIVELOG

SQL> alter database archivelog;

Database altered.

SQL> select name,LOG_MODE from v$database;

NAME LOG_MODE
--------- ------------
BOOBOO ARCHIVELOG

SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/orc_booboo_dest1
Oldest online log sequence 7
Next log sequence to archive 9
Current log sequence 9

4. 启动实例到open模式

SQL> alter database open;
Database altered.
SQL> create pfile from spfile;
File created.
SQL> create pfile from spfile;
File created.

5. 关闭数据库实例,备份数据库

更改数据库归档模式将更新控制文件。更改数据库归档模式后,必须备份所有数据库文件和控制文件。以前的任何备份均已在NOARCHIVELOG模式下使用,因此不再可用。

所以变更归档模式后一定要备份 。

具体备份操作查看上一节课。

实践03-在线热备用户表空间

备份需求

  1. 备份scott用户的表
  2. 备份期间不能影响用户使用数据库

备份步骤

  1. 查看scott用户使用的表空间 select username,default_tablespace from dba_users where username ='SCOTT';
  2. 查看表空间对应的表空间文件select file_name,tablespace_name from dba_data_files;
  3. 开始在线热备表空间alter tablespace <tablespace_name> begin backup;
  4. 拷贝表空间文件到备份目录!cp -v <tablespace_file> /home/oracle/hotbk/
  5. 结束在线热备表空间alter tablespace <tablespace_name> end backup;

操作记录

--scott用户
SQL> conn scott/tiger;
Connected.
SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEP01 TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE

SQL> select * from dep01;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

--sysdba
SQL> conn / as sysdba
Connected.
SQL> select username,default_tablespace from dba_users where username = 'SCOTT';

USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SCOTT USERS
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name='USERS';

TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
USERS
/alidata/oracle/oradata/testdb/users01.dbf
SQL> alter tablespace users begin backup;

Tablespace altered.

--scott
SQL> insert into dep01 values (50,'DBA','SHANGHAI');

1 row created.
SQL> select * from dep01;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DBA SHANGHAI


--sysdba
SQL> !cp -v /alidata/oracle/oradata/testdb/users01.dbf /home/oracle/hotbk/
"/alidata/oracle/oradata/testdb/users01.dbf" -> "/home/oracle/hotbk/users01.dbf"

SQL> alter tablespace users end backup;

Tablespace altered.

[oracle@NB-flexgw1 ~]$ ll hotbk/
总用量 5128
-rw-r----- 1 oracle oinstall 5251072 12月 1 02:17 users01.dbf

实践04-模拟表空间丢失并还原

模拟故障场景

  1. scott用户插入5条数据库后,sysdba用户手动切换redolog;
  2. sysdba用户模拟表空间文件丢失,删除scott用户的表空间文件;
  3. scott用户继续插入1条数据后,sysdba用户手动切换redolog;
  4. sysdba用户正常维护数据库关闭数据库实例报错。

通过在线热备的表空间文件进行恢复步骤

  1. sysdba用户尝试正常启动数据库startup
  2. 将缺失的表空间文件设置为offline状态alter database datafile <FILE#> offline;,打开数据库alter database open;
  3. 查看需要恢复的文件编号select * from v$recover_file;
  4. 拷贝备份表空间文件到数据库表空间文件位置!cp <backup_tablespace_file> <tablespace_file>
  5. 执行恢复命令recover datafile 4;
  6. 由于备份的表空间文件已经落后,需要应用日志来补数据,将数据库恢复到最后一个事务提交的点
  7. 恢复成功后将数据文件onlinealter database datafile <FILE#> online;
  8. 校验数据

操作记录

--scott
SQL> insert into dep01 values (51,'DBA01','SHANGHAI');

1 row created.

SQL> commit;

Commit complete.
--sysdba
SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> !rm -rf /alidata/oracle/oradata/testdb/users01.dbf

--scott
SQL> insert into dep01 values (52,'DBA02','SHANGHAI');

1 row created.

SQL> commit;

Commit complete.

--sysdba
SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> shutdown immediate
ORA-01116: error in opening database file 4
ORA-01110: data file 4: '/alidata/oracle/oradata/testdb/users01.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3

恢复操作记录:

--sysdba
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 2137886720 bytes
Fixed Size 2215064 bytes
Variable Size 1392509800 bytes
Database Buffers 738197504 bytes
Redo Buffers 4964352 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file
ORA-01110: data file 4: '/alidata/oracle/oradata/testdb/users01.dbf'

SQL> alter database datafile 4 offline;

Database altered.
SQL> alter database open;

Database altered.
SQL> select * from v$recover_file;

FILE# ONLINE ONLINE_
---------- ------- -------
ERROR CHANGE#
----------------------------------------------------------------- ----------
TIME
---------
4 OFFLINE OFFLINE
FILE NOT FOUND
SQL> !cp /home/oracle/hotbk/users01.dbf /alidata/oracle/oradata/testdb/users01.dbf
SQL> recover datafile 4;
ORA-00279: change 3571792 generated at 12/01/2019 02:14:56 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_120_1019996090.dbf
ORA-00280: change 3571792 for thread 1 is in sequence #120


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
AUTO
ORA-00279: change 3572245 generated at 12/01/2019 02:34:20 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_121_1019996090.dbf
ORA-00280: change 3572245 for thread 1 is in sequence #121


ORA-00279: change 3572248 generated at 12/01/2019 02:34:22 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_122_1019996090.dbf
ORA-00280: change 3572248 for thread 1 is in sequence #122


ORA-00279: change 3572251 generated at 12/01/2019 02:34:26 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_123_1019996090.dbf
ORA-00280: change 3572251 for thread 1 is in sequence #123


ORA-00279: change 3572254 generated at 12/01/2019 02:34:28 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_124_1019996090.dbf
ORA-00280: change 3572254 for thread 1 is in sequence #124


ORA-00279: change 3572313 generated at 12/01/2019 02:36:16 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_125_1019996090.dbf
ORA-00280: change 3572313 for thread 1 is in sequence #125


ORA-00279: change 3572316 generated at 12/01/2019 02:36:17 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_126_1019996090.dbf
ORA-00280: change 3572316 for thread 1 is in sequence #126


Log applied.
Media recovery complete.
SQL> alter database datafile 4 online;

Database altered.
SQL> select * from dep01;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DBA SHANGHAI
51 DBA01 SHANGHAI
52 DBA02 SHANGHAI

7 rows selected.

实践05-无备份恢复表空间

无备份的还原和恢复前提

  1. 数据库为归档模式
  2. 数据文件创建于归档之后

模拟故障场景

  1. 创建实验用的表空间create tablespace <tablespace_name> datafile <datafile> size <size>;
  2. 在新的表空间中创建表,将表空间写满数据create table <table_name> tablespace <tablespace_name> ...
  3. 切换日志alter system switch logfile;
  4. 模拟数据增长insert into...
  5. 切换日志alter system switch logfile;
  6. 模拟数据文件丢失!rm -rf <datafile>
  7. 重新启动数据库,发现文件丢失startup force;

通过控制文件创建崭新的数据文件进行恢复步骤

  1. 根据报错中指定的FILE#将该文件offlinealter database datafile <FILE#> offline;
  2. 打开数据库alter database open;
  3. 根据控制创建崭新的数据文件alter database create datafile <FILE#> as '<datafile>';
  4. 使用日志恢复数据文件recover datafile <FILE#>;
  5. 将数据文件onlinealter database datafile <FILE#> online;
  6. 校验数据

故障恢复操作记录

[oracle@NB-flexgw1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Sun Dec 1 18:49:02 2019

Copyright (c) 1982, 2009, Oracle. All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select instance_name from v$instances;
select instance_name from v$instances
*
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
testdb

SQL> create tablespace booboo datafile '/home/oracle/booboo.dbf' size 10m;

Tablespace created.

SQL> create table scott.t01 tablespace booboo as select * from scott.emp;

Table created.

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> insert into scott.t01 select * from scott.t01;

14 rows created.

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> !rm -rf /home/oracle/booboo.dbf

SQL> startup force
ORACLE instance started.

Total System Global Area 2137886720 bytes
Fixed Size 2215064 bytes
Variable Size 1392509800 bytes
Database Buffers 738197504 bytes
Redo Buffers 4964352 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/home/oracle/booboo.dbf'

SQL> alter database datafile 6 offline;

Database altered.

SQL> alter database open;

Database altered.

SQL> alter database create datafile 6 as '/home/oracle/booboo.dbf';

Database altered.

SQL> recover datafile 6;
ORA-00279: change 3630702 generated at 12/01/2019 18:53:13 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_131_1019996090.dbf
ORA-00280: change 3630702 for thread 1 is in sequence #131


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 3631012 generated at 12/01/2019 18:54:28 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_132_1019996090.dbf
ORA-00280: change 3631012 for thread 1 is in sequence #132


ORA-00279: change 3631015 generated at 12/01/2019 18:54:29 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_133_1019996090.dbf
ORA-00280: change 3631015 for thread 1 is in sequence #133


ORA-00279: change 3631019 generated at 12/01/2019 18:54:33 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_134_1019996090.dbf
ORA-00280: change 3631019 for thread 1 is in sequence #134


ORA-00279: change 3631022 generated at 12/01/2019 18:54:34 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_135_1019996090.dbf
ORA-00280: change 3631022 for thread 1 is in sequence #135


ORA-00279: change 3631039 generated at 12/01/2019 18:55:17 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_136_1019996090.dbf
ORA-00280: change 3631039 for thread 1 is in sequence #136


ORA-00279: change 3631042 generated at 12/01/2019 18:55:19 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_testdb_dest11_137_1019996090.dbf
ORA-00280: change 3631042 for thread 1 is in sequence #137


Log applied.
Media recovery complete.
SQL> alter database datafile 6 online;

Database altered.

SQL> select count(*) from scott.t01;

COUNT(*)
----------
14

实践06-不完全恢复

模拟故障场景

人为误操作后将数据库进行恢复:

  1. 在线热备所有表空间
  2. 人为误操作将某张表drop
  3. 继续正常操作其他表
  4. 发现表异常需要进行恢复

恢复数据

  1. 确认是否有全备份

  2. 确认灾难产生时间点和误操作SQL(dbms_logmnr进行挖掘)

    exec dbms_logmnr.add_logfile('/home/oracle/arc_booboo_dest1/1_18_1023917451.dbf',dbms_logmnr.new);
    exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
    select scn,sql_redo from v$logmnr_contents where seg_name='T01' and seg_owner='SCOTT';
  3. 在备用节点还原数据文件

  4. 创建控制文件

    create controlfile reuse database BOOBOO resetlogs archivelog
    datafile
    '/home/oracle/test/system01.dbf',
    '/home/oracle/test/sysaux01.dbf',
    '/home/oracle/test/undotbs01.dbf',
    '/home/oracle/test/users01.dbf'
    logfile
    group 1 '/home/oracle/test/redo01.log' size 50m,
    group 2 '/home/oracle/test/redo02.log' size 50m;
  5. 恢复数据库到灾难产生时间点recover database using backup controlfile until change 279430;

  6. 拷贝需要的归档日志

  7. 使用resetlogs方式打开数据库

操作记录

SQL> set linesize 100
SQL> set pagesize 100
select 'alter tablespace '||tablespace_name||' begin backup;'||chr(10)||
'!cp -v '||file_name||' /home/oracle/hotbk/'||chr(10)||
'alter tablespace '||tablespace_name||' end backup;'
4 from dba_data_files;

'ALTERTABLESPACE'||TABLESPACE_NAME||'BEGINBACKUP;'||CHR(10)||'!CP-V'||FILE
--------------------------------------------------------------------------
alter tablespace SYSTEM begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/system01.dbf /home/oracle/hotbk/
alter tablespace SYSTEM end backup;

alter tablespace SYSAUX begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/sysaux01.dbf /home/oracle/hotbk/
alter tablespace SYSAUX end backup;

alter tablespace UNDOTBS1 begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/undotbs01.dbf /home/oracle/hotbk/
alter tablespace UNDOTBS1 end backup;

alter tablespace USERS begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/users01.dbf /home/oracle/hotbk/
alter tablespace USERS end backup;


alter tablespace SYSTEM begin backup;
lter tablespace SYSAUX end backup;

alter tablespace UNDOTBS1 begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/undotbs01.dbf /home/oracle/hotbk/
alter tablespace UNDOTBS1 end backup;

alter tablespace USERS begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/system01.dbf /home/oracle/hotbk/
alter tablespace SYSTEM end backup;

alter tablespace SYSAUX begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/sysaux01.dbf /home/oracle/hotbk/
alter tablespace SYSAUX end backup;


Tablespace altered.

SQL> alter tablespace UNDOTBS1 begin backup;
`/u01/app/oracle/oradata/BOOBOO/system01.dbf' -> `/home/oracle/hotbk/syste
!cp -v /u01/app/oracle/oradata/BOOBOO/undotbs01.dbf /home/oracle/hotbk/
alter tablespace UNDOTBS1 end backup;

alter tablespace USERS begin backup;
!cp -v /u01/app/oracle/oradata/BOOBOO/users01.dbf /home/oracle/hotbk/
alter tablespace USERS end backup;

SQL>
Tablespace altered.

SQL> SQL>
Tablespace altered.

SQL> `/u01/app/oracle/oradata/BOOBOO/sysaux01.dbf' -> `/home/oracle/hotbk/

SQL>
Tablespace altered.

SQL> SQL>
Tablespace altered.

SQL> `/u01/app/oracle/oradata/BOOBOO/undotbs01.dbf' -> `/home/oracle/hotbk

SQL>
Tablespace altered.

SQL> SQL>
Tablespace altered.

SQL> `/u01/app/oracle/oradata/BOOBOO/users01.dbf' -> `/home/oracle/hotbk/u

SQL>
Tablespace altered.
SQL> conn scott/tiger;
Connected.
SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
T02 TABLE

SQL> select * from t02;

X Y
---------- --------------------
1 中国
2 巴西
3 美国

SQL> create table t01 as select * from emp;

Table created.

SQL> insert into t01 select * from t01;

14 rows created.

SQL> insert into t01 select * from t01;

28 rows created.

SQL> insert into t01 select * from t01;

56 rows created.

SQL> insert into t01 select * from t01;

112 rows created.

SQL> insert into t01 select * from t01;

224 rows created.

SQL> insert into t01 select * from t01;

448 rows created.

SQL> conn / as sysdba
Connected.
SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> insert into t01 select *from t01;
insert into t01 select *from t01
*
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> insert into scott.t01 select *from scott.t01;

896 rows created.

SQL> /

1792 rows created.

SQL> /

3584 rows created.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> insert into scott.t01 select *from scott.t01;

7168 rows created.

SQL> alter system switch logfile;

System altered.

SQL> drop table scott.t01;

Table dropped.

SQL> insert into scott.t02 select *from scott.t02;
insert into scott.t02 select *from scott.t02
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003974) violated


SQL> create table scott.t03 as select * from scott.emp;

Table created.

SQL> insert into scott.t03 select * from scott.t03;

14 rows created.

SQL> /

28 rows created.

SQL> /

56 rows created.

SQL> /

112 rows created.

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> insert into scott.t03 select * from scott.t03;

224 rows created.

SQL> /

448 rows created.

SQL> /

896 rows created.

SQL> /

1792 rows created.

SQL> /

3584 rows created.

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> select * from scott.t01;
select * from scott.t01
*
ERROR at line 1:
ORA-00942: table or view does not exist
exec dbms_logmnr.add_logfile('/home/oracle/arc_booboo_dest1/1_18_1023917451.dbf',dbms_logmnr.new);
exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
select scn,sql_redo from v$logmnr_contents where seg_name='T01' and seg_owner='SCOTT';

SQL> desc v$logmnr_contents
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SCN NUMBER
START_SCN NUMBER
COMMIT_SCN NUMBER
TIMESTAMP DATE
START_TIMESTAMP DATE
COMMIT_TIMESTAMP DATE
XIDUSN NUMBER
XIDSLT NUMBER
XIDSQN NUMBER
XID RAW(8)
PXIDUSN NUMBER
PXIDSLT NUMBER
PXIDSQN NUMBER
PXID RAW(8)
TX_NAME VARCHAR2(256)
OPERATION VARCHAR2(32)
OPERATION_CODE NUMBER
ROLLBACK NUMBER
SEG_OWNER VARCHAR2(32)
SEG_NAME VARCHAR2(256)
TABLE_NAME VARCHAR2(32)
SEG_TYPE NUMBER
SEG_TYPE_NAME VARCHAR2(32)
TABLE_SPACE VARCHAR2(32)
ROW_ID VARCHAR2(18)
USERNAME VARCHAR2(30)
OS_USERNAME VARCHAR2(4000)
MACHINE_NAME VARCHAR2(4000)
AUDIT_SESSIONID NUMBER
SESSION# NUMBER
SERIAL# NUMBER
SESSION_INFO VARCHAR2(4000)
THREAD# NUMBER
SEQUENCE# NUMBER
RBASQN NUMBER
RBABLK NUMBER
RBABYTE NUMBER
UBAFIL NUMBER
UBABLK NUMBER
UBAREC NUMBER
UBASQN NUMBER
ABS_FILE# NUMBER
REL_FILE# NUMBER
DATA_BLK# NUMBER
DATA_OBJ# NUMBER
DATA_OBJV# NUMBER
DATA_OBJD# NUMBER
SQL_REDO VARCHAR2(4000)
SQL_UNDO VARCHAR2(4000)
RS_ID VARCHAR2(32)
SSN NUMBER
CSF NUMBER
INFO VARCHAR2(32)
STATUS NUMBER
REDO_VALUE NUMBER
UNDO_VALUE NUMBER
SAFE_RESUME_SCN NUMBER
CSCN NUMBER
OBJECT_ID RAW(16)
EDITION_NAME VARCHAR2(30)
CLIENT_ID VARCHAR2(64)

exec dbms_logmnr.add_logfile('/home/oracle/arc_booboo_dest1/1_10_1023917451.dbf',dbms_logmnr.new);

PL/SQL procedure successfully completed.

exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);

PL/SQL procedure successfully completed.

SQL> select scn,sql_redo from v$logmnr_contents where seg_name='T01' and seg_owner='SCOTT';

SCN
----------
SQL_REDO
----------------------------------------------------------------------------------------------------
279340
create table t01 as select * from emp;

exec dbms_logmnr.add_logfile('/home/oracle/arc_booboo_dest1/1_18_1023917451.dbf',dbms_logmnr.new);

PL/SQL procedure successfully completed.

exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);

PL/SQL procedure successfully completed.

SQL> select scn,sql_redo from v$logmnr_contents where seg_name='T01' and seg_owner='SCOTT';

SCN
----------
SQL_REDO
----------------------------------------------------------------------------------------------------
279427
ALTER TABLE "SCOTT"."T01" RENAME TO "BIN$mOJcvUGpEDHgU4YrqMCLfg==$0" ;

279430
drop table scott.t01 AS "BIN$mOJcvUGpEDHgU4YrqMCLfg==$0" ;


SQL> select scn,sql_redo from v$logmnr_contents where seg_name='T01' and seg_owner='SCOTT' and sql_redo like '%drop%';

SCN
----------
SQL_REDO
----------------------------------------------------------------------------------------------------
279430
drop table scott.t01 AS "BIN$mOJcvUGpEDHgU4YrqMCLfg==$0" ;


--在备用节点还原数据文件
SQL> startup nomount
ORACLE instance started.

Total System Global Area 229683200 bytes
Fixed Size 2251936 bytes
Variable Size 171967328 bytes
Database Buffers 50331648 bytes
Redo Buffers 5132288 bytes
SQL> show parameter db_name;

NAME TYPE
------------------------------------ ----------------------
VALUE
------------------------------
db_name string
BOOBOO


--创建控制文件
create controlfile reuse database BOOBOO resetlogs archivelog
datafile
'/home/oracle/test/system01.dbf',
'/home/oracle/test/sysaux01.dbf',
'/home/oracle/test/undotbs01.dbf',
'/home/oracle/test/users01.dbf'
logfile
group 1 '/home/oracle/test/redo01.log' size 50m,
group 2 '/home/oracle/test/redo02.log' size 50m;

Control file created.
SQL> select name,open_mode from v$database;

NAME OPEN_MODE
------------------ ----------------------------------------
BOOBOO MOUNTED

--拷贝需要的归档日志
[oracle@oratest ~]$ cp -v arc_back_dest1/1_{10..18}_1023917451.dbf arc_booboo_dest1/
`arc_back_dest1/1_10_1023917451.dbf' -> `arc_booboo_dest1/1_10_1023917451.dbf'
`arc_back_dest1/1_11_1023917451.dbf' -> `arc_booboo_dest1/1_11_1023917451.dbf'
`arc_back_dest1/1_12_1023917451.dbf' -> `arc_booboo_dest1/1_12_1023917451.dbf'
`arc_back_dest1/1_13_1023917451.dbf' -> `arc_booboo_dest1/1_13_1023917451.dbf'
`arc_back_dest1/1_14_1023917451.dbf' -> `arc_booboo_dest1/1_14_1023917451.dbf'
`arc_back_dest1/1_15_1023917451.dbf' -> `arc_booboo_dest1/1_15_1023917451.dbf'
`arc_back_dest1/1_16_1023917451.dbf' -> `arc_booboo_dest1/1_16_1023917451.dbf'
`arc_back_dest1/1_17_1023917451.dbf' -> `arc_booboo_dest1/1_17_1023917451.dbf'
`arc_back_dest1/1_18_1023917451.dbf' -> `arc_booboo_dest1/1_18_1023917451.dbf'


--恢复数据库到灾难产生时间点
SQL> recover database using backup controlfile until change 279430;
ORA-00279: change 278815 generated at 12/04/2019 21:56:49 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_10_1023917451.dbf
ORA-00280: change 278815 for thread 1 is in sequence #10


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 279381 generated at 12/04/2019 22:08:45 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_11_1023917451.dbf
ORA-00280: change 279381 for thread 1 is in sequence #11
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_10_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279384 generated at 12/04/2019 22:08:49 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_12_1023917451.dbf
ORA-00280: change 279384 for thread 1 is in sequence #12
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_11_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279387 generated at 12/04/2019 22:08:52 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_13_1023917451.dbf
ORA-00280: change 279387 for thread 1 is in sequence #13
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_12_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279403 generated at 12/04/2019 22:09:17 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_14_1023917451.dbf
ORA-00280: change 279403 for thread 1 is in sequence #14
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_13_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279406 generated at 12/04/2019 22:09:17 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_15_1023917451.dbf
ORA-00280: change 279406 for thread 1 is in sequence #15
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_14_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279409 generated at 12/04/2019 22:09:19 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_16_1023917451.dbf
ORA-00280: change 279409 for thread 1 is in sequence #16
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_15_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279412 generated at 12/04/2019 22:09:21 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_17_1023917451.dbf
ORA-00280: change 279412 for thread 1 is in sequence #17
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_16_1023917451.dbf' no
longer needed for this recovery


ORA-00279: change 279421 generated at 12/04/2019 22:09:26 needed for thread 1
ORA-00289: suggestion : /home/oracle/arc_booboo_dest1/1_18_1023917451.dbf
ORA-00280: change 279421 for thread 1 is in sequence #18
ORA-00278: log file '/home/oracle/arc_booboo_dest1/1_17_1023917451.dbf' no
longer needed for this recovery


Log applied.
Media recovery complete.

-- 使用resetlogs方式打开数据库
SQL> alter database open resetlogs;

Database altered.

SQL> select count(*) from scott.t01;

COUNT(*)
----------
14336

将数据在测试环境恢复出来后导出,再导入到生产环境。

在线热备表空间SQL

  1. 在线热备份所有的表空间;

    select 'alter tablespace '||tablespace_name||' begin backup;'||chr(10)||
    '!cp -v '||file_name||' /home/oracle/hotbk/'||chr(10)||
    'alter tablespace '||tablespace_name||' end backup;'
    from dba_data_files;
  1. 切换日志

    alter system switch logfile;
    alter system switch logfile;
    alter system switch logfile;
    alter system switch logfile;

老师笔记

热备份:(online backup)在数据库open状态下进行的备份,需要数据库运行在归档模式下!!!

archive log list
shut immediate
startup mount
alter database archivelog;
alter database open;

修改存档位置:
mkdir /home/oracle/arc_db01_dest1
alter system set log_archive_dest_1='location=/home/oracle/arc_db01_dest1/';

如何热备份用户表空间
alter tablespace users begin backup;
!cp -v /u01/app/oracle/oradata/db01/users01.dbf /home/oracle/hotbk/
alter tablespace users end backup;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;

模拟数据改变:
insert into scott.e01 select * from scott.e01;
commit;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;

模拟数据文件丢失:
!rm -f /u01/app/oracle/oradata/db01/users01.dbf

模拟数据改变:
insert into scott.e01 select * from scott.e01;
commit;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;

重新启动数据库
startup force

将普通文件offline,打开数据库(offline状态的数据文件open时不会被校验)
SQL> alter database datafile 4 offline;
SQL> alter database open;
查看哪些文件需要恢复
SQL> select * from v$recover_file;

还原数据文件:将备份的文件拷贝回数据库中
cp -v /home/oracle/hotbk/users01.dbf /u01/app/oracle/oradata/db01/users01.dbf

恢复数据文件
SQL> recover datafile 4;

Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
AUTO

恢复成功后将数据文件online,然后校验数据:
SQL> alter database datafile 4 online;

Database altered.

Elapsed: 00:00:00.15
SQL> select count(*) from scott.e01;

COUNT(*)
----------
57344
\#################################################################################
无备份的还原和恢复:
1.数据库要归档模式
2.数据文件创建于归档之后

创建实验用的表空间
create tablespace tbs1 datafile '/home/oracle/tbs01.dbf' size 10m;

在新的表空间中创建表,将表空间写满数据
create table scott.t01 tablespace tbs1 as select * from scott.emp;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;

模拟数据增长
insert into scott.t01 select * from scott.t01;
commit;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;

模拟数据文件丢失
!rm -f /home/oracle/tbs01.dbf

重新启动数据库,发现文件丢失
startup force

ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/home/oracle/tbs01.dbf'

将5号文件offline打开数据库
SQL> alter database datafile 5 offline;
SQL> alter database open;

根据控制创建崭新的数据文件
alter database create datafile 5 as '/u01/app/oracle/oradata/db01/tbs01.dbf';

使用日志恢复数据文件
SQL> recover datafile 5;

将数据文件online
SQL> alter database datafile 5 online;

校验数据:
select count(*) from scott.t01;
\#################################################################################
热备份全库:
select 'alter tablespace '||tablespace_name||' begin backup;'||chr(10)||
'!cp -v '||file_name||' /home/oracle/hotbk/'||chr(10)||
'alter tablespace '||tablespace_name||' end backup;'
from dba_data_files;

切换日志:
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
\#################################################################################
不完全恢复:

1.要有全备
2.要确定灾难产生时间点(log miner)
生成挖掘队列:告诉oracle要对哪些日志进行挖掘
exec dbms_logmnr.add_logfile('/home/oracle/arc_db01_dest1/1_39_924710753.dbf',dbms_logmnr.new);
开始挖掘
exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
查看挖掘结果
select scn,sql_redo from v$logmnr_contents where seg_name='E01' and seg_owner='SCOTT';

SCN
----------
SQL_REDO
---------------------------
--------------------
1032938
drop table scott.e01 purge;

3.在备用节点还原数据文件

4.创建控制文件
create controlfile reuse database db01 resetlogs archivelog
datafile
'/home/oracle/test/system01.dbf',
'/home/oracle/test/sysaux01.dbf',
'/home/oracle/test/tbs01.dbf',
'/home/oracle/test/undotbs01.dbf',
'/home/oracle/test/users01.dbf'
logfile
group 1 '/home/oracle/test/redo01.log' size 50m,
group 2 '/home/oracle/test/redo02.log' size 50m;

5.恢复数据库到灾难产生时间点
recover database using backup controlfile until change 1032938;

6.拷贝需要的归档日志
scp /home/oracle/arc_db01_dest1/1_{27..35}_924710753.dbf oracle@172.25.5.11:/home/oracle/arc_db01_dest1/

7.使用resetlogs方式打开数据库
Log applied.
Media recovery complete.
SQL> alter database open resetlogs;

Database altered.

SQL> select count(*) from scott.e01;

COUNT(*)
----------
50000

大宝笔记

本节课需要掌握的要点:

  1. 如何从非归档变更为归档模式,注意修改后要立刻全备份;
  2. 指定归档路径时应注意归档目录容量的评估;
  3. 手动做物理备份的在线热备只需了解即可,后面会学习RMAN工具
  4. 掌握人为误操作后通过挖掘日志的方式恢复到指定时间点的方法,注意和MySQL的对比(MySQL正向恢复可以跳过人为误操作的点,而Oracle不可以。)