3. Oracle數據庫邏輯備份與恢復


一. Oracle邏輯備份介紹

  Oracle邏輯備份的核心就是復制數據;Oracle提供的邏輯備份與恢復的命令有exp/imp,expdp/impdp。當然像表級復制(create table table_back as select * from table)也算是一種邏輯備份。Oracle邏輯備份沒有支持增量備份;對數據恢復也是非一致性的。所以一般都是用於數據遷移的工作。

  創建演示對象

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命令是最原始的一種數據保護工具;效率方面確實不好;支持客戶端執行操作。在這簡單演示下如何操作。

  備份的對象列表:

. 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命令操作。

  • 導入某個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;在功能和效率方面有巨大的提升。

  • 支持並行
  • 支持任務的暫停和重啟動
  • 支持對象的過濾

  備份/恢復效率方面那是大大的提升。所以10G之后可以棄用exp/imp。

  empdp和impdp是服務端的工具程序,他們只能在ORACLE服務端使用,不能在客戶端使用;使用之前需要創建目錄;如下演示

  • 在服務器上創建目錄
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替換即可。

  impdp導入的方案,表或者表空間與dump文件不一致;可以用下列參數替換

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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM