码迷,mamicode.com
首页 > 数据库 > 详细

OracleDBA职责—备份与恢复技术—逻辑备份1

时间:2020-07-17 16:23:28      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:run   操作   directory   edit   res   completed   cat   物理   employee   

逻辑备份

toc

1.逻辑备份的概念:

就如前面备份与恢复概念中所说的一样,逻辑备份只关心数据部分。通常作为物理备份的辅助工具。
逻辑备份,备份级别很宽松适应多种备份。例如,表级别,模式级别(按用户),表空间级别(数据泵可以),数据库级别(整库迁移)。

2.逻辑备份的两个工具:

  • imp/exp:导入/导出,最原始的导入导出工具
  • impdp/expdp:数据泵导入导出,这个工具是在Oracle10g后诞生的,用于大数据量的导入导出
    上述两种工具区别在于,前者是客户端工具,可以在客户端工作也可以在服务端。后者是服务端工具,只能在Oracle的服务端使用,不能在客户端使用。

3.关于数据泵:

数据泵是一种高效的数据和元数据的迁移工具。相比RMAN,RMAN可以还原到时间点并且根据日志可以更新数据到最新的状态,而数据泵只能恢复到备份的时间点。

3.1数据泵的使用-导出:

??DATADUMP EXPORT:用来将数据库中的数据或者元数据导出到操作系统的文件中,这些导出的操作系统文件叫做导出文件集。这些导出文件可以使用数据泵导入工具导入回源数据库,也可以是其他数据库。

数据泵的导出模式:
全库导出模式,模式导出,表级别导出,表空间级别,传输表空间模式。


目录对象
目录对象就是一个服务器文件系统的目录的别名,用于外部二进制文件和外部表。
创建目录对象:

#需要现在操作系统层面创建目录:
[oracle@server1 ~]$ mkdir pumpdir
[oracle@server1 ~]$ cd pumpdir/
[oracle@server1 pumpdir]$ pwd
/home/oracle/pumpdir

#在数据库中创建:
SYS@proe>create directory pump_dir as ‘/home/oracle/pumpdir‘;
Directory created.

#进行授权,授予HR使用目录对象的权限:
SYS@proe>grant read,write on directory pump_dir to hr;
Grant succeeded.

删除目录对象:

SYS@proe>drop directory PUMP_DIR;    
Directory dropped.
#在操作系统层面也要将这个目录删除

查询当前系统下目录对象的数据字典:

SYS@proe>select DIRECTORY_NAME,DIRECTORY_PATH from dba_directories;

DIRECTORY_ DIRECTORY_PATH
---------- --------------------------------------------------
SUBDIR     /u01/app/oracle/product/11.2.0/db_home1/demo/schem
           a/order_entry//2002/Sep
...

模拟实验:

  • 表级别导出:
[oracle@server1 pumpdir]$ expdp hr/hr directory=pump_dir dumpfile=hr_employees.dmp tables=employees

Export: Release 11.2.0.4.0 - Production on Thu Jul 16 14:16:16 2020

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

UDE-28002: operation generated ORACLE error 28002
ORA-28002: the password will expire within 6 days

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
Starting "HR"."SYS_EXPORT_TABLE_01":  hr/******** directory=pump_dir dumpfile=hr_employees.dmp tables=employees 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/TRIGGER
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows
Master table "HR"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_TABLE_01 is:
  /home/oracle/pumpdir/hr_employees.dmp
Job "HR"."SYS_EXPORT_TABLE_01" successfully completed at Thu Jul 16 14:16:22 2020 elapsed 0 00:00:05

支持导出一个库中的多个表,用逗号分隔
[oracle@server1 pumpdir]$ expdp hr/hr directory=pump_dir dumpfile=hr_employees.dmp tables=employees,departments

  • 模式级别导出:
#使用导出模式的权限进行模式级别导出:
[oracle@server1 pumpdir]$ expdp hr/hr directory=pump_dir dumpfile=sch_hr.dmp;
。。。
Master table "HR"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:
  /home/oracle/pumpdir/sch_hr.dmp
Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at Thu Jul 16 14:21:38 2020 elapsed 0 00:00:25

#使用sys用户或system用户导出需要指定模式:
[oracle@server1 pumpdir]$ expdp \‘sys\/123456 as sysdba\‘ directory=pump_dir dumpfile=sys_hr.dmp schemas=hr
。。。
Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:
  /home/oracle/pumpdir/sys_hr.dmp
Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at Thu Jul 16 14:25:03 2020 elapsed 0 00:00:14
  • 表空间级别导出:
[oracle@server1 proe]$ expdp \‘sys\/123456 as sysdba\‘ directory=pump_dir dumpfile=tbs_test_1.dmp tablespaces=users
  • 整库导出:(只导出用户表而不会导出系统表)
[oracle@server1 proe]$ expdp \‘sys\/123456 as sysdba\‘ directory=pump_dir dumpfile=full.dmp full=y
3.2数据泵的使用-导入:

??DATADUMP IMPORT:数据泵导入工具,与导出工具相对应,但是导入时会有一些额外条件:首先导入表的时候,在目标库应该不存在同名的对象。如果导入的仅仅只是数据那么就需要忽略创建表的过程加上额外的参数。
模拟实验:

  • 模式级别导入:

借助11g中自带的一些样板来完成这个实验,使用scott.emp表。

#首先在emp表中插入一些数据。让这个表发生变化
SYS@proe>insert into scott.emp(empno) values (18);

1 row created.

SYS@proe>select count(*) from scott.emp;

  COUNT(*)
----------
        16

#将scott模式导出。注意对用户的授权。
[oracle@server1 ~]$ expdp scott/1234 directory=pump_dir dumpfile=scott.dmp
。。。
*****************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
  /home/oracle/pumpdir/scott.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at Thu Jul 16 16:27:54 2020 elapsed 0 00:00:12

#在目标库内创建目录对象
[oracle@11gtest ~]$ mkdir pumpdir
SYS@proe>create directory pump_dir as ‘/home/oracle/pumpdir‘;
[oracle@11gtest ~]$ mv scott.dmp pumpdir/

#在目标库创建一个新的模式
SYS@proe>create user new_scott identified by oracle
  2  default tablespace users
  3  temporary tablespace temp
  4  quota unlimited on users;
User created.

#授权
SYS@proe>grant connect,resource to new_scott;
Grant succeeded.

#开始导入
[oracle@11gtest ~]$ impdp \‘sys\/123456 as sysdba \‘ directory=pump_dir dumpfile=scott.dmp remap_schema=scott:new_scott
注意:remap_schema参数可以将数据从一个schema导入到另一个schema中。进行重新映射。
#登入查看:
NEW_SCOTT@proe>select count(*) from emp;

  COUNT(*)
----------
        16

模式导入成功。
模式删除:

SYS@proe>drop user new_scott cascade;
User dropped.

关于参数remap_schema,如果将数据导入到不同于原来的schema中,这个参数的使用方法是remap_schema=source_schema:target_schema,即使指定的schema不存在只要在导入时的权限是足够的,那么就可以利用dump中metadata来自行创建。(没有尝试过)


  • 表空间导入:
    进行导入前相应的表空间需要被先创建或者表空间已经存在。同时在导入过程中也可能会出现表已经存在等问题。
    模拟实验:
    1) 在原库上创建表空间:
SYS@proe>create tablespace tbs1 datafile ‘/u01/app/oracle/oradata/proe/tbs1.dbf‘
   2 size 50m;
Tablespace created.

SYS@proe>select tablespace_name,file_name from dba_data_files where tablespace_name=‘TBS1‘;

TABLESPACE_NAME        FILE_NAME
----------------    -----------------------------------------------------------
TBS1                 /u01/app/oracle/oradata/proe/tbs1.dbf

2) 在这个表空间上创建几个样例表。

SYS@proe>create table scott.tbst1 tablespace tbs1 as select * from scott.emp;

Table created.

SYS@proe>create table hr.tbst2 tablespace tbs1 as select * from scott.emp;

Table created.

SYS@proe>select owner,table_name,tablespace_name from dba_tables where tablespace_name=‘TBS1‘;

OWNER       TABLE_NAME     TABLESPACE_NAME
--------- --------------   --------------
SCOTT          TBST1            TBS1
HR             TBST2            TBS1

3)按照表空间级别导出所有数据

[oracle@server1 admin]$ expdp \‘sys\/123456 as sysdba\‘ directory=pump_dir dumpfile=tbs1.dmp tablespaces=tbs1
Export: Release 11.2.0.4.0 - Production on Thu Jul 16 19:31:00 2020

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
Starting "SYS"."SYS_EXPORT_TABLESPACE_01":  "sys/******** AS SYSDBA" directory=pump_dir dumpfile=tbs1.dmp tablespaces=tbs1 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
. . exported "HR"."TBST2"                                8.578 KB      16 rows
. . exported "SCOTT"."TBST1"                             8.585 KB      16 rows
Master table "SYS"."SYS_EXPORT_TABLESPACE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_TABLESPACE_01 is:
  /home/oracle/pumpdir/tbs1.dmp
Job "SYS"."SYS_EXPORT_TABLESPACE_01" successfully completed at Thu Jul 16 19:31:03 2020 elapsed 0 00:00:03

4) 直接导入未在目标库创建相应表空间(错误)

[oracle@11gtest ~]$ impdp \‘sys\/123456 as sysdba \‘ directory=pump_dir dumpfile=tbs1.dmp tablespaces=tbs1 table_exists_action=truncate

技术图片

5)在目标库创建相应的表空间(克隆的SID一样)

SYS@proe>create tablespace tbs1 datafile ‘/u01/app/oracle/oradata/proe/tbs1.dbf‘
 size 50m  2  ;

Tablespace created.

6)重新进行导入

[oracle@11gtest ~]$ impdp \‘sys\/123456 as sysdba \‘ directory=pump_dir dumpfile=tbs1.dmp tablespaces=tbs1 table_exists_action=truncate

Import: Release 11.2.0.4.0 - Production on Fri Jul 17 14:42:33 2020

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
Master table "SYS"."SYS_IMPORT_TABLESPACE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLESPACE_01":  "sys/******** AS SYSDBA" directory=pump_dir dumpfile=tbs1.dmp tablespaces=tbs1 table_exists_action=truncate 
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "HR"."TBST2"                                8.578 KB      16 rows
. . imported "SCOTT"."TBST1"                             8.585 KB      16 rows
Job "SYS"."SYS_IMPORT_TABLESPACE_01" successfully completed at Fri Jul 17 14:42:36 2020 elapsed 0 00:00:03

7)登入验证数据

[oracle@11gtest ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jul 17 14:43:26 2020

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

SYS@proe>select owner,table_name,tablespace_name from dba_tables where tablespace_name=‘TBS1‘;

OWNER       TABLE_NAME     TABLESPACE_NAME
--------- --------------   --------------
SCOTT          TBST1            TBS1
HR             TBST2            TBS1

至此表空间导入成功。





OracleDBA职责—备份与恢复技术—逻辑备份1

标签:run   操作   directory   edit   res   completed   cat   物理   employee   

原文地址:https://www.cnblogs.com/plutozzl/p/13330047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!