一看就会一做就废系列:说说 RECOVER DATABASE(上)

这里是:一看就会,一做就废系列

数据库演示版本为 19.3 (12.2.0.3

该系列涉及恢复过程中使用的 5 个语句:

1. recover database

2. recover database until cancel

3. recover database using backup controlfile

4. recover database until cancel using backup controlfile

5. recover database using backup controlfile until cancel

继续之前,你得知道什么叫还原什么叫恢复 ?以及备份与恢复的基础 点我查看己亥清爽系列

恢复级别一共三个:recover database > recover tablespace > recover datafile 本系列目的是为了演示上面 5 条命令的区别与联系,所以不探讨恢复级别以及数据库状态对应所使用的恢复级别问题。*别 database 已经包含了 tablespace 和 datafile 两个级别。

版权声明:博客园 AskScuti 版权所有,未经允许,禁止转载!

 目录

1. 概念解释

2. 情况说明

3. 实验过程

  3.1 备份 CDB

  3.2 PDB1 创建测试数据

  3.3 删除 PDB1 所有数据文件

  3.4 还原 PDB1 所有数据文件

  3.5 恢复 PDB1 所有数据文件

  3.6 开启 PDB1 进行数据验证

 

1. 概念解释

  首先你得知道,recover database 这个命令是用作对所有数据文件进行恢复的,而且是完全恢复

  有个细节注意下,这个命令分为两种,一种用在 SQL 命令行一种用在 RMAN

  SQL > recover database 仅对所有数据文件进行完全恢复,前提是控制文件为当前最新状态不可以是还原过来的老版本或是手工重建,否则 SQL 命令行执行将返回错误

  RMAN > recover database 对所有数据文件和控制文件进行完全恢复

  本例中仅演示对数据文件的完全恢复,此命令对控制文件的恢复将在下篇讨论。

  一句话:recover database 就是对所有数据进行完全恢复操作(控制文件、归档日志、联机日志完好无损的情况下

 

2. 情况说明

  当前系统中,控制文件、归档日志、联机日志都完好无损,删除数据文件,进行还原,使用 recover database 进行完全恢复。

 

3. 实验过程

3.1 备份 CDB

备份整个 CDB(CDB$ROOT / PDB1 / PDB$SEED)

RMAN> backup database format '/u02/backup/%s_%d_%U.full' tag 'full_backup';
一看就会一做就废系列:说说 RECOVER DATABASE(上)
RMAN> backup database format '/u02/backup/%s_%d_%U.full' tag 'full_backup';

Starting backup at 08-JUN-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/CDB1/system01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/CDB1/sysaux01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/CDB1/undotbs01.dbf
input datafile file number=00007 name=/u01/app/oracle/oradata/CDB1/users01.dbf
channel ORA_DISK_1: starting piece 1 at 08-JUN-19
channel ORA_DISK_1: finished piece 1 at 08-JUN-19
piece handle=/u02/backup/8_CDB1_08u3ji4n_1_1.full tag=FULL_BACKUP comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:55
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00010 name=/u01/app/oracle/oradata/CDB1/pdb1/sysaux01.dbf
input datafile file number=00009 name=/u01/app/oracle/oradata/CDB1/pdb1/system01.dbf
input datafile file number=00011 name=/u01/app/oracle/oradata/CDB1/pdb1/undotbs01.dbf
input datafile file number=00012 name=/u01/app/oracle/oradata/CDB1/pdb1/users01.dbf
input datafile file number=00013 name=/u01/app/oracle/oradata/CDB1/pdb1/henry01.dbf
channel ORA_DISK_1: starting piece 1 at 08-JUN-19
channel ORA_DISK_1: finished piece 1 at 08-JUN-19
piece handle=/u02/backup/9_CDB1_09u3ji6f_1_1.full tag=FULL_BACKUP comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/u01/app/oracle/oradata/CDB1/pdbseed/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/CDB1/pdbseed/system01.dbf
input datafile file number=00008 name=/u01/app/oracle/oradata/CDB1/pdbseed/undotbs01.dbf
channel ORA_DISK_1: starting piece 1 at 08-JUN-19
channel ORA_DISK_1: finished piece 1 at 08-JUN-19
piece handle=/u02/backup/10_CDB1_0au3ji78_1_1.full tag=FULL_BACKUP comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
Finished backup at 08-JUN-19

Starting Control File and SPFILE Autobackup at 08-JUN-19
piece handle=/u02/backup/c-983951798-20190608-02.CTL comment=NONE
Finished Control File and SPFILE Autobackup at 08-JUN-19
process

3.2 PDB1 创建测试数据

连接 PDB1 查看当前数据文件

[oracle@henry ~]$ sqlplus sys/oracle@pdb1 as sysdba
SQL> select name from v$datafile;
[oracle@henry ~]$ sqlplus sys/oracle@pdb1 as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Jun 8 16:48:51 2019
Version 19.3.0.0.0

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

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> select name from v$datafile;

NAME
------------------------------------------------
/u01/app/oracle/oradata/CDB1/pdb1/system01.dbf
/u01/app/oracle/oradata/CDB1/pdb1/sysaux01.dbf
/u01/app/oracle/oradata/CDB1/pdb1/undotbs01.dbf
/u01/app/oracle/oradata/CDB1/pdb1/users01.dbf
/u01/app/oracle/oradata/CDB1/pdb1/henry01.dbf

创建测试表 recover_db,插入数据 1,2,3 并 commit 提交

SQL> create table recover_db(id number) tablespace henry;

Table created.

SQL> insert into recover_db values(1);

1 row created.

SQL> insert into recover_db values(2);

1 row created.

SQL> insert into recover_db values(3);

1 row created.

SQL> commit;

Commit complete.

3.3 删除 PDB1 所有数据文件

SQL> !rm -rf /u01/app/oracle/oradata/CDB1/pdb1/*

继续插入数据 4,5,6 不提交,执行触发完全检查点,刷缓存数据,报错(请思考:物理文件删除,是否还可以继续插入数据?为什么)

SQL> insert into recover_db values(4);

1 row created.

SQL> insert into recover_db values(5);

1 row created.

SQL> insert into recover_db values(6);

1 row created.

SQL> alter system checkpoint;

System altered.

SQL> /
alter system checkpoint
*
ERROR at line 1:
ORA-03135: connection lost contact
Process ID: 30992
Session ID: 273 Serial number: 51273

因为对表的所有操作都是在内存里面,此时和物理数据文件无关,所以可以继续。

触发检查点的目的是为了将缓存数据刷入磁盘,而这时就和数据文件有关系了,第一次触发完全检查点刷数据,没反应过来,第二次刷报错,因为缓存数据要写入磁盘,可是对应的磁盘文件被删除,因此报错。

请留意:插入 4,5,6 三条数据后的操作,是否有提交?完全恢复是否可以恢复出 4,5,6 ?

切换 CDB 查看 PDB1 状态,并尝试启动(这里为什么 PDB1 是 MOUNT ?)

SQL> conn / as sysdba
Connected.
SQL> show pdbs

    CON_ID CON_NAME      OPEN MODE  RESTRICTED
---------- ------------- ---------- ----------
     2   PDB$SEED       READ ONLY  NO
     3   PDB1           MOUNTED
SQL> alter pluggable database pdb1 open; alter pluggable database pdb1 open * ERROR at line 1: ORA-01157: cannot identify/lock data file 13 - see DBWR trace file ORA-01110: data file 13: '/u01/app/oracle/oradata/CDB1/pdb1/henry01.dbf'

因为多租户体系中,参数文件控制文件都处于 CDB 级别,因此,只要 CDB 是开启的,下面所有 PDB 最低级别都是 MOUNT(哪怕这个库已经出了问题,停止了,依然显示MOUNT)

3.4 还原 PDB1 所有数据文件

还原数据文件

你可以连接 PDB1 还原自己

[oracle@henry ~]$ rman target sys/oracle@pdb1

Recovery Manager: Release 19.0.0.0.0 - Production on Sat Jun 8 17:13:26 2019
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

connected to target database: CDB1:PDB1 (DBID=2947650716, not open)

RMAN> restore database from tag='FULL_BACKUP';

Starting restore at 08-JUN-19
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=11 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00009 to /u01/app/oracle/oradata/CDB1/pdb1/system01.dbf
channel ORA_DISK_1: restoring datafile 00010 to /u01/app/oracle/oradata/CDB1/pdb1/sysaux01.dbf
channel ORA_DISK_1: restoring datafile 00011 to /u01/app/oracle/oradata/CDB1/pdb1/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00012 to /u01/app/oracle/oradata/CDB1/pdb1/users01.dbf
channel ORA_DISK_1: restoring datafile 00013 to /u01/app/oracle/oradata/CDB1/pdb1/henry01.dbf
channel ORA_DISK_1: reading from backup piece /u02/backup/9_CDB1_09u3ji6f_1_1.full
channel ORA_DISK_1: piece handle=/u02/backup/9_CDB1_09u3ji6f_1_1.full tag=FULL_BACKUP
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:36
Finished restore at 08-JUN-19

还可以连接 CDB ,还原 PDB1

RMAN> restore pluggable database pdb1 from tag='FULL_BACKUP';

Starting restore at 08-JUN-19
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00009 to /u01/app/oracle/oradata/CDB1/pdb1/system01.dbf
channel ORA_DISK_1: restoring datafile 00010 to /u01/app/oracle/oradata/CDB1/pdb1/sysaux01.dbf
channel ORA_DISK_1: restoring datafile 00011 to /u01/app/oracle/oradata/CDB1/pdb1/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00012 to /u01/app/oracle/oradata/CDB1/pdb1/users01.dbf
channel ORA_DISK_1: restoring datafile 00013 to /u01/app/oracle/oradata/CDB1/pdb1/henry01.dbf
channel ORA_DISK_1: reading from backup piece /u02/backup/9_CDB1_09u3ji6f_1_1.full
channel ORA_DISK_1: piece handle=/u02/backup/9_CDB1_09u3ji6f_1_1.full tag=FULL_BACKUP
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 08-JUN-19

3.5 恢复 PDB1 所有数据文件

恢复数据文件

你可以连接 PDB1 恢复自己

RMAN> recover database;

Starting recover at 08-JUN-19
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:00

Finished recover at 08-JUN-1

还可以连接 CDB ,恢复 PDB1

RMAN> recover pluggable database pdb1;

Starting recover at 08-JUN-19
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:00

Finished recover at 08-JUN-19

3.6 开启 PDB1 进行数据验证

打开数据库

你可以连接 PDB1 打开自己

RMAN> alter database open;

Statement processed

还可以连接 CDB ,打开 PDB1

RMAN> alter pluggable database pdb1 open;

Statement processed

验证数据

请问,完全恢复之后,recover_db 表中有几条数据?为什么?

SQL> select * from recover_db;

    ID
----------
     1
     2
     3

因为插入 4,5,6 没有提交,事务没有结束,因此不具有持久性。完全恢复是恢复到宕机前最后一次 commit 的状态

上一篇:使用rman增量备份恢复std数据库


下一篇:shell脚本 -- sed流编辑器