一、了解数据库备份与恢复的基础知识

1,备份是数据库中数据的副本,它可以保护数据在出现意外损失时最大限度的恢复

2,Oracle 数据库的备份包括以下两种类型:

2-1,物理备份是对数据库的操作系统物理文件(如数据文件、控制文件和日志文件等)的备份

2-2,逻辑备份是对数据库逻辑组件(如表、视图和存储过程等数据库对象)的备份

二、理解在 Oracle 环境中可能发生的故障类型

1,导致数据库操作中止的故障包括四种类型:

1-1,语句故障:在执行 SQL 语句无效可导致语句故障。

1-2,用户进程故障:当用户程序出错而无法访问数据库时发生用户进程故障。导致用户进程故障的原因是异常断开连接或异常终止进程。

1-3,实例故障:当 ORACLE 的数据库实例由于硬件或软件问题而无法断续运行时,就会发生实例故障

1-4,介质故障:在数据库无法正确读取或写入某个数据库文件时,会发生介质故障

三、掌握传统的导出和导入程序 exp/imp

1,传统的导出导入程序用于实施数据库的逻辑备份和恢复;导出程序将数据库中的对象定义和数据备份到一个操作系统二进制文件中

2,导入程序读取二进制导出文件并将对象和数据载入数据库中;传统的导入程序是客户端工具

3,导出和导入实用程序的特点有:

3-1,可以按时间保存表结构和数据;

3-2,允许导出指定的表,并重新导入到新的数据库中;

3-3,可以把数据库迁移到另外一台异构服务器上;

3-4,在两个不同版本的 Oracle 数据库之间传输数据;

3-5,在联机状态下进行备份和恢复;

3-6,可以重新组织表的存储结构,减少链接及磁盘碎片

4,可以使用以下三种方法调用导出和导入实用程序:

4-1,交互提示符:以交互的方式提示用户逐个输入参数的值

4-2,命令行参数:在命令行指定执行程序的参数和参数值

4-3,参数文件:允许用户将运行参数和参数值存储在参数文件中,以便重复使用参数

5,导出和导入数据库对象的四种模式是:

5-1,数据库模式:导出和导入整个数据库中的所有对象;

5-2,表空间模式:导出和导入一个或多个指定的表空间中的所有对象;

5-3,用户模式:导出和导入一个用户模式中的所有对象;

5-4,表模式:导出和导入一个或多个指定的表或表分区;

6,示例:命令:exp help=y  会得到所有可输入参数及其含义

 

6-1,导出 scott 的 student 、address 表

exp scott/tiger@orcl tables=(student,address) file=c:\scott1.dmp log=c:\scott1.log;

6-2,导出 scott 的所有对象

exp scott/tiger@orcl owner=scott file=c:\scott2.dmp log=c:\scott2.log;

6-3,只有系统管理用户如 system 及以上权限用户才可以导出其他用户的对象

 7,传统的导入实用程序:命令:imp help=y 可以查看所有参数及含义:如下图

7-1,导入文件:可以将文件中所有的对象全都导入

7-1-1,scott 导入从 scott 用户导出的文件

imp scott/tiger@orcl file=c:\scott1.dmp;  --文件中的内容全部导入

imp scott/tiger@orcl file=c:\scott1.dmp tables=student;  --只是将文件中的 student 表导入

7-1-2,user1 导入从 scott 用户导出的文件

imp scott/tiger@orcl file=c:scott1.dmp fromuser=scott touser=user1; --这样写是不可以的

imp user1/user1@orcl file=c:scott1.dmp fromuser=scott to user=user1;  --这样写可以

imp system/system@orcl file=c:\scott1.dmp fromuser=scott touser=user1; --这样写也可以

如果上面的语句已经执行了一次,再次执行一样的语句是会报错的,比如说表的创建语句,一个用户不可能创建两个相同名的表,但表里面的内容可以依靠 ignore=y 重新插入一次,如果不建反约束的话

imp user1/user1@orcl file=c:\scott1.dmp fromuser=scott touser=user1 ignore=y;

8,依照参数文件,就是将命令写进文件中(如:userid=scott/scott@orcl tables=student file=c:\scott2.dmp),然后执行此文件:exp parfile=文件路径

四、掌握可传输表空间的操作方法

1,如果迁移的数据量很大,可以使用可传输表空间:

1-1,检查要传输的表空间是否是自包含的。

1-2,将表空间设置成只读。

1-3,exp 进行可传输表空间模式的导出。

1-4,将导出文件和数据文件复制到目标数据库上。

1-5,目标数据库上,imp 进行可传输表空间模式的导入。

1-6,目标数据库上,把表空间设置成读写状态。

五、掌握 oracle 11g 的逻辑泵的使用,重要的参数

1,在 expdp 进行导出时,先创建了 MT 表,并把对象的信息插入到 MT 表,之后进行导出动作;导出完成后,MT 表也导出到转储文件中;导出任务完成后、或者删除了导出任务后,MT 表自动删除;

如果导出任务异常终止,MT 表仍然保留

2,expdp 也具有四种模式:表、用户、可传输表空间、全库

3,例:导出 scott 的 student address 表,有 MT 表出现

system 用户建立目录对象:create directory MY_DIR as 'c:\dir1';

将目录对象的使用权交给 scott:grant read,write on directory MY_DIR to scott;

导出 scott 的 student 和 address 表:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp tables=(student,address);

导出 scott 的两表的表结构,不导出数据,并且将上面已经存在的文件覆盖:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp tables=(student,address) content=metadata_only reuse_dumpfiles=y;

导出 scott 的两表的表数据,不导出结构,并且将上面已经存在的文件覆盖:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp tables=(student,address) content=data_only reuse_dumpfiles=y;

导出 scott、hr 用户的内容:expdp system/system@orcl directory=MY_DIR dumpfile=expdp_system1.dmp schemas=(scott,hr) ;

导出 student 表以及上面的约束、索引:exp scott/scott@orcl file=c:\exp11.dmp tables=student; 导入:imp scott/scott@orcl file=c:\exp11.dmp; 传统方式导出表时自动导出表约束及索引

导出 student 表以及上面的约束、索引:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp tables=student; 在不指定的情况下默认导出表的约束及索引

导出 student 表,和其上的约束,但是不导出索引:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp4.dmp tables=student exclude=index;

导出 student 表,和其上的约束,但是不导出索引:exp scott/scott@orcl file=c:\exp11.dmp tables=student indexes=n;

导出 student 表,但是不导出其上的约束及索引:exp scott/scott@orcl file=c:\exp11.dmp tables=student indexes=n constraints=n;

导出 student 表,但是不导出其上的约束及索引:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp4.dmp tables=student exclude=index,constraint;

对于 scott 对象,除了 student address 表,其他表都导出:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp4.dmp exclude=table:"in('STUDENT')" exclude=table:"in( 'ADDRESS')";

导出 student 表及约束和索引,只导出 address 表的结构和数据:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp6.dmp tables=(student,address) exclude=constraint:"in('CON_ZZ)" exclude=index:"in('IND_ADD_SNO')";

4,expdp 的重要参数:

4-1,部分的 exp 中的参数仍然可用,有的不能使用,如 index.

4-2,directory:供转储文件和日志文件使用的目录对象。

4-3,job_name:指定的任务的名称。

4-4,content:指定要导出的数据,其中有效关键字值为:(ALL)、DATA_ONLY 和 METADATA_ONLY,当设置 content 为 ALL 时,将导出对象定义及其所有数据:

DATA_ONLY 时,只导出对象数据;为 METADATA_ONLY 时,只导出对象定义。

4-5,reuse_dumpfiles:如果导出文件已经存在,是否覆盖。

4-6,compression:压缩导出文件。

4-7,estimate:指定估算被导出表所占用磁盘空间的方法,默认值是 BLOCKS。

5,导入:impdp scott/scott@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp;--注意:如果文件中只有表数据,则导入之前要建立表结构

6,利用参数文件导出:

6-1,导出 student 表和约束及索引,只导出 address 表的结构和数据:expdp scott/scott@orcl parfile=exp1.txt;

6-1-1,exp1.txt 的内容:derectory=MY_DIR dumpfile=expdp7.dmp include=table:"in('STUDENT', 'ADDRESS')" include=index:"in('PK_SNO', 'IND_STU_SNAME')" include=constraint:"in('PK_SNO', 'CON_SAGE')" reuse_dumfiles=y;

6-2,导出 student 表的 sno>1 的记录,和 address 的 sno=2 的记录:expdp scott/scott@orcl parfile=exp1.txt;

6-2-1,exp1.txt 的内容:directory=MY_DIR dumpfile=expdp7.dmp reuse_dumpfiles=y tables=student,address query=student:"where sno>1", address:"where sno=2";

7,impdp 中 remap_schema 的使用:

7-1,导出 scott 用户中的所有对象:expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp1.dmp schemas=scott;

7-2,新用户 hr 进行导入,注意提前使用 system 用户对其进行使用目录的赋权:grant read,write on directory MY_DIR to hr;

7-3,hr 用户导入 scott 导出的文件中的表:impdp hr/hr@orcl directory=MY_DIR dumpfile=expdp1.dmp tables=student remap_schema=scott:hr;

7-4,remap_tablespace:将一个表空间的数据转移到另一个表空间

7-4-1,expdp scott/scott@orcl directory=MY_DIR dumpfile=expdp1.dmp schemas=scott;

7-4-2,impdp scott/scott@orcl directory=MY_DIR dumpfile=expdp1.dmp remap_tablespace=users:example;

六、了解 exp/imp 和数据泵的区别

七、掌握针对不同的生产环境而采取的逻辑备份的方法

posted on 2018-12-17 17:58  独自的独行  阅读(130)  评论(0)    收藏  举报