Oracle数据备份与恢复RMAN
一、通过RMAN方式备份
通过 RMAN(Oracle 数据库备份和恢复管理器)方式备份 Oracle 数据库,可以使用以下步骤:
1. 启动归档模式 查看是否为存档模式:
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 11
Current log sequence 13
SQL>
关闭数据库,并启动归档模式
SQL> shutdown;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 1286066176 bytes
Fixed Size 2213016 bytes
Variable Size 352324456 bytes
Database Buffers 922746880 bytes
Redo Buffers 8781824 bytes
Database mounted.
SQL> alter database archivelog;
Database altered.
SQL>
打开数据库,并查看是否修改成功
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 11
Next log sequence to archive 13
Current log sequence 13
SQL>
如果想在归档模式下进行备份,只能把数据库启动到mount状态下才可以进行 但在生产环境中,一般情况下数据库是不能随意进行关闭的,所以一般在安装好数据库之后直接就开启归档模式,更改归档需要在mount模式下
2. 打开 RMAN 命令行界面:
[oracle@localhost ~]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 17:58:54 2023
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1679473799)
RMAN>
3.开始备份
(1) 使用backup database;对数据库直接备份
RMAN> backup database;
Starting backup at 03-NOV-23
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=192 device type=DISK
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/orcl/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 03-NOV-23
channel ORA_DISK_1: finished piece 1 at 03-NOV-23
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_nnndf_TAG20231103T194131_ln9q3d1w_.bkp tag=TAG20231103T194131 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 03-NOV-23
channel ORA_DISK_1: finished piece 1 at 03-NOV-23
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_ncsnf_TAG20231103T194131_ln9q3wok_.bkp tag=TAG20231103T194131 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 03-NOV-23
RMAN>
进入相关目录查看备份结果
在这里插入图片描述
[oracle@localhost ~]$ cd /u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/
[oracle@localhost 2023_11_03]$ ll
total 1016728
-rw-r----- 1 oracle oinstall 9830400 Nov 3 18:09 o1_mf_ncsnf_TAG20231103T180906_ln9kp3ff_.bkp
-rw-r----- 1 oracle oinstall 9830400 Nov 3 19:25 o1_mf_ncsnf_TAG20231103T192501_ln9p4gqt_.bkp
-rw-r----- 1 oracle oinstall 9830400 Nov 3 19:41 o1_mf_ncsnf_TAG20231103T194131_ln9q3wok_.bkp
-rw-r----- 1 oracle oinstall 1011638272 Nov 3 19:41 o1_mf_nnndf_TAG20231103T194131_ln9q3d1w_.bkp
[oracle@localhost 2023_11_03]$
对数据进行恢复(恢复数据时数据库也需要处于mount状态下):
run {
set until time "to_date('11/22/2023 10:00:00','mm/dd/yyyy hh24:mi:ss')";
restore database;
recover database;
alter database open resetlogs;
}
rman不但可以备份,也可以使用其进行管理,示例如下:
在这里插入图片描述
(2)使用backup tablespace USERS;对表空间的备份和恢复
RMAN> backup tablespace USERS;
Starting backup at 03-NOV-23
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=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 03-NOV-23
channel ORA_DISK_1: finished piece 1 at 03-NOV-23
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_nnndf_TAG20231103T223829_lnb1h5d1_.bkp tag=TAG20231103T223829 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 03-NOV-23
RMAN>
若表空间已损毁恢复,需要恢复表空间可以先使用restore tablespace USERS;再使用recover tablespace USERS;(3)使用backup archivelog all;对归档日志进行备份
RMAN> backup archivelog all;
Starting backup at 07-NOV-23
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=13 RECID=1 STAMP=1152203517
input archived log thread=1 sequence=14 RECID=2 STAMP=1152231422
channel ORA_DISK_1: starting piece 1 at 07-NOV-23
channel ORA_DISK_1: finished piece 1 at 07-NOV-23
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_07/o1_mf_annnn_TAG20231107T001702_lnl4cy94_.bkp tag=TAG20231107T001702 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 07-NOV-23
使用CROSSCHECK BACKUP命令验证备份的完整性。RMAN 将检查备份文件的状态,并将其标记为“EXPIRED”(过期)或“AVAILABLE”(可用)。示例如下:
RMAN> crosscheck backup;
using channel ORA_DISK_1
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_ncsnf_TAG20231103T180906_ln9kp3ff_.bkp RECID=1 STAMP=1151950147
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_ncsnf_TAG20231103T192501_ln9p4gqt_.bkp RECID=2 STAMP=1151954702
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_nnndf_TAG20231103T194131_ln9q3d1w_.bkp RECID=3 STAMP=1151955692
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_ncsnf_TAG20231103T194131_ln9q3wok_.bkp RECID=4 STAMP=1151955708
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2023_11_03/o1_mf_nnndf_TAG20231103T223829_lnb1h5d1_.bkp RECID=5 STAMP=1151966309
Crosschecked 5 objects
RMAN>
二、使用emp/imp和expdb/impdb工具进行备份和恢复
emp/imp是Oracle传统的备份和恢复工具,它们使用二进制格式将数据导出和导入。expdb/impdb是Oracle 10g及以上版本引入的新工具,它们使用XML格式将数据导出和导入。下面我们将详细讲解如何使用这些工具进行备份和恢复。
1. 使用emp/imp进行备份和恢复 emp和imp命令用于将Oracle数据库中的数据导出到一个文件中,以便进行备份或迁移。它们可以将整个数据库或单个表导出到一个文件中,然后再将文件导入到另一个数据库中。备份数据:
exp userid=username/password file=d:\backup\emp_backup.dmp full=y
上述命令将整个数据库导出到d:\backup\emp_backup.dmp文件中。
恢复数据:
imp userid=username/password file=d:\backup\emp_backup.dmp full=y
上述命令将d:\backup\emp_backup.dmp文件中的数据导入到数据库中。
2. 使用expdb/impdb进行备份和恢复expdb和impdb命令是Oracle 10g及以上版本引入的新工具,它们使用XML格式将数据导出和导入。它们的使用方法与emp/imp相似。备份数据:
expdp userid=username/password directory=backup_dir dumpfile=expdb_backup.dmp full=y
上述命令将整个数据库导出到backup_dir目录下的expdb_backup.dmp文件中。
恢复数据:
impdp userid=username/password directory=backup_dir dumpfile=expdb_backup.dmp full=y
上述命令将backup_dir目录下的expdb_backup.dmp文件中的数据导入到数据库中。
三、使用Data guard进行备份与恢复
Oracle Data Guard是Oracle数据库提供的一种高可用性解决方案,它可以实现数据备份和恢复的功能。它通过将主数据库的数据实时复制到一个或多个备用数据库,以提供数据冗余和灾难恢复能力。
下面是使用Data Guard进行备份和恢复的详细讲解:
1. 配置Data Guard 首先,需要在主数据库和备用数据库上配置Data Guard。在主数据库上进行以下操作:在备用数据库上进行以下操作:
2. 启动Data Guard 在主数据库上启动Data Guard:在备用数据库上启动Data Guard:
3. 数据备份和恢复 在Data Guard配置完成后,主数据库上的数据将实时复制到备用数据库。如果主数据库发生故障,可以使用备用数据库进行数据恢复。备份数据:恢复数据: