此实验关于Oracle 数据库备份和恢复。
此实验申请地址在这里。
实验帮助在这里。
此实验预估完成时间2小时。时间从2:00开始。实际花了将近3小时。
Oracle 数据库备份和恢复操作的重点是数据库文件的物理备份,这允许您在发生故障/损坏的情况下重建您的 Oracle 数据库。
Oracle Recovery Manager (RMAN) 是一种命令行工具,是 Oracle 首选的用于有效备份和恢复 Oracle 数据库的方法。由 RMAN 内置的备份和恢复工具保护的文件包括数据文件、控制文件、服务器参数文件和归档重做日志文件。使用这些文件,您可以重建您的 Oracle 数据库。 RMAN 旨在与服务器密切合作,在备份和恢复期间提供块级损坏检测。 RMAN 通过文件多路复用和备份集压缩优化备份期间的性能和空间消耗,并与领先的磁带和存储介质产品集成。备份机制在物理级别工作以防止文件损坏,例如数据文件的意外删除或磁盘驱动器的故障。当无法使用闪回等其他技术时,RMAN 还可用于执行时间点恢复以从逻辑故障中恢复。
Oracle 闪回特性提供了一系列物理和逻辑数据恢复工具,作为物理和逻辑备份的高效、易于使用的替代方案。 Oracle 闪回特性使您能够回退不需要的数据库更改的影响,而无需从备份中恢复数据文件。
本实验的作者为Suresh Mohan,其它贡献者为Suresh Rajan, Manish Garodia, Subhash Chandra, Ramya P。
export ORAENV_ASK=NO
export ORACLE_SID=CDB1
. oraenv
或者直接运行:
.set-env-db.sh
curl -O https://objectstorage.us-ashburn-1.oraclecloud.com/p/wVaLF_P62mfpzEzA7rRaCh7CgG8WtfStsG5MQ_kmRI6JkWNwErWWnQREmO0FLXcv/n/c4u04/b/livelabsfiles/o/labfiles/backup-and-recovery-operations-prerequisities.zip
unzip backup-and-recovery-operations-prerequisities.zip
只有一个文件,修改前为:
$ cat backup-and-recovery-operations-prerequisities.sql
alter session set container = pdb1;
CREATE user appuser IDENTIFIED BY mypassword container=current;
grant all privileges to appuser;
connect appuser/<password>@//<hostname>:<port>/<pdbname>
create tablespace oc datafile 'octs.dbf' size 32m;
create table regions (id number(2), name varchar2(20)) tablespace oc;
insert into regions values (1,'America');
insert into regions values (2,'Europe');
insert into regions values (3,'Asia');
commit;
!
mkdir /opt/oracle/oradata/CDB1
exit;
替换尖括号中的内容,修改后为:
$ cat backup-and-recovery-operations-prerequisities.sql
alter session set container = pdb1;
CREATE user appuser IDENTIFIED BY Welcome1 container=current;
grant all privileges to appuser;
connect appuser/Welcome1@//10.0.0.253:1521/pdb1
create tablespace oc datafile 'octs.dbf' size 32m;
create table regions (id number(2), name varchar2(20)) tablespace oc;
insert into regions values (1,'America');
insert into regions values (2,'Europe');
insert into regions values (3,'Asia');
commit;
! mkdir /opt/oracle/oradata/CDB1
exit;
这个表空间oc 就是我们用来实验的。
以下是获取信息的部分命令:
# 获取主机名
hostname -i
# 获取service
lsntctl status
然后执行以上脚本。
sqlplus / as sysdba @backup-and-recovery-operations-prerequisities
这个脚本写得太low了。
预计时间:20分钟
目标
快速恢复区是文件系统或 Oracle 自动存储管理 (Oracle ASM) 磁盘组上由 Oracle 管理的目录,为备份和恢复文件提供集中存储位置。 Oracle 在快速恢复区创建归档日志和闪回日志。 Oracle 自动管理快速恢复区,删除不再需要的文件。
Recovery Manager (RMAN) 可以将其备份集和映像副本存储在快速恢复区域中,并在介质恢复期间恢复文件时使用它们。 如果配置了快速恢复区,当您发出 RMAN 备份命令而不指定备份目标时,RMAN 会自动备份到快速恢复区。
其实环境已经设置好了:
SQL> connect / as sysdba
Connected.
SQL> show parameter recovery;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /opt/oracle/fast_recovery_area
db_recovery_file_dest_size big integer 10G
recovery_parallelism integer 0
remote_recovery_file_dest string
SQL> alter system set db_recovery_file_dest_size=10G;
System altered.
$ du -sh /opt/oracle/fast_recovery_area
6.7G /opt/oracle/fast_recovery_area
归档默认未启用:
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 15
Current log sequence 17
启用。用rman执行以下脚本(用sqlplus的话也可以,但其中做了个冷备,还是rman方便些):
connect target
shutdown immediate;
startup mount;
-- 这这里可以做个冷备, 在归档未启用时,也只能做冷备。
backup database;
alter database archivelog;
alter database open;
-- 归档已开,可以热备了
backup database plus archivelog;
冷备和热备居然报错了:
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03009: failure of backup command on ORA_DISK_1 channel at 11/14/2022 06:50:24
ORA-19809: limit exceeded for recovery files
ORA-19804: cannot reclaim 67108864 bytes disk space from 10737418240 bytes limit
可是,看上去空间是够的:
SQL> set lines 150
SQL> select * from v$flash_recovery_area_usage;
FILE_TYPE PERCENT_SPACE_USED PERCENT_SPACE_RECLAIMABLE NUMBER_OF_FILES CON_ID
----------------------- ------------------ ------------------------- --------------- ----------
CONTROL FILE .17 0 1 0
REDO LOG 5.86 0 3 0
ARCHIVED LOG 0 0 0 0
BACKUP PIECE 50.34 0 5 0
IMAGE COPY 0 0 0 0
FLASHBACK LOG 3.91 0 2 0
FOREIGN ARCHIVED LOG 0 0 0 0
AUXILIARY DATAFILE COPY 0 0 0 0
8 rows selected.
SQL>
col name for a50
select name
, floor(space_limit / 1024 / 1024) "Size MB"
, ceil(space_used / 1024 / 1024) "Used MB"
from v$recovery_file_dest
order by name ;
NAME Size MB Used MB
-------------------------------------------------- ---------- ----------
/opt/oracle/fast_recovery_area 10240 6193
确认归档已开启:
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 15
Next log sequence to archive 17
Current log sequence 17
空间确实是不够的,你看到的空间够,是因为是最终的而非过程中的状态,如果在备份过程中检测,可发现其到了13G。而且源数据库确实超过了10G:
$ du -sh /opt/oracle/oradata/CDB1
17G /opt/oracle/oradata/CDB1
因此,需要把FRA扩充到30G:
alter system set db_recovery_file_dest_size=30G scope=both;
单次在线备份消耗了21GB:
$ du -sh /opt/oracle/fast_recovery_area
21G /opt/oracle/fast_recovery_area
您可以使用以下2种方式将整个 Oracle 数据库恢复到之前的时间点:通过恢复备份和执行时间点恢复将整个 Oracle 数据库恢复到之前的时间点,或者启用闪回数据库。 启用闪回数据库时,Oracle 数据库会在快速恢复区生成闪回日志。 这些日志用于将 Oracle 数据库闪回到指定时间。 Oracle 数据库自动创建、删除和调整闪回日志的大小。
启用过程:
SQL> select FLASHBACK_ON from v$database;
FLASHBACK_ON
------------------
NO
SQL> alter database flashback on;
Database altered.
SQL> select FLASHBACK_ON from v$database;
FLASHBACK_ON
------------------
YES
和闪回相关的参数如下:
SQL> show parameter db_flashback_retention_target
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target integer 1440
SQL> show parameter db_recovery_file_dest
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /opt/oracle/fast_recovery_area
db_recovery_file_dest_size big integer 10G
预计时间:20分钟
目标
RMAN> connect target /
connected to target database: CDB1 (DBID=1090351676)
RMAN> show all;
using target database control file instead of recovery catalog
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE RMAN OUTPUT TO KEEP FOR 7 DAYS; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/opt/oracle/dbs/snapcf_CDB1.f'; # default
其实缺省备份设备就是disk:
RMAN> configure default device type to disk;
new RMAN configuration parameters:
CONFIGURE DEFAULT DEVICE TYPE TO DISK;
new RMAN configuration parameters are successfully stored
RMAN> show default device type;
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE DEFAULT DEVICE TYPE TO DISK;
配置备份优化以节省快速恢复区的空间。 优化排除了以前备份的未更改文件,例如只读文件和脱机数据文件。
RMAN> configure backup optimization on;
new RMAN configuration parameters:
CONFIGURE BACKUP OPTIMIZATION ON;
new RMAN configuration parameters are successfully stored
RMAN> show backup optimization;
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE BACKUP OPTIMIZATION ON;
配置保留策略以指定备份和归档重做日志必须保留多长时间以进行介质恢复。默认的保留策略是REDUNDANCY 1
:
RMAN> configure retention policy to recovery window of 31 days;
new RMAN configuration parameters:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 31 DAYS;
new RMAN configuration parameters are successfully stored
RMAN> show retention policy;
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 31 DAYS;
您可以将 RMAN 配置为在每次备份时自动备份控制文件和服务器参数文件。 这称为自动备份(autobackup)。 控制和服务器参数文件对 Oracle 数据库和 RMAN 至关重要。 创建控制文件的自动备份使 RMAN 能够恢复 Oracle 数据库,即使当前控制文件和服务器参数文件丢失。 与典型的数据文件相比,控制和服务器参数文件相对较小,因此,经常备份它们会导致相对较少的存储开销。
如果 Oracle 数据库在 ARCHIVELOG 模式下运行,那么只要控制文件中的 Oracle 数据库结构元数据发生更改,也会进行自动备份。
此配置默认就是打开的:
RMAN> show controlfile autobackup;
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE CONTROLFILE AUTOBACKUP ON; # default
RMAN> configure controlfile autobackup on;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored
RMAN> show controlfile autobackup;
RMAN configuration parameters for database with db_unique_name CDB1 are:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
块更改跟踪通过在块更改跟踪文件中记录更改的块来提高增量备份的性能。 在增量备份期间,RMAN 不是扫描所有数据块来识别哪些块已更改,而是使用此文件来识别需要备份的更改块。
您可以在 Oracle 数据库打开或挂载时启用块更改跟踪。 本节假设您打算将块更改跟踪文件创建为数据库区域中的由Oracle 管理的文件,其中 Oracle 数据库维护活动的数据库文件,例如数据文件、控制文件和联机重做日志文件。
alter system set db_create_file_dest = '/opt/oracle/oradata/CDB1';
alter database enable block change tracking;
col filename for a80
select status, filename from v$block_change_tracking;
STATUS FILENAME
---------- --------------------------------------------------------------------------------
ENABLED /opt/oracle/oradata/CDB1/CDB1/changetracking/o1_mf_kq3x7884_.chg
使用rman执行以下:
backup database plus archivelog;
实验设计者为了启发我们的心智,将FRA设置为只能容纳一次全备。所以,为了备份成功,每次备份前都需要删除之前的备份:
delete backup;
list backup summary;
使用 LIST 命令查看有关存储在 RMAN 存储库中的备份的信息。 这些信息包括数据文件、单个表空间、归档重做日志文件和控制文件的备份。 您还可以使用此命令显示有关过期和过时备份的信息。
查看备份概况,第4列S列表示状态,A表示Available,X表示Expired,U表示Unavailable。
LV列表示Level of backup,0和1表示增量备份,F表示全量备份,A表示归档备份。
TY列表示备份类型,B表示backup set。说明详见List命令帮助中的表2-16。
RMAN> list backup summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
3 B A A DISK 14-NOV-22 1 1 NO TAG20221114T065745
4 B F A DISK 14-NOV-22 1 1 NO TAG20221114T065746
5 B F A DISK 14-NOV-22 1 1 NO TAG20221114T065746
6 B F A DISK 14-NOV-22 1 1 NO TAG20221114T072701
7 B A A DISK 14-NOV-22 1 1 NO TAG20221114T073657
8 B F A DISK 14-NOV-22 1 1 NO TAG20221114T073658
9 B F A DISK 14-NOV-22 1 1 NO TAG20221114T073658
10 B A A DISK 14-NOV-22 1 1 NO TAG20221114T074125
11 B F A DISK 14-NOV-22 1 1 NO TAG20221114T074126
12 B F A DISK 14-NOV-22 1 1 NO TAG20221114T074126
13 B F A DISK 14-NOV-22 1 1 NO TAG20221114T074126
14 B A A DISK 14-NOV-22 1 1 NO TAG20221114T074504
15 B F A DISK 14-NOV-22 1 1 NO TAG20221114T074505
16 B A A DISK 14-NOV-22 1 1 NO TAG20221114T080049
17 B F A DISK 14-NOV-22 1 1 NO TAG20221114T080050
查看指定备份的详细状态:
RMAN> list backup of datafile 3;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
4 Full 1.84G DISK 00:00:02 14-NOV-22
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20221114T065746
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T065746_kq3svkr0_.bkp
List of Datafiles in backup set 4
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
3 Full 5507797 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
8 Full 1.83G DISK 00:00:01 14-NOV-22
BP Key: 8 Status: AVAILABLE Compressed: NO Tag: TAG20221114T073658
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T073658_kq3w35x4_.bkp
List of Datafiles in backup set 8
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
3 Full 5511663 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
12 Full 1.83G DISK 00:00:01 14-NOV-22
BP Key: 12 Status: AVAILABLE Compressed: NO Tag: TAG20221114T074126
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T074126_kq3wh032_.bkp
List of Datafiles in backup set 12
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
3 Full 5512092 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
17 Full 1.83G DISK 00:00:01 14-NOV-22
BP Key: 17 Status: AVAILABLE Compressed: NO Tag: TAG20221114T080050
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T080050_kq3xlfq9_.bkp
List of Datafiles in backup set 17
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
3 Full 5513470 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
验证特定备份会检查这些备份是否存在并且可以恢复。 它不会测试可用备份集是否满足您的可恢复性目标。 例如,可能存在来自 Oracle 数据库的多个表空间的数据文件的映像副本,每个都可以验证。 但是,如果某些表空间不存在有效备份,则无法还原和恢复 Oracle 数据库。
RMAN> list backup summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
18 B A A DISK 14-NOV-22 1 1 NO TAG20221114T081244
19 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
20 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
21 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
22 B A A DISK 14-NOV-22 1 1 NO TAG20221114T081543
23 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081544
RMAN> list backupset;
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
18 10.83M DISK 00:00:00 14-NOV-22
BP Key: 18 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081244
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_annnn_TAG20221114T081244_kq3y3wdo_.bkp
List of Archived Logs in backup set 18
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 18 5507618 14-NOV-22 5511557 14-NOV-22
1 19 5511557 14-NOV-22 5511769 14-NOV-22
1 20 5511769 14-NOV-22 5512129 14-NOV-22
1 21 5512129 14-NOV-22 5513253 14-NOV-22
1 22 5513253 14-NOV-22 5514777 14-NOV-22
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
19 Full 11.45G DISK 00:02:19 14-NOV-22
BP Key: 19 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081245
Piece Name: /opt/oracle/fast_recovery_area/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y3xp2_.bkp
List of Datafiles in backup set 19
Container ID: 3, PDB Name: PDB1
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
9 Full 5514795 14-NOV-22 NO /opt/oracle/oradata/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/datafile/o1_mf_system_km9s6o7b_.dbf
10 Full 5514795 14-NOV-22 NO /opt/oracle/oradata/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/datafile/o1_mf_sysaux_km9s6o7n_.dbf
11 Full 5514795 14-NOV-22 NO /opt/oracle/oradata/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/datafile/o1_mf_undotbs1_km9s6o7o_.dbf
12 Full 5514795 14-NOV-22 NO /opt/oracle/oradata/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/datafile/o1_mf_users_km9s7k3w_.dbf
13 Full 5514795 14-NOV-22 NO /opt/oracle/homes/OraDBHome21cEE/dbs/octs.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
20 Full 1.83G DISK 00:00:01 14-NOV-22
BP Key: 20 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081245
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y8gtl_.bkp
List of Datafiles in backup set 20
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
1 Full 5514879 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_system_km9rjx9n_.dbf
3 Full 5514879 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
4 Full 5514879 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_undotbs1_km9rmqhz_.dbf
7 Full 5514879 14-NOV-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_users_km9rmrky_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
21 Full 618.27M DISK 00:00:01 14-NOV-22
BP Key: 21 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081245
Piece Name: /opt/oracle/fast_recovery_area/CDB1/E9C6C2916324E719E0538200000A6F76/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y97w4_.bkp
List of Datafiles in backup set 21
Container ID: 2, PDB Name: PDB$SEED
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
5 Full 2758243 29-SEP-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_system_km9rt6fd_.dbf
6 Full 2758243 29-SEP-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rt6fg_.dbf
8 Full 2758243 29-SEP-22 NO /opt/oracle/oradata/CDB1/datafile/o1_mf_undotbs1_km9rt6fh_.dbf
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
22 38.00K DISK 00:00:00 14-NOV-22
BP Key: 22 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081543
Piece Name: /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_annnn_TAG20221114T081543_kq3y9h2t_.bkp
List of Archived Logs in backup set 22
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 23 5514777 14-NOV-22 5514904 14-NOV-22
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
23 Full 17.95M DISK 00:00:00 14-NOV-22
BP Key: 23 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081544
Piece Name: /opt/oracle/fast_recovery_area/CDB1/autobackup/2022_11_14/o1_mf_s_1120724144_kq3y9j9l_.bkp
SPFILE Included: Modification time: 14-NOV-22
SPFILE db_unique_name: CDB1
Control File Included: Ckp SCN: 5514924 Ckp time: 14-NOV-22
RMAN> list backupset 23;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
23 Full 17.95M DISK 00:00:00 14-NOV-22
BP Key: 23 Status: AVAILABLE Compressed: NO Tag: TAG20221114T081544
Piece Name: /opt/oracle/fast_recovery_area/CDB1/autobackup/2022_11_14/o1_mf_s_1120724144_kq3y9j9l_.bkp
Control File Included: Ckp SCN: 5514924 Ckp time: 14-NOV-22
SPFILE Included: Modification time: 14-NOV-22
SPFILE db_unique_name: CDB1
验证:
RMAN> validate backupset 23;
Starting validate at 14-NOV-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting validation of datafile backup set
channel ORA_DISK_1: reading from backup piece /opt/oracle/fast_recovery_area/CDB1/autobackup/2022_11_14/o1_mf_s_1120724144_kq3y9j9l_.bkp
channel ORA_DISK_1: piece handle=/opt/oracle/fast_recovery_area/CDB1/autobackup/2022_11_14/o1_mf_s_1120724144_kq3y9j9l_.bkp tag=TAG20221114T081544
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: validation complete, elapsed time: 00:00:01
Finished validate at 14-NOV-22
validate命令的其它形式包括:
RMAN> validate datafile 3;
Starting validate at 14-NOV-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting validation of datafile
channel ORA_DISK_1: specifying datafile(s) for validation
input datafile file number=00003 name=/opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
channel ORA_DISK_1: validation complete, elapsed time: 00:00:01
List of Datafiles
=================
File Status Marked Corrupt Empty Blocks Blocks Examined High SCN
---- ------ -------------- ------------ --------------- ----------
3 OK 0 32274 120326 5515590
File Name: /opt/oracle/oradata/CDB1/datafile/o1_mf_sysaux_km9rlyfm_.dbf
Block Type Blocks Failing Blocks Processed
---------- -------------- ----------------
Data 0 16633
Index 0 16252
Other 0 55161
Finished validate at 14-NOV-22
RMAN> restore tablespace users validate;
Starting restore at 14-NOV-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting validation of datafile backup set
channel ORA_DISK_1: reading from backup piece /opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y8gtl_.bkp
channel ORA_DISK_1: piece handle=/opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y8gtl_.bkp tag=TAG20221114T081245
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: validation complete, elapsed time: 00:00:01
Finished restore at 14-NOV-22
备份和恢复策略的一个重要部分是在创建备份后对其进行管理。 备份管理包括删除过时的备份和执行定期检查以确保备份可用和可用。 在多租户环境中,您可以管理整个多租户容器数据库 (CDB) 或一个或多个可插拔数据库 (PDB) 的备份。
您可以通过删除过时的备份和执行定期检查来管理备份,以确保备份可用且可用。
目标
RMAN> select session_key, input_type, status, start_time, end_time, elapsed_seconds/3600 hrs from v$rman_backup_job_details;
SESSION_KEY INPUT_TYPE STATUS START_TIM END_TIME HRS
----------- ------------- ----------------------- --------- --------- ----------
5 DB FULL FAILED 14-NOV-22 14-NOV-22 .076388888
8 DB FULL FAILED 14-NOV-22 14-NOV-22 .046666666
15 DB FULL FAILED 14-NOV-22 14-NOV-22 .030277777
23 DB FULL COMPLETED 14-NOV-22 14-NOV-22 .061111111
29 DB FULL FAILED 14-NOV-22 14-NOV-22 .050555555
32 DB FULL COMPLETED 14-NOV-22 14-NOV-22 .05
注意:SESSION_KEY 是发生备份作业的 RMAN 会话的唯一键。
交叉检查备份将备份的物理现实与其在 RMAN 存储库中的逻辑记录同步。 例如,如果使用操作系统命令删除了磁盘上的备份,则交叉检查会检测到这种情况。 交叉检查后,RMAN 存储库正确反映了备份的状态。
如果磁盘备份仍在 RMAN 存储库中列出的位置的磁盘上,并且文件头中没有损坏,则磁盘备份将显示为可用。 如果磁带上的备份仍在磁带上,它们将被列为可用。 不检查磁带上的文件头是否损坏。 丢失或损坏的备份被列为过期(我怀疑此处应该为Unavailable)。
RMAN> list backup summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
18 B A A DISK 14-NOV-22 1 1 NO TAG20221114T081244
19 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
20 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
21 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081245
22 B A A DISK 14-NOV-22 1 1 NO TAG20221114T081543
23 B F A DISK 14-NOV-22 1 1 NO TAG20221114T081544
RMAN> crosscheck backup;
using channel ORA_DISK_1
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_annnn_TAG20221114T081244_kq3y3wdo_.bkp RECID=18 STAMP=1120723964
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y3xp2_.bkp RECID=19 STAMP=1120723965
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y8gtl_.bkp RECID=20 STAMP=1120724110
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/E9C6C2916324E719E0538200000A6F76/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T081245_kq3y97w4_.bkp RECID=21 STAMP=1120724135
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_annnn_TAG20221114T081543_kq3y9h2t_.bkp RECID=22 STAMP=1120724143
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/autobackup/2022_11_14/o1_mf_s_1120724144_kq3y9j9l_.bkp RECID=23 STAMP=1120724144
Crosschecked 6 objects
RMAN> crosscheck backupset 18;
using channel ORA_DISK_1
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/fast_recovery_area/CDB1/backupset/2022_11_14/o1_mf_annnn_TAG20221114T081244_kq3y3wdo_.bkp RECID=18 STAMP=1120723964
Crosschecked 1 objects
delete expired backup 命令将从 RMAN 存储库中删除 EXPIRED 备份。 在交叉检查期间无法访问的那些备份称为过期备份。 此命令仅更新 RMAN 存储库。 它不会尝试从磁盘或磁带中删除备份文件。
RMAN> delete expired backup;
using channel ORA_DISK_1
specification does not match any backup in the repository
您应该监视快速恢复区以确保它足够大以包含备份和其他与恢复相关的文件。 Oracle 数据库中的空间使用情况可能与本实验中显示的有所不同。
Oracle 数据库提供了两个视图来监控快速恢复区空间使用情况,即 v$recovery_file_dest
和 v$recovery_area_usage
。
SQL> select * from v$recovery_file_dest;
NAME
--------------------------------------------------------------------------------
SPACE_LIMIT SPACE_USED SPACE_RECLAIMABLE NUMBER_OF_FILES CON_ID
----------- ---------- ----------------- --------------- ----------
/opt/oracle/fast_recovery_area
3.2212E+10 1.6023E+10 11384832 18 0
RMAN> select file_type, percent_space_used PCT_USED, percent_space_reclaimable PCT_RECLAIM, number_of_files NO_FILES from v$recovery_area_usage;
FILE_TYPE PCT_USED PCT_RECLAIM NO_FILES
----------------------- ---------- ----------- ----------
CONTROL FILE .06 0 1
REDO LOG 1.95 0 3
ARCHIVED LOG .04 .04 6
BACKUP PIECE 46.39 0 6
IMAGE COPY 0 0 0
FLASHBACK LOG 1.3 0 2
FOREIGN ARCHIVED LOG 0 0 0
AUXILIARY DATAFILE COPY 0 0 0
8 rows selected
Data Recovery Advisor是Oracle数据库的一项功能,可自动诊断数据故障,确定并提供适当的修复选项,并在用户请求时执行修复。通过提供一个用于自动化数据修复的集中化工具,Data Recovery Advisor提高了Oracle数据库的可管理性和可靠性。
注意:Data Recovery Advisor只能用于诊断和修复多租户容器数据库(CDB)中的故障。不支持可插拔数据库(PDB)。
Recovery Manager(RMAN)为Data Recovery Advisor提供了一个命令行界面。您可以使用以下RMAN命令来诊断和修复Oracle数据库(包括Oracle Real Application Clusters(RAC)数据库)的数据故障:
当您怀疑或发现故障时,恢复过程就开始了。 您可以通过多种方式发现故障,包括错误消息、警报、跟踪文件和运行状况检查。 然后,您可以使用 Data Recovery Advisor 获取有关故障的信息和建议并自动修复它们。
使用以下的命令查询并删除数据文件,以模拟错误:
connect / as sysdba;
alter pluggable database pdb1 open;
alter session set container = pdb1;
select * from appuser.regions;
set pages 9999
select name from v$datafile;
alter pluggable database pdb1 close;
! rm /opt/oracle/homes/OraDBHome21cEE/dbs/octs.dbf
再次打开数据库,此时出现错误:
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: '/opt/oracle/homes/OraDBHome21cEE/dbs/octs.dbf'
在RMAN中使用Data Recovery Advisor修复错误:
connect target;
list failure;
advise failure;
repair failure;
list failure;
执行过程如下:
RMAN> list failure;
using target database control file instead of recovery catalog
Database Role: PRIMARY
List of Database Failures
=========================
Failure ID Priority Status Time Detected Summary
---------- -------- --------- ------------- -------
182 HIGH OPEN 14-NOV-22 One or more non-system datafiles are missing
RMAN> advise failure;
Database Role: PRIMARY
List of Database Failures
=========================
Failure ID Priority Status Time Detected Summary
---------- -------- --------- ------------- -------
182 HIGH OPEN 14-NOV-22 One or more non-system datafiles are missing
analyzing automatic repair options; this may take some time
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=26 device type=DISK
analyzing automatic repair options complete
Mandatory Manual Actions
========================
no manual actions available
Optional Manual Actions
=======================
1. If file /opt/oracle/homes/OraDBHome21cEE/dbs/octs.dbf was unintentionally renamed or moved, restore it
Automated Repair Options
========================
Option Repair Description
------ ------------------
1 Restore and recover datafile 13
Strategy: The repair includes complete media recovery with no data loss
Repair script: /opt/oracle/diag/rdbms/cdb1/CDB1/hm/reco_2435873803.hm
RMAN> repair failure;
Strategy: The repair includes complete media recovery with no data loss
Repair script: /opt/oracle/diag/rdbms/cdb1/CDB1/hm/reco_2435873803.hm
contents of repair script:
# restore and recover datafile
sql 'PDB1' 'alter database datafile 13 offline';
restore ( datafile 13 );
recover datafile 13;
sql 'PDB1' 'alter database datafile 13 online';
Do you really want to execute the above repair (enter YES or NO)? YES
executing repair script
sql statement: alter database datafile 13 offline
Starting restore at 14-NOV-22
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 00013 to /opt/oracle/homes/OraDBHome21cEE/dbs/octs.dbf
channel ORA_DISK_1: reading from backup piece /opt/oracle/fast_recovery_area/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T131015_kq4hkqsz_.bkp
channel ORA_DISK_1: piece handle=/opt/oracle/fast_recovery_area/CDB1/E9C6D8B3A63EF2CEE0538200000A7F73/backupset/2022_11_14/o1_mf_nnndf_TAG20221114T131015_kq4hkqsz_.bkp tag=TAG20221114T131015
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 14-NOV-22
Starting recover at 14-NOV-22
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 14-NOV-22
sql statement: alter database datafile 13 online
repair failure complete
RMAN> list failure;
Database Role: PRIMARY
no failures found that match specification
错误已修复,此时打开数据库和查询数据正常:
alter pluggable database pdb1 open;
alter session set container = pdb1;
select * from appuser.regions;
还记得在任务2中空间不够的错误吗?我们试着用Data Recovery Advisor来修复一下。
RMAN> list failure;
Database Role: PRIMARY
no failures found that match specification
没想到,备份空间不够在这里不算是错误。
Oracle 数据库的闪回表使您能够将一个或多个表回退到之前的内容,而不会影响其他 Oracle 数据库对象。 因此,您可以从逻辑数据损坏中恢复,例如意外添加或删除表行。 与时间点恢复不同,Oracle 数据库在闪回操作期间保持可用。
目标
在使用闪回表之前,您必须确保在要闪回或返回到先前状态的表上启用行移动。 行移动表示闪回发生后行 ID 会发生变化。 存在此限制是因为如果应用程序在闪回之前存储行 ID,则无法保证行 ID 将对应于闪回之后的相同行。
可以针对具体的表操作:
connect / as sysdba;
alter session set container = pdb1;
alter table appuser.regions enable row movement;
记录修改前的状态:
SQL> select * from appuser.regions;
ID NAME
---------- --------------------
1 America
2 Europe
3 Asia
SQL> select to_char(current_timestamp,'YYYY-MM-DD HH:MI:SS') from dual;
TO_CHAR(CURRENT_TIM
-------------------
2022-11-14 08:52:59
修改数据:
SQL> update appuser.regions set name = 'ORACLE';
3 rows updated.
SQL> commit;
Commit complete.
SQL> select * from appuser.regions;
ID NAME
---------- --------------------
1 ORACLE
2 ORACLE
3 ORACLE
SQL> flashback table appuser.regions to timestamp to_timestamp('2022-11-14 08:52:59','YYYY-MM-DD HH:MI:SS');
Flashback complete.
SQL> select * from appuser.regions;
ID NAME
---------- --------------------
1 America
2 Europe
3 Asia
alter session set container=pdb1;
drop table appuser.regions;
select * from appuser.regions;
SQL> flashback table appuser.regions to before drop;
Flashback complete.
SQL> select * from appuser.regions;
ID NAME
---------- --------------------
1 America
2 Europe
3 Asia