windows, 数据库

Oracle数据库的备份与恢复

特性数据泵RMAN热备份
备份类型逻辑备份物理备份
一致性需特殊参数保证自动保证一致性
恢复粒度表/Schema级块/文件/全库级
速度较慢(需转换数据格式)极快(直接复制文件)
系统影响高(消耗CPU/内存)低(优化I/O)
最佳用途选择性恢复、跨版本迁移灾难恢复、快速还原
真正热备份的对比(RMAN)

一、使用rman对数据库进行热备份

1.启用归档日志模式。

打开SQL Plus 执行:
Enter user-name: sys/ as sysdba
Enter password:
SQL> shutdown immediate;
SQL> startup mount;
SQL> archive log list; 查看是否启用了归档日志
SQL> alter database archivelog; 启用归档日志
SQL> alter database open; 打开数据库

2.创建恢复目录数据库并注册目标数据库到恢复目录

1.在恢复目录数据库中 创建表空间
SQL> create tablespace rmants datafile 'c:\backup\rmants.dbf' size 500m;
            大小为500M,名称为rmants的表空间
            注意:backup目录需事先手工创建,rmants.dbf会自动创建

2.创建用户,用户名rman,密码rman
SQL> create user rman identified by rman default tablespace rmants;

            授权:
SQL> grant connect,resource,recovery_catalog_owner to rman;

            退出:
SQL>exit;

3.rman连接到恢复目录数据库
C:\Users\ASUS>rman catalog rman/
Recovery Manager: Release 11.2.0.1.0 - Production on Tue Apr 29 18:58:06 2025
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
recovery catalog database Password:
connected to recovery catalog database
         建立恢复目录:
RMAN>  create catalog tablespace rmants;

recovery catalog created
            退出:
RMAN>exit;

4.连接到目标数据库(即要备份的数据库)
C:\Users\ASUS>rman target rman/
Recovery Manager: Release 11.2.0.1.0 - Production on Tue Apr 29 12:09:26 2025
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
target database Password:
connected to target database: ORCL (DBID=1726536795)
RMAN> connect catalog rman/ ;
connected to recovery catalog database
            上述命令表示连接到恢复目录数据库

RMAN> register database;

database registered in recovery catalog
starting full resync of recovery catalog
full resync complete

            上述命令表示注册数据库

3、备份数据库

1.配置自动通道
   指定设备的类型是磁盘(disk),通道的个数是5个,默认1个,通道多效率会更高。
RMAN> configure device type disk parallelism 5;

2.开始备份
RMAN> backup database format 'c:\backup\%U';
       // format指定了文件名,文件名是动态的,会自动生成,如果没有指定文件名,默认备份到快闪恢复区(C:\app\Administrator\flash_recovery_area)

RMAN>backup database plus archivelog delete input;
    //如果还要包含归档日志文件,则要加上plus archivelog关键字,delete input的意思是在备份完成后,删除archivelog文件

4、数据库恢复

1.关闭数据库并删除文件
C:\Users\Administrator>sqlplus sys/  as sysdba
SQL> shutdown  immediate;
        把C:\app\Administrator\oradata\orcl\USERS01.DBF删除到回收站

2.启动数据库并恢复文件 
C:\Users\Administrator>sqlplus sys/  as sysdba
SQL> startup mount;  -- 数据库要在mount状态下执行恢复
SQL>exit;

C:\Users\Administrator>rman target rman/
RMAN> restore database;

Starting restore at 30-APR-25
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=189 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=221 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=3 device type=DISK
allocated channel: ORA_DISK_4
channel ORA_DISK_4: SID=33 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 00001 to C:\APP\ASUS\ORADATA\ORCL\SYSTEM01.DBF
channel ORA_DISK_1: restoring datafile 00002 to C:\APP\ASUS\ORADATA\ORCL\SYSAUX01.DBF
channel ORA_DISK_1: restoring datafile 00003 to C:\APP\ASUS\ORADATA\ORCL\UNDOTBS01.DBF
channel ORA_DISK_1: restoring datafile 00004 to C:\APP\ASUS\ORADATA\ORCL\USERS01.DBF
channel ORA_DISK_1: restoring datafile 00005 to C:\APP\ASUS\ORADATA\ORCL\EXAMPLE01.DBF
channel ORA_DISK_1: restoring datafile 00006 to C:\BACKUP\RMANTS.DBF
channel ORA_DISK_1: reading from backup piece C:\BACKUP\053O4TUH_1_1
channel ORA_DISK_1: piece handle=C:\BACKUP\053O4TUH_1_1 tag=TAG20250429T190449
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 30-APR-25

RMAN> recover database;

Starting recover at 30-APR-25
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4

starting media recovery
media recovery complete, elapsed time: 00:00:01

Finished recover at 30-APR-25

RMAN> alter database open;

database opened     \\    如果数据库启动成功表示恢复成功。

二、使用数据泵对数据库进行逻辑备份还原

数据泵是逻辑备份工具:导出的是数据库对象的结构和数据(SQL可读格式)。支持的对象类型:完整数据库、Schema、表、表空间;视图、索引、约束、触发器;存储过程、函数、包;权限、角色、上下文等元数据。

1.创建一个操作目录并备份数据库表:

c:\users\administrator>sqlplus sys/ as sysdba
SQL> create directory dump_dir as 'c:\opt\backup';  
   \\首先创建一个磁盘目录c:\opt\backup 创建一个名为dump_dir的对象(名字是随便起的),类型是directory, 该对象和c:\opt\backup关联起来。

c:\users\administrator>expdp scott/  directory=dump_dir dumpfile=scotttab.dmp 
tables=dept,emp
    \\以scott的账号连接数据库,备份emp表和dept表,生成备份文件scotttab.dmp,放到c:\opt\backup

2、使用数据泵恢复数据库表

C:\Users\Administrator>impdp scott/   directory=dump_dir dumpfile=scotttab.dmp 
tables=emp  TABLE_EXISTS_ACTION=replace
    \\以scott的账号连接数据库,恢复emp,备份文件名为scotttab.dmp,放在c:\opt\backup目录中

3、使用数据泵备份完整数据库

-- 导出数据库
   C:\users\administrator>expdp system/  directory=dump_dir 
dumpfile=full.dmp full=y
-- 导入数据库
   C:\users\administrator>impdp system/  directory=dump_dir 
dumpfile=full.dmp full=y TABLE_EXISTS_ACTION=replace

Leave a Reply