由于主库和备库的db_name相同的,只是db_unique_name不同,可以使用备库进行数据库的备份,从而减轻主库备份的负担。本篇演示DataGuard环境下RMAN工具的使用。
1 查看主库RMAN配置
[oracle@sz ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 21:49:56 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
RMAN> show all;
RMAN configuration parameters for database with db_unique_name ORCL are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # 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 DB_UNIQUE_NAME 'orcl' CONNECT IDENTIFIER 'shzhen';
CONFIGURE DB_UNIQUE_NAME 'orcldg' CONNECT IDENTIFIER 'shhai';
CONFIGURE RMAN OUTPUT TO KEEP FOR 7 DAYS; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/12.1.0/dbhome_1/dbs/snapcf_orcl.f'; # default
RMAN>
2 查看备库RMAN配置
[oracle@sh ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 21:50:16 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
RMAN> show all;
RMAN configuration parameters for database with db_unique_name ORCLDG are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON;
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 DB_UNIQUE_NAME 'orcl' CONNECT IDENTIFIER 'shzhen';
CONFIGURE DB_UNIQUE_NAME 'orcldg' CONNECT IDENTIFIER 'shhai';
CONFIGURE RMAN OUTPUT TO KEEP FOR 7 DAYS; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/12.1.0/dbhome_1/dbs/snapcf_orcldg.f'; # default
RMAN>
3 备库进行数据库的备份
对主备用数据库进行RMAN配置后,接下来就可以将物理备库作为RMAN的目标数据库进行连接,将原来在主库上的备份操作完全移至备库。由于备库本质上是主库的物理副本,因此,不论在主库备份,还是在备库备份,在恢复和还原数据库时,两者时通用的、可以互换的,但需注意的是,在RMAN中查看和管理备份时,它们还是和dB_UNIQUE_NAME相关联的。
[oracle@sh ~]$ rman target sys/system@shhai catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 22:49:07 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
RMAN> backup database plus archivelog;
Starting backup at 26-APR-20
current log archived at primary database
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
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=12 RECID=1 STAMP=1038160947
input archived log thread=1 sequence=13 RECID=2 STAMP=1038160948
input archived log thread=1 sequence=14 RECID=3 STAMP=1038161310
input archived log thread=1 sequence=15 RECID=4 STAMP=1038161793
input archived log thread=1 sequence=16 RECID=5 STAMP=1038164237
input archived log thread=1 sequence=17 RECID=7 STAMP=1038341915
input archived log thread=1 sequence=18 RECID=6 STAMP=1038341913
input archived log thread=1 sequence=19 RECID=8 STAMP=1038344530
input archived log thread=1 sequence=20 RECID=9 STAMP=1038344716
input archived log thread=1 sequence=21 RECID=11 STAMP=1038345256
input archived log thread=1 sequence=22 RECID=13 STAMP=1038351398
input archived log thread=1 sequence=23 RECID=16 STAMP=1038351676
input archived log thread=1 sequence=24 RECID=18 STAMP=1038351701
input archived log thread=1 sequence=25 RECID=17 STAMP=1038351701
input archived log thread=1 sequence=26 RECID=19 STAMP=1038768564
input archived log thread=1 sequence=27 RECID=20 STAMP=1038768690
input archived log thread=1 sequence=28 RECID=21 STAMP=1038768781
input archived log thread=1 sequence=29 RECID=22 STAMP=1038768877
input archived log thread=1 sequence=30 RECID=23 STAMP=1038772825
input archived log thread=1 sequence=31 RECID=24 STAMP=1038782819
input archived log thread=1 sequence=32 RECID=25 STAMP=1038782819
input archived log thread=1 sequence=33 RECID=26 STAMP=1038782826
input archived log thread=1 sequence=34 RECID=27 STAMP=1038782922
input archived log thread=1 sequence=35 RECID=28 STAMP=1038782959
channel ORA_DISK_1: starting piece 1 at 26-APR-20
channel ORA_DISK_1: finished piece 1 at 26-APR-20
piece handle=/u01/app/oracle/fast_recovery_area/ORCLDG/backupset/2020_04_26/o1_mf_annnn_TAG20200426T224922_hbc7vlqf_.bkp tag=TAG20200426T224922 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
Finished backup at 26-APR-20
Starting backup at 26-APR-20
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=00005 name=/u01/app/oracle/oradata/orcldg/example01.dbf
input datafile file number=00001 name=/u01/app/oracle/oradata/orcldg/system01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/orcldg/sysaux01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/orcldg/undotbs01.dbf
input datafile file number=00006 name=/u01/app/oracle/oradata/orcldg/users01.dbf
channel ORA_DISK_1: starting piece 1 at 26-APR-20
channel ORA_DISK_1: finished piece 1 at 26-APR-20
piece handle=/u01/app/oracle/fast_recovery_area/ORCLDG/backupset/2020_04_26/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp tag=TAG20200426T224929 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
Finished backup at 26-APR-20
Starting backup at 26-APR-20
current log archived at primary database
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=36 RECID=29 STAMP=1038783056
channel ORA_DISK_1: starting piece 1 at 26-APR-20
channel ORA_DISK_1: finished piece 1 at 26-APR-20
piece handle=/u01/app/oracle/fast_recovery_area/ORCLDG/backupset/2020_04_26/o1_mf_annnn_TAG20200426T225059_hbc7ympo_.bkp tag=TAG20200426T225059 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 26-APR-20
Starting Control File and SPFILE Autobackup at 26-APR-20
piece handle=/u01/app/oracle/fast_recovery_area/ORCLDG/autobackup/2020_04_26/o1_mf_s_1038782959_hbc7ynyn_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 26-APR-20
RMAN>
4 模拟主库故障,恢复主库
4.1 有备份,删除主库数据文件,进行恢复
1)删除数据文件
[oracle@sz ~]$ cd /u01/app/oracle/oradata/orcl/
[oracle@sz orcl]$ ll
total 3362568
-rw-r----- 1 oracle asmadmin 10076160 Apr 26 22:53 control01.ctl
-rw-r----- 1 oracle asmadmin 1304174592 Apr 26 22:49 example01.dbf
-rw-r----- 1 oracle asmadmin 52429312 Apr 26 22:50 redo01.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 26 22:53 redo02.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 26 22:49 redo03.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 21 23:00 redo04.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 21 23:01 redo05.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 21 23:01 redo06.log
-rw-r----- 1 oracle asmadmin 52429312 Apr 21 23:01 redo07.log
-rw-r----- 1 oracle asmadmin 744497152 Apr 26 22:49 sysaux01.dbf
-rw-r----- 1 oracle asmadmin 838868992 Apr 26 22:49 system01.dbf
-rw-r----- 1 oracle asmadmin 206577664 Apr 26 19:00 temp01.dbf
-rw-r----- 1 oracle asmadmin 167780352 Apr 26 22:49 undotbs01.dbf
-rw-r----- 1 oracle asmadmin 5251072 Apr 26 22:49 users01.dbf
[oracle@sz orcl]$ rm users01.dbf
2)数据库出错
[oracle@sz orcl]$ sqlplus scott/scott@shzhen
SQL*Plus: Release 12.1.0.2.0 Production on Sun Apr 26 22:54:17 2020
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
SQL> select count(1) from scott.emp;
select count(1) from scott.emp
*
ERROR at line 1:
ORA-01116: error in opening database file 6
ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/users01.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
3)查看备份信息
[oracle@sz ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 22:55:30 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
RMAN> list backup summary;
specification does not match any backup in the repository
RMAN> list backup summary for db_unique_name orcldg;
List of Backups for database with db_unique_name ORCLDG
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
621 B A A DISK 26-APR-20 1 1 NO TAG20200426T224922
654 B F A DISK 26-APR-20 1 1 NO TAG20200426T224929
691 B A A DISK 26-APR-20 1 1 NO TAG20200426T225059
692 B F A DISK 26-APR-20 1 1 NO TAG20200426T225100
RMAN> list backupset 654 for db_unique_name orcldg;
List of Backup Set for database with db_unique_name ORCLDG
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
654 Full 1.42G DISK 00:01:18 26-APR-20
BP Key: 679 Status: AVAILABLE Compressed: NO Tag: TAG20200426T224929
Piece Name: /u01/app/oracle/fast_recovery_area/ORCLDG/backupset/2020_04_26/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp
List of Datafiles in backup set 654
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
1 Full 2222976 26-APR-20 /u01/app/oracle/oradata/orcl/system01.dbf
3 Full 2222976 26-APR-20 /u01/app/oracle/oradata/orcl/sysaux01.dbf
4 Full 2222976 26-APR-20 /u01/app/oracle/oradata/orcl/undotbs01.dbf
5 Full 2222976 26-APR-20 /u01/app/oracle/oradata/orcl/example01.dbf
6 Full 2222976 26-APR-20 /u01/app/oracle/oradata/orcl/users01.dbf
RMAN> catalog backuppiece '/home/oracle/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp';
cataloged backup piece
backup piece handle=/home/oracle/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp RECID=2 STAMP=1038785184
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
621 B A A DISK 26-APR-20 1 1 NO TAG20200426T224922
654 B F A DISK 26-APR-20 1 1 NO TAG20200426T224929
4)还原数据文件
RMAN> restore datafile 6 from tag='TAG20200426T224929';
Starting restore at 26-APR-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=24 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 00006 to /u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp
channel ORA_DISK_1: piece handle=/home/oracle/o1_mf_nnndf_TAG20200426T224929_hbc7vtob_.bkp tag=TAG20200426T224929
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 26-APR-20
RMAN> recover datafile 6;
Starting recover at 26-APR-20
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 26-APR-20
RMAN> alter database open;
Statement processed
5)查看还原后的文件
[oracle@sz orcl]$ ll users01.dbf
-rw-r----- 1 oracle asmadmin 5251072 Apr 26 23:28 users01.dbf
4.2 无备份,删除主库数据文件,进行恢复
1)主备库均无备份可用
[oracle@sz ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 23:33:28 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
RMAN> list backup for db_unique_name orcl;
specification does not match any backup in the repository
RMAN> list backup for db_unique_name orcldg;
specification does not match any backup in the repository
2)删除数据文件
[oracle@sz orcl]$ rm users01.dbf
[oracle@sz orcl]$ ll users01.dbf
ls: cannot access users01.dbf: No such file or directory
3)启动数据库出错
SQL> startup
ORACLE instance started.
Total System Global Area 1644167168 bytes
Fixed Size 2925024 bytes
Variable Size 973082144 bytes
Database Buffers 654311424 bytes
Redo Buffers 13848576 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/users01.dbf'
4)利用物理备库的数据文件还原主库损坏的数据文件
在RMAN中将备库作为目标数据库,将主库作为辅助数据库连接,同时连接恢复目录。
[oracle@sh ~]$ rman target sys/system@shhai auxiliary sys/system@shzhen catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 23:37:03 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
connected to auxiliary database: ORCL (DBID=1565938253, not open)
RMAN> backup as copy datafile 6 auxiliary format '/home/oracle/users_from_std.dbf';
Starting backup at 26-APR-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=37 device type=DISK
channel ORA_DISK_1: starting datafile copy
input datafile file number=00006 name=/u01/app/oracle/oradata/orcldg/users01.dbf
output file name=/home/oracle/users_from_std.dbf tag=TAG20200426T233826
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 26-APR-20
5)在主库注册获得的文件复制(可选)
[oracle@sz ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 23:41:14 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253, not open)
connected to recovery catalog database
RMAN> catalog datafilecopy '/home/oracle/users_from_std.dbf';
cataloged datafile copy
datafile copy file name=/home/oracle/users_from_std.dbf RECID=1 STAMP=1038786100
6)利用已注册的数据文件拷贝还原损坏的数据文件(可选)
RMAN> restore datafile 6;
Starting restore at 26-APR-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=26 device type=DISK
channel ORA_DISK_1: restoring datafile 00006
input datafile copy RECID=1 STAMP=1038786100 file name=/home/oracle/users_from_std.dbf
destination for restore of datafile 00006: /u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: copied datafile copy of datafile 00006
output file name=/u01/app/oracle/oradata/orcl/users01.dbf RECID=0 STAMP=0
Finished restore at 26-APR-20
或者直接利用下面的命令:
RMAN> run{
2> set newname for datafile 6 to '/home/oracle/users_from_std.dbf';
3> switch datafile 6;
4> }
executing command: SET NEWNAME
datafile 6 switched to datafile copy
input datafile copy RECID=1 STAMP=1038786100 file name=/home/oracle/users_from_std.dbf
starting full resync of recovery catalog
full resync complete
7)恢复损坏的数据文件,打开数据库
RMAN> recover datafile 6 ;
Starting recover at 26-APR-20
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 26-APR-20
RMAN> alter database open;
Statement processed
4.3 利用网络进行恢复(12c新特性)
1)删除数据文件
[oracle@sz ~]$ rm users_from_std.dbf
2)连接到主库
[oracle@sz ~]$ rman target / catalog rman/rman@catdb
Recovery Manager: Release 12.1.0.2.0 - Production on Sun Apr 26 23:49:41 2020
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1565938253)
connected to recovery catalog database
3)设置加密算法
RMAN> set encryption algorithm 'AES128';
executing command: SET encryption
4)利用备库恢复主库
RMAN> restore datafile '/home/oracle/users_from_std.dbf'
2> from service 'shhai' section size 120M;
Starting restore at 26-APR-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=22 device type=DISK
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: using network backup set from service shhai
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00006 to /home/oracle/users_from_std.dbf
channel ORA_DISK_1: restoring section 1 of 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 26-APR-20
RMAN> alter database open;
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of sql statement command at 04/26/2020 23:53:33
ORA-01113: file 6 needs media recovery
ORA-01110: data file 6: '/home/oracle/users_from_std.dbf'
RMAN> recover datafile 6;
Starting recover at 26-APR-20
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 26-APR-20
RMAN> alter database open;
Statement processed
RMAN> select count(1) from scott.emp;
COUNT(1)
----------
14
以上就是DataGuard环境下使用RMAN进行数据库备份和恢复的演示。