Oracle database backup and recovery [RMAN]

1. Understand backup and recovery

1.1, what is backup and recovery

Database backups are usually divided into the following two types:
1. Physical backup: Back up the physical files that make up the database (including data files, control files, archived redo log files), that is, the same copy of the database physical files stored in other locations, such as Offline disk or tape devices;
2. Logical backup: refers to backing up the logical data in the database, such as data tables and stored procedures, which can be exported to binary files using Oracle's data pump tool, and imported into the database when the database is restored.
The relationship between 1 and 2: Physical backup is the basis for making any backup strategy. Logical backup can be regarded as an effective supplement to physical backup, but physical backup requires longer backup and recovery time, while logical backup can only be used for important database data Performing backups can achieve faster backup and recovery operations. Therefore, in actual work, physical backups must be performed, as well as instant logical backups of important data.
In the actual work process, there are three types of faults that need to be handled by the DBA. They are media faults, user errors or application errors. Other errors will not cause data loss or need to be restored from backups, such as the need to restart the database. In order to recover the failure of the database instance. The following is a detailed description of these three types of DBA errors that need to be backed up and restored:

  1. Media failure, such as the failure of reading or writing to the disk due to physical disk damage, requires media recovery to achieve data integrity. For such failures, the DBA must develop a complete backup and recovery strategy to prevent catastrophic data lost.
    Database backup and media recovery
  2. User error
  3. Application error
  4. Instance recovery
  5. The media recovery
    DBA must try to formulate effective backup and recovery strategies, reduce the mean recovery time (MTTR) and extend the mean time between failures (MTBF), and regularly check the effectiveness of the backup and recovery strategy (whether automatic backup can be performed normally, after backup The file can be restored normally).

1.2 Methods of backup and recovery

1. There are three ways to perform Oracle backup and recovery:

  1. Use RMAN Recovery Manager for backup and recovery. RMAN is Oracle's built-in backup and recovery program and does not need to be installed separately. You can also manage database instances through the Database Control interface;
  2. User management backup and recovery, this is the use of operating system commands to backup and restore database files;
  3. Use SQL*Loader or data pump import and export tools for logical backup and recovery. This method can back up important data, and can realize data transmission between different operating systems or different Oracle versions.
    But Oracke recommends using RMAN to complete the backup and recovery work. The advantage is that users do not need to remember the backup data files and archived redo log files. RMAN can record this information in its own storage location, and RMAAN can verify the validity of the internal data blocks of the backup file. And can record the copy in the database.
    2. The way of backup
  4. Consistent backup: The system change number (SCN) in each file included in the backup is the same, that is, all data in each file included in the backup comes from the same point in time.
  5. Non-consistent backup: refers to files containing data from different points in time. This is mainly used for online backup mode, that is, the database backs up data files when processing transactions. In addition to restoring data, this backup method also requires archiving and online Redo log files are restored to restore database data to a specific point in time.
  6. A complete database backup refers to the backup of all data files and control files in the database. It is the most commonly used backup type. Completely classified can be either a consistent backup or an inconsistent backup.
  7. Partially back up the database and back up part of the contents of the database, such as a table or a data file. This method is generally seldom used.
  8. Online backup refers to the backup of the database during operation, also known as hot backup, but only when the database is running in archive mode can online backup of the entire database be performed. If the database is in non-archive mode, it can only be performed Offline backup.
  9. Offline backup, the backup when the data is in the closed state is also a hot backup. As long as the database is not closed by SHUTDOWN ABORT, the backup is always a consistent backup, otherwise the backup is an inconsistent backup. After the Fuyuan database, it needs to be re-applied. Make log files to maintain a consistent state.

2. Use RMAN to backup

2.1, set the archive log mode

The first step: Use ARCHIVE LOG LIST in SQL*PLUS or query the v$database data dictionary view to view the current database mode.
1. Use the sysdba account to log in to the database in the operating system

1、sqlplus / as sysdba;--登陆本机数据库
2、sqlplus [username]/[password]@[IP]:1521/[SID];--远程登陆数据库

Step 2: Query the log_mode field of the v$database data dictionary view, or use ARCHIVE LOG LIST to view the mode of the database.

set linesize 500;--格式化输出结果宽度
select dbid,name,log_mode,platform_name from v$database;--使用v$database查询

The results are as follows: NOARCHIVELOG stands for non-archive mode
v$database query results
or

archive log list;

The results are as follows:
crchive log list query result
Step 2: From the results of the first step, you can see that the current database is in non-archive mode. Now use the following steps to set the database to archive mode:
(1) Use the SHUTDOWN command to shut down the database instance, and then start the database To the MOUNT state, the example is as follows:

SQL> SHUTDOWN IMMEDIATE;--关闭数据库实例
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。
SQL> STARTUP MOUNT;--启动数据库到MOUNT状态
ORACLE 例程已经启动。

Total System Global Area 3373858816 bytes
Fixed Size                  2180424 bytes
Variable Size            2348812984 bytes
Database Buffers         1006632960 bytes
Redo Buffers               16232448 bytes
数据库装载完毕。

(2), use ALTER DATABASE ARCHIVELOG statement to set the database to archive log mode, or use ALTER DATABASE NOARCHIVELOG to set the database to non-archive log mode, the statement is as follows:

SQL> ALTER DATABASE ARCHIVELOG;

数据库已更改。

(3) Set the database to open state, the statement is as follows:

SQL> ALTER DATABASE OPEN;

数据库已更改。

(4) At this time, the database is already running in archive mode, you can use ARCHIVE LOG ALL to archive the redo log files. The example statement is as follows:

SQL> ARCHIVE LOG ALL;
ORA-00271: 没有需要归档的日志

As shown in the above result, the error ORA-00271: No log to be archived is reported. This may be because the archive has been archived or the log file group has not yet started to write. You can use the following command to archive the current log group.
(5) Solve the error message ORA-00271

SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;

系统已更改。

The third step: At this point, the database can automatically complete the archive. At this time, you can again use the sentence in the "second step" to query the archive log storage location.

SQL> ARCHIVE LOG LIST;
数据库日志模式            存档模式
自动存档             启用
存档终点            USE_DB_RECOVERY_FILE_DEST--归档日志默认使用闪回区域
最早的联机日志序列     593
下一个存档日志序列   595
当前日志序列           595

If you need to view the archive log file, you can execute the following command:

C:\>RMAN TARGET /   --使用rman target / 命令登陆RAMN并连接数据库,或者使用rman target [username]/[passwd]@[IP]:1521/[SID]连接目标数据库

恢复管理器: Release 11.2.0.1.0 - Production on 星期四 1017 17:46:06 2019

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

连接到目标数据库: ORCL (DBID=1547773451)

RMAN> crosscheck archivelog all;--查看所有的归档日志文件

使用目标数据库控制文件替代恢复目录
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: SID=129 设备类型=DISK
对归档日志的验证成功
归档日志文件名=D:\APP\MINGYUAN\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2019_10_17\O1_MF_1_594_GTJ9Q3HR_.ARC RECID=1 STAMP=1021912291  --这个就是归档日志存储位置
已交叉检验的 1 对象
RMAN>

2.2, complete database backup

Create a completed database backup, back up all database files and SPFILE files, copy the Oracle installation directory, the statement is as follows:

2.2.1, specify the location of the backup set

RMAN> BACKUP DATABASE FORMAT 'C:\backup\%d_bak_%U';--%d指定数据库名称,%U指定产生一个唯一的命名,%t指定备份集的时间戳,%s指定备份集编号及%p指定备份片编号。

启动 backup18-10-19
使用通道 ORA_DISK_1
通道 ORA_DISK_1: 正在启动全部数据文件备份集
通道 ORA_DISK_1: 正在指定备份集内的数据文件
输入数据文件: 文件号=00005 名称=D:\APP\ORADATA\ORCL\XTDATA.DBF
输入数据文件: 文件号=00003 名称=D:\APP\ORADATA\ORCL\UNDOTBS01.DBF
输入数据文件: 文件号=00004 名称=D:\APP\ORADATA\ORCL\USERS01.DBF
输入数据文件: 文件号=00001 名称=D:\APP\ORADATA\ORCL\SYSTEM01.DBF
输入数据文件: 文件号=00002 名称=D:\APP\ORADATA\ORCL\SYSAUX01.DBF
通道 ORA_DISK_1: 正在启动段 118-10-19
通道 ORA_DISK_1: 已完成段 118-10-19
段句柄=C:\BACKUP\ORCL_BAK_04UEKOF4_1_1 标记=TAG20191018T150404 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:05:25
通道 ORA_DISK_1: 正在启动全部数据文件备份集
通道 ORA_DISK_1: 正在指定备份集内的数据文件
备份集内包括当前控制文件
备份集内包括当前的 SPFILE
通道 ORA_DISK_1: 正在启动段 118-10-19
通道 ORA_DISK_1: 已完成段 118-10-19
段句柄=C:\BACKUP\ORCL_BAK_05UEKOP9_1_1 标记=TAG20191018T150404 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:01
完成 backup18-10-19

RMAN>

2.2.2, use the flashback recovery area to create backups by default

RMAN> BACKUP AS BACKUPSET DATABASE SPFILE;

启动 backup18-10-19
使用目标数据库控制文件替代恢复目录
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: SID=192 设备类型=DISK
通道 ORA_DISK_1: 正在启动全部数据文件备份集
通道 ORA_DISK_1: 正在指定备份集内的数据文件
输入数据文件: 文件号=00005 名称=D:\APP\ORADATA\ORCL\XTDATA.DBF
输入数据文件: 文件号=00003 名称=D:\APP\ORADATA\ORCL\UNDOTBS01.DBF
输入数据文件: 文件号=00004 名称=D:\APP\ORADATA\ORCL\USERS01.DBF
输入数据文件: 文件号=00001 名称=D:\APP\ORADATA\ORCL\SYSTEM01.DBF
输入数据文件: 文件号=00002 名称=D:\APP\ORADATA\ORCL\SYSAUX01.DBF
通道 ORA_DISK_1: 正在启动段 118-10-19
RMAN-03009: backup 命令 (ORA_DISK_1 通道上,10/18/2019 14:32:33) 失败
ORA-19809: 超出了恢复文件数的限制
ORA-19804: 无法回收 52428800 字节磁盘空间 (4102029312 限制中)
继续执行其他作业步骤, 将不重新运行失败的作业
通道 ORA_DISK_1: 正在启动全部数据文件备份集
通道 ORA_DISK_1: 正在指定备份集内的数据文件
备份集内包括当前控制文件
备份集内包括当前的 SPFILE
通道 ORA_DISK_1: 正在启动段 118-10-19
通道 ORA_DISK_1: 已完成段 118-10-19
段句柄=D:\APP\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2019_10_18\O1_MF_NCSNF_TAG20191018T143217_GTLQ426G_.BKP 标记=TAG20191018T143217 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:01
通道 ORA_DISK_1: 正在启动全部数据文件备份集
通道 ORA_DISK_1: 正在指定备份集内的数据文件
备份集内包括当前的 SPFILE
通道 ORA_DISK_1: 正在启动段 118-10-19
通道 ORA_DISK_1: 已完成段 118-10-19
段句柄=D:\APP\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2019_10_18\O1_MF_NNSNF_TAG20191018T143217_GTLQ439W_.BKP 标记=TAG20191018T143217 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:01
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================

RMAN-03009: backup 命令 (ORA_DISK_1 通道上,10/18/2019 14:32:33) 失败
ORA-19809: 超出了恢复文件数的限制
ORA-19804: 无法回收 52428800 字节磁盘空间 (4102029312 限制中)

RMAN>

As shown above, an error message appears, which means that the archive file is too large and the archive fails. db_recovery_file_desc has a size limit, and the default is 2G.
Solution: Delete redundant archive files. Then set a larger db_recovery_file_dest_size.
The process is as follows:
(1) View the flash recovery area space

C:\Users>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期五 1018 16:07:10 2019

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


连接到:
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 * from v$recovery_file_dest;--space_limit:该区域的大小是多少,space_used:已经使用了多少空间,space_reclaimable:删除一些垃圾数据之后可以回收的空间大小(如obsolete,redundant、low priority),number_of_files:说明该区域目前存在有多少文件

NAME                                           SPACE_LIMIT SPACE_USED        SPACE_RECLAIMABLE NUMBER_OF_FILES
---------------------------------------------- ----------  ----------------- ----------------- ---------------
D:\app\flash_recovery_area                     4102029312  372635648         372635648         12
SQL>

(2), query and delete invalid archive log files

C:\Users>rman target /
RMAN>crosscheck archivelog all;   -- 运行这个命令可以把无效的expired的archivelog标出来。
RMAN>delete expired archivelog all;  -- 直接全部删除过期的归档日志。

(3) If the archive log file cannot be deleted, you can reset the archive log file, allocate more space, and configure a reasonable coverage strategy. [For specific methods, please refer to relevant Blog articles]

2.2.3, query, delete backup set


RMAN> LIST BACKUP OF DATABASE;--查询备份数据库文件


备份集列表
===================

BS 关键字  类型 LV 大小       设备类型 经过时间 完成时间
------- ---- -- ---------- ----------- ------------ ----------
3       Full    11.13G     DISK        00:05:17     18-10-19
        BP 关键字: 3   状态: AVAILABLE  已压缩: NO  标记: TAG20191018T150404
段名:C:\BACKUP\ORCL_BAK_04UEKOF4_1_1
  备份集 3 中的数据文件列表
  文件 LV 类型 Ckp SCN    Ckp 时间   名称
  ---- -- ---- ---------- ---------- ----
  1       Full 4228805    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\SYSTEM01.DBF
  2       Full 4228805    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\SYSAUX01.DBF
  3       Full 4228805    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\UNDOTBS01.DBF
  4       Full 4228805    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\USERS01.DBF
  5       Full 4228805    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\XTDATA.DBF

BS 关键字  类型 LV 大小       设备类型 经过时间 完成时间
------- ---- -- ---------- ----------- ------------ ----------
5       Full    11.13G     DISK        00:03:34     18-10-19
        BP 关键字: 5   状态: AVAILABLE  已压缩: NO  标记: TAG20191018T154338
段名:C:\BACKUP\HYYS0508_BAK_06UEKQPA_1_1
  备份集 5 中的数据文件列表
  文件 LV 类型 Ckp SCN    Ckp 时间   名称
  ---- -- ---- ---------- ---------- ----
  1       Full 4230473    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\SYSTEM01.DBF
  2       Full 4230473    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\SYSAUX01.DBF
  3       Full 4230473    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\UNDOTBS01.DBF
  4       Full 4230473    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\USERS01.DBF
  5       Full 4230473    18-10-19 D:\APP\MINGYUAN\ORADATA\ORCL\XTDATA.DBF

RMAN> DELETE BACKUPSET 5;--删除BS关键字为5的备份数据库文件

使用通道 ORA_DISK_1

备份片段列表
BP 关键字  BS 关键字  Pc# Cp# 状态      设备类型段名称
------- ------- --- --- ----------- ----------- ----------
5       5       1   1   AVAILABLE   DISK        C:\BACKUP\HYYS0508_BAK_06UEKQPA_1_1

是否确定要删除以上对象 (输入 YES 或 NO)? Y
已删除备份片段
备份片段句柄=C:\BACKUP\HYYS0508_BAK_06UEKQPA_1_1 RECID=5 STAMP=1021995818
1 对象已删除

RMAN>

3. Use RMAN to restore the database

There are two operations for RMAN recovery:

  • Database restoration [RESTORE] refers to using the data file of the backup set to replace the damaged database file or restore it to a new location.
  • Database recovery [RECOVER], apply all archived redo logs to restore the database to a healthy state, or apply only part of REDO to restore the database to a specified point in time.
    When performing database recovery, you need to enter the entire database into the MOUNT state. If you only restore individual table spaces or data files, you can operate in the OPEN state.
    During the recovery operation, you can use full recovery to recover to the most recent point in time, or partial recovery to recover to a specific point in time. After the recovery is complete, open the database, or if it is not completely recovered, you need to specify RESETLOGS to open the database.
    The steps to completely restore the database are as follows:
    (1) Start the database to MOUNT state
RMAN> STARTUP MOUNT;--需要数据库已经被SHUTDOWN才能恢复到这个状态,如果是正常的数据库需要使用管理员账号进行SHUTDOWN IMMEDIATE之后才能进行该操作。
Oracle 实例已启动
数据库已装载
系统全局区域总计    3373858816 字节
Fixed Size                     2180424 字节
Variable Size               2348812984 字节
Database Buffers            1006632960 字节
Redo Buffers                  16232448 字节

RMAN>

(2) Use RESTORE DATABASE to restore the database

RMAN> RESTORE DATABASE;

启动 restore18-10-19
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: SID=212 设备类型=DISK

通道 ORA_DISK_1: 正在开始还原数据文件备份集
通道 ORA_DISK_1: 正在指定从备份集还原的数据文件
通道 ORA_DISK_1: 将数据文件 00001 还原到 D:\APP\MINGYUAN\ORADATA\ORCL\SYSTEM01.DBF
通道 ORA_DISK_1: 将数据文件 00002 还原到 D:\APP\MINGYUAN\ORADATA\ORCL\SYSAUX01.DBF
通道 ORA_DISK_1: 将数据文件 00003 还原到 D:\APP\MINGYUAN\ORADATA\ORCL\UNDOTBS01.DBF
通道 ORA_DISK_1: 将数据文件 00004 还原到 D:\APP\MINGYUAN\ORADATA\ORCL\USERS01.DBF
通道 ORA_DISK_1: 将数据文件 00005 还原到 D:\APP\MINGYUAN\ORADATA\ORCL\XTDATA.DBF
通道 ORA_DISK_1: 正在读取备份片段 C:\BACKUP\HYYS0508_BAK_08UELAFM_1_1
通道 ORA_DISK_1: 段句柄 = C:\BACKUP\HYYS0508_BAK_08UELAFM_1_1 标记 = TAG20191018T201134
通道 ORA_DISK_1: 已还原备份片段 1
通道 ORA_DISK_1: 还原完成, 用时: 00:01:55
完成 restore18-10-19

RMAN>

(3) After restoring the database, if the database has not generated any transactions after the backup, the restoration operation is now complete.
Otherwise, you need to use RECOVER for recovery operations. The operation method is as follows:

RMAN> RECOVER DATABASE;

启动 recover 于 18-10-19
使用通道 ORA_DISK_1
正在开始介质的恢复
介质恢复完成, 用时: 00:00:01

完成 recover 于 18-10-19

RMAN>

(4) Switch the restored database to the OPEN state to complete the restoration of the database.

RMAN> ALTER DATABASE OPEN;
数据库已打开

Note: The above recovery commands can be executed in batches using RUN

RMAN>RUN{
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
RESTORE DATABASE;
RECOVER DATABASE;
ALTER DATABASE OPEN;
}

Guess you like

Origin blog.csdn.net/sinat_31633205/article/details/102602803