首页 > 代码库 > Oracle RMAN备份与还原
Oracle RMAN备份与还原
RMAN在数据库服务器的帮助下实现数据库文件、控制文件、数据库文件与控制文件的映像副本、归档日志文件、数据库服务器参数文件的备份。
RMAN的特点:
(1) 支持增量备份:传统的exp与expdp备份工具,只能实现一个完整备份而不能增量备份,RMAN采用备份级别实现增量备份,在一个完整的备份基础上采用增量备份可以大大减少备份的数量;
(2) 自动管理备份文件:RMAN备份的数据是RMAN自动管理的,包括文件名字,备份文件存储目录等;
(3) 自动化备份与恢复:在备份与恢复操作时,使用简单的指令就可以实现备份与恢复,执行过程完全有RMAN维护。
(4) 不产生重做信息: RMAN联机备份不产生重做信息。
(5) 支持映像复制: 使用RMAN可以实现映像复制,映像以操作系统的文件格式存在,这种复制类似于用户管理的脱机备份方式。
(6) 备份的数据文件压缩处理: RMAN提供一个参数,说明是否对备份文件进行压缩,压缩的备份文件以二进制文件格式存在,可以减少备份文件的存储空间。
(7) 备份文件有效性检查功能: 可以在备份之后恢复检测备份文件是否可用,避免无效恢复操作。
1 RMAN 脱机备份与还原
1.1 使用RMAN命令连接到数据库
[oracle@oracledb ~]$ rman target/
Recovery Manager: Release 11.2.0.1.0 - Production on Tue May 30 14:33:04 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1452257309)
1.2 脱机备份之前确定数据库处于非归档模式下,使用管理员账号登录到数据库查看
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 1
Current log sequence
1.3 脱机备份时数据必须处于mount状态下,关机启动数据库到mount状态下
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 450953216 bytes
Fixed Size 2214256 bytes
Variable Size 339740304 bytes
Database Buffers 104857600 bytes
Redo Buffers 4141056 bytes
Database mounted.
1.4 RMAN模式下输入脱机备份命令,系统会自动备份整个数据库到默认的目录下
RMAN> backup as compressed backupset database;
Starting backup at 30-MAY-17
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00005 name=/usr/oracle/app/oradata/orcl/CTRR_DATA.dbf
input datafile file number=00007 name=/home/oracle/data/CTRR_DATA_1.dbf
channel ORA_DISK_1: starting piece 1 at 30-MAY-17
channel ORA_DISK_1: finished piece 1 at 30-MAY-17
piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqjoft_.bkp tag=TAG20170530T105100 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00009 name=/home/oracle/data/rman_ts1.dbf
input datafile file number=00006 name=/usr/oracle/app/oradata/orcl/APSALU3_DATA.dbf
channel ORA_DISK_1: starting piece 1 at 30-MAY-17
channel ORA_DISK_1: finished piece 1 at 30-MAY-17
piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmdbx_.bkp tag=TAG20170530T105100 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/usr/oracle/app/oradata/orcl/system01.dbf
input datafile file number=00004 name=/usr/oracle/app/oradata/orcl/users01.dbf
input datafile file number=00008 name=/home/oracle/data/default_tablespace.dbf
input datafile file number=00003 name=/usr/oracle/app/oradata/orcl/undotbs01.dbf
input datafile file number=00002 name=/usr/oracle/app/oradata/orcl/sysaux01.dbf
channel ORA_DISK_1: starting piece 1 at 30-MAY-17
channel ORA_DISK_1: finished piece 1 at 30-MAY-17
piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmfgv_.bkp tag=TAG20170530T105100 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
Finished backup at 30-MAY-17
Starting Control File and SPFILE Autobackup at 30-MAY-17
piece handle=/usr/oracle/app/flash_recovery_area/ORCL/autobackup/2017_05_30/o1_mf_s_945338539_dlsqp4yv_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 30-MAY-17
以上备份信息可以看出,RMAN将数据文件备份在目录/usr/oracle/app/flash_recovery_area/ORCL/backupset/下,将控制文件与服务器参数文件备份在/usr/oracle/app/flash_recovery_area/ORCL/autobackup/下。这里需要注意如果想要RMAN在整库备份时自动备份控制文件与服务器参数文件,需要设置参数
configure controlfile autobackup的值为on: configure controlfile autobackup on;
RMAN> configure controlfile autobackup on;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored
此时使用RMAN完成了整个数据库的脱机备份。
备份完成之后用scott用户创建一个测试表,插入一条数据,等恢复完成之后看这个表是否还存在。
create table t_test1(id number, name varchar2(50));
insert into t_test1(id, name)
values(1,‘latiny1‘);
commit;
非归档模式下实现脱机备份恢复
为了测试利用脱机备份文件完全恢复数据库,我们删除部分数据文件、全部控制文件模拟数据库文件丢失,然后进行恢复。
[root@oracledb orcl]# ls -l
total 4867456
-rw-r----- 1 oracle oinstall 524296192 May 30 12:12 APSALU3_DATA.dbf
-rw-r----- 1 oracle oinstall 9748480 May 30 14:48 control01.ctl
-rw-r----- 1 oracle oinstall 2147491840 May 30 12:12 CTRR_DATA.dbf
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 14:48 redo01.log
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 12:12 redo02.log
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 12:12 redo03.log
-rwxrwxr-x 1 oracle oinstall 660611072 May 30 14:48 sysaux01.dbf
-rwxrwxr-x 1 oracle oinstall 754982912 May 30 14:48 system01.dbf
-rw-r----- 1 oracle oinstall 20979712 Mar 23 10:52 temp01.dbf
-rwxrwxr-x 1 oracle oinstall 723525632 May 30 14:48 undotbs01.dbf
-rw-r----- 1 oracle oinstall 5251072 May 30 12:12 users01.dbf
首先关闭数据库,然后删除users01.dbf、CTRR_DATA.dbf、control01.ctl、APSALU3_DATA.dbf,然后启动数据库:
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 450953216 bytes
Fixed Size 2214256 bytes
Variable Size 339740304 bytes
Database Buffers 104857600 bytes
Redo Buffers 4141056 bytes
ORA-00205: error in identifying control file, check alert log for more info
由于控制文件丢失,数据库无法启动到mount状态,先恢复控制文件再启动,恢复控制文件需要指定控制文件对应的备份文件路劲,之前的备份信息里可以获取得到:
RMAN> restore controlfile from ‘/usr/oracle/app/flash_recovery_area/ORCL/autobackup/2017_05_30/o1_mf_s_945338539_dlsqp4yv_.bkp‘;
Starting restore at 30-MAY-17
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/usr/oracle/app/oradata/orcl/control01.ctl
output file name=/usr/oracle/app/flash_recovery_area/orcl/control02.ctl
Finished restore at 30-MAY-17
控制文件恢复成功之后启动数据库到mount状态:
SQL> alter database mount;
Database altered.
启动到mount状态之后,在RMAN模式下恢复数据文件:
RMAN> restore database;
Starting restore at 30-MAY-17
released channel: ORA_DISK_1
Starting implicit crosscheck backup at 30-MAY-17
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
Crosschecked 11 objects
Finished implicit crosscheck backup at 30-MAY-17
Starting implicit crosscheck copy at 30-MAY-17
using channel ORA_DISK_1
Finished implicit crosscheck copy at 30-MAY-17
searching for all files in the recovery area
cataloging files...
cataloging done
List of Cataloged Files
=======================
File Name: /usr/oracle/app/flash_recovery_area/ORCL/autobackup/2017_05_30/o1_mf_s_945338539_dlsqp4yv_.bkp
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 00005 to /usr/oracle/app/oradata/orcl/CTRR_DATA.dbf
channel ORA_DISK_1: restoring datafile 00007 to /home/oracle/data/CTRR_DATA_1.dbf
channel ORA_DISK_1: reading from backup piece /usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqjoft_.bkp
channel ORA_DISK_1: ORA-19870: error while restoring backup piece /usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqjoft_.bkp
ORA-19502: write error on file "/usr/oracle/app/oradata/orcl/CTRR_DATA.dbf", block number 216192 (block size=8192)
ORA-27072: File I/O error
Linux-x86_64 Error: 25: Inappropriate ioctl for device
Additional information: 4
Additional information: 216192
Additional information: 6963
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 00006 to /usr/oracle/app/oradata/orcl/APSALU3_DATA.dbf
channel ORA_DISK_1: restoring datafile 00009 to /home/oracle/data/rman_ts1.dbf
channel ORA_DISK_1: reading from backup piece /usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmdbx_.bkp
channel ORA_DISK_1: piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmdbx_.bkp tag=TAG20170530T105100
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:01:25
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 00001 to /usr/oracle/app/oradata/orcl/system01.dbf
channel ORA_DISK_1: restoring datafile 00002 to /usr/oracle/app/oradata/orcl/sysaux01.dbf
channel ORA_DISK_1: restoring datafile 00003 to /usr/oracle/app/oradata/orcl/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00004 to /usr/oracle/app/oradata/orcl/users01.dbf
channel ORA_DISK_1: restoring datafile 00008 to /home/oracle/data/default_tablespace.dbf
channel ORA_DISK_1: reading from backup piece /usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmfgv_.bkp
channel ORA_DISK_1: piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqmfgv_.bkp tag=TAG20170530T105100
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:02:25
failover to previous backup
creating datafile file number=5 name=/usr/oracle/app/oradata/orcl/CTRR_DATA.dbf
Finished restore at 30-MAY-17
其他文件正常恢复,CTRR_DATA.dbf数据文件时遇到错误:
ORA-19502: write error on file "/usr/oracle/app/oradata/orcl/CTRR_DATA.dbf", block number 216192 (block size=8192)
ORA-27072: File I/O error
Linux-x86_64 Error: 25: Inappropriate ioctl for device
查了一下错误原因,发现因为此数据文件较大恢复时导致磁盘空间不足引起的,于是删除一些文件释放磁盘重新执行命令恢复成功:
RMAN> restore database;
Starting restore at 30-MAY-17
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
skipping datafile 1; already restored to file /usr/oracle/app/oradata/orcl/system01.dbf
skipping datafile 2; already restored to file /usr/oracle/app/oradata/orcl/sysaux01.dbf
skipping datafile 3; already restored to file /usr/oracle/app/oradata/orcl/undotbs01.dbf
skipping datafile 4; already restored to file /usr/oracle/app/oradata/orcl/users01.dbf
skipping datafile 8; already restored to file /home/oracle/data/default_tablespace.dbf
skipping datafile 7; already restored to file /home/oracle/data/CTRR_DATA_1.dbf
skipping datafile 6; already restored to file /usr/oracle/app/oradata/orcl/APSALU3_DATA.dbf
skipping datafile 9; already restored to file /home/oracle/data/rman_ts1.dbf
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 00005 to /usr/oracle/app/oradata/orcl/CTRR_DATA.dbf
channel ORA_DISK_1: reading from backup piece /usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqjoft_.bkp
channel ORA_DISK_1: piece handle=/usr/oracle/app/flash_recovery_area/ORCL/backupset/2017_05_30/o1_mf_nnndf_TAG20170530T105100_dlsqjoft_.bkp tag=TAG20170530T105100
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:02:26
Finished restore at 30-MAY-17
然后去之前存放数据文件、控制文件的目录下看一下文件是否都恢复成功:
[root@oracledb orcl]# ls -l
total 4867456
-rw-r----- 1 oracle oinstall 524296192 May 30 12:12 APSALU3_DATA.dbf
-rw-r----- 1 oracle oinstall 9748480 May 30 14:48 control01.ctl
-rw-r----- 1 oracle oinstall 2147491840 May 30 12:12 CTRR_DATA.dbf
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 14:48 redo01.log
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 12:12 redo02.log
-rwxrwxr-x 1 oracle oinstall 52429312 May 30 12:12 redo03.log
-rwxrwxr-x 1 oracle oinstall 660611072 May 30 14:48 sysaux01.dbf
-rwxrwxr-x 1 oracle oinstall 754982912 May 30 14:48 system01.dbf
-rw-r----- 1 oracle oinstall 20979712 Mar 23 10:52 temp01.dbf
-rwxrwxr-x 1 oracle oinstall 723525632 May 30 14:48 undotbs01.dbf
-rw-r----- 1 oracle oinstall 5251072 May 30 12:12 users01.dbf
使用recover 命令恢复数据库,由于是脱机备份恢复不使用重做日志恢复模式:
RMAN> recover database noredo;
Starting recover at 30-MAY-17
using channel ORA_DISK_1
Finished recover at 30-MAY-17
然后打开数据库到open状态:
SQL> alter database open resetlogs;
Database altered.
查看备份之后scott用户创建的表,已经不存在了,自备份之后的数据全部丢失。
Oracle RMAN备份与还原