3. Oracle数据库逻辑备份与恢复

一. Oracle逻辑备份介绍

  Oracle逻辑备份的核心就是复制数据;Oracle提供的逻辑备份与恢复的命令有exp/imp,expdp/impdp。固然像表级复制(create table table_back as select * from table)也算是一种逻辑备份。Oracle逻辑备份没有支持增量备份;对数据恢复也是非一致性的。因此通常都是用于数据迁移的工做。sql

  建立演示对象数据库

create tablespace lottu datafile '/data/oracle/data/lottu01.dbf' size 2G autoextend on; create user lottu identified by li0924  default tablespace lottu; grant connect, resource to lottu ; grant select any dictionary to lottu ; create user rax identified by rax123 default tablespace lottu; grant connect, resource to rax ; grant select any dictionary to rax ; conn lottu/li0924 create table tbl_lottu as select level as id, 'lottu' as name from dual connect by level <= 10000; create table tbl_lottu_01 as select level as id, 'lottu'||level as name from dual connect by level <= 100; conn rax/rax123 create table tbl_rax as select level as id, 'rax' as name from dual connect by level <= 10000;

二. Oracle导出/导入命令exp/imp

  exp/imp命令是最原始的一种数据保护工具;效率方面确实很差;支持客户端执行操做。在这简单演示下如何操做。服务器

  备份的对象列表:oracle

. exporting tablespace definitions . exporting profiles . exporting user definitions . exporting roles . exporting resource costs . exporting rollback segment definitions . exporting database links . exporting sequence numbers . exporting directory aliases . exporting context namespaces . exporting foreign function library names . exporting PUBLIC type synonyms . exporting private type synonyms . exporting object type definitions . exporting system procedural objects and actions . exporting pre-schema procedural objects and actions . exporting cluster definitions

2.1 导出命令exp

  • 导出表中某些记录
exp lottu/li0924 GRANTS=Y TABLES=tbl_lottu  QUERY="'where id < 100'" FILE=/home/oracle/exp/lottu01.dmp LOG=/home/oracle/exp/log/lottu01.log
  • 导出某个Schema下某些表
exp lottu/li0924 GRANTS=Y TABLES="(tbl_lottu,tbl_lottu_01)" FILE=/home/oracle/exp/lottu02.dmp LOG=/home/oracle/exp/log/lottu02.log
  • 导出哪些Schema下的对象
exp system/Oracle235 OWNER="(lottu,rax)"  FILE=/home/oracle/exp/system03.dmp LOG=/home/oracle/exp/log/system03.log
  • 导出全库
exp system/Oracle235 FULL=Y FILE=/home/oracle/exp/system04.dmp LOG=/home/oracle/exp/log/system04.log

2.2 导入命令imp

  imp至关于exp的反向操做;操做以前;须要确认需导入的对象在数据库上面是不存在的;如果在本地作恢复;须要将恢复的对象先drop掉;在执行imp命令操做。app

  • 导入某个Schema下某些表;对exp上面的示例2
[oracle@oracle235 ~]$ sqlplus lottu/li0924 SQL*Plus: Release 11.2.0.4.0 Production on Sat Aug 4 04:42:18 2018 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> drop table tbl_lottu; Table dropped. SQL> drop table tbl_lottu_01; Table dropped. SQL> exit Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options [oracle@oracle235 ~]$ imp lottu/li0924 GRANTS=Y TABLES="(tbl_lottu,tbl_lottu_01)" FILE=/home/oracle/exp/lottu02.dmp LOG=/home/oracle/imp/lottu02.log Import: Release 11.2.0.4.0 - Production on Sat Aug 4 04:43:25 2018 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options Export file created by EXPORT:V11.02.00 via conventional path import done in UTF8 character set and UTF8 NCHAR character set export server uses AL16UTF16 NCHAR character set (possible ncharset conversion) . importing LOTTU's objects into LOTTU
. importing LOTTU's objects into LOTTU
. . importing table                    "TBL_LOTTU"      10000 rows imported . . importing table "TBL_LOTTU_01"        100 rows imported Import terminated successfully without warnings.

三. Oracle导出/导入命令expdp/impdp

  对expdp/impdp是在Oracle10G以后才出现;其实自己使用并非须要很高的技术含量。相比exp/imp;在功能和效率方面有巨大的提高。ide

  • 支持并行
  • 支持任务的暂停和重启动
  • 支持对象的过滤

  备份/恢复效率方面那是大大的提高。因此10G以后能够弃用exp/imp。工具

  empdp和impdp是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用;使用以前须要建立目录;以下演示spa

  • 在服务器上建立目录
mkdir -p /data/ora_dir_lottu
  • 建立逻辑目录,给操做的用户赋予在指定目录的操做权限
create directory dp_lottu as '/data/ora_dir_lottu'; grant read,write on directory dp_lottu to lottu;

3.1 导出命令expdp

  • 按表模式导出
expdp lottu/li0924  tables=tbl_lottu,tbl_lottu_01 dumpfile=expdp_lottu01.dmp logfile=expdp_lottu01.log directory=dp_lottu
  • 按表空间导出
expdp "'/ as sysdba'" tablespaces=tp_lottu dumpfile=expdp_lottu02.dmp logfile=expdp_lottu02.log directory=dp_lottu job_name=lottu02
  • 导出方案
expdp "'/ as sysdba'" SCHEMAS=lottu,rax dumpfile=expdp_lottu05.dmp logfile=expdp_lottu05.log directory=dp_lottu
  • 导出整个数据库
expdp "'/ as sysdba'" dumpfile=expdp_full.dmp full=y logfile=expdp_lottu05.log directory=dp_lottu

3.2 导入命令impdp

  impdp至关于导入命令expdp的反向操做;使用方法跟expdp相同。至关于把上面expdp替换便可。code

  impdp导入的方案,表或者表空间与dump文件不一致;能够用下列参数替换server

REMAP_SCHEMA Objects from one schema are loaded into another schema. REMAP_TABLE Table names are remapped to another table. For example, REMAP_TABLE=HR.EMPLOYEES:EMPS. REMAP_TABLESPACE Tablespace objects are remapped to another tablespace.

  如示例所示:#将lottu用户下的数据所有导入到表空间tp_rax

impdp "'/ as sysdba'" directory=dp_lottu dumpfile=expdp_lottu04.dmp remap_tablespace=tp_lottu:tp_rax

四. 复制表

  Oracle中复制表方式:

create table tablename_back as select * from tablename;
相关文章
相关标签/搜索