oracle数据库备份测试

文章目录

  • oracle数据库备份测试
    • 基本语法
      • 1.EXP:导出
      • 2.IMP: 导入
    • 测试范例
      • 准备测试数据
      • 导出脚本.bat
      • 导入脚本.bat
      • ddl_backup.bat
      • ddl_backup.sql

oracle数据库备份测试

基本语法

1.EXP:导出

有三种主要的方式(完全、用户、表)

1、完全:

EXP SYSTEM/MANAGER BUFFER=64000 FILE=C:\FULL.DMP FULL=Y

如果要执行完全导出,必须具有特殊的权限

2、用户模式:

-- 用户SONIC所有对象导出
EXP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP OWNER=SONIC

-- 用户SONIC,SCOTT所有对象导出
EXP SYSTEM/MANAGER BUFFER=64000 FILE=C:\SONIC.DMP OWNER=(SONIC,SCOTT)

这样用户的所有对象被输出到文件中

3、表模式:

-- 导出SONIC用户的SONIC表
EXP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP TABLES=(SONIC)

-- 导出SONIC用户的SONIC表file1以'00'打头的数据
EXP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP OWNER=SONIC TABLES=(SONIC,SONIC2) query=\"where filed1 like '00%'\"

-- 导出SONIC用户的SONIC,SONIC2表
EXP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP  TABLES=(SONIC,SONIC2)

这样用户SONIC的表就被导出,注意表模式不能加OWNER

2.IMP: 导入

具有三种模式(完全、用户、表)

所有导入皆可加【ignore=y】设置,表示如果有存在的表,则新增到末尾,建议最好不加,一般导入都是先将表彻底删除,然后再导入

1、完全:

IMP SYSTEM/MANAGER BUFFER=64000 FILE=C:\FULL.DMP FULL=Y

将导出数据完全导入,一般用这种即可,若想定制数据,可在导出处定制数据

2、用户模式:

IMP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP FROMUSER=SONIC TOUSER=SONIC

这样用户SONIC的所有对象被导入到文件中。必须指定FROMUSER、TOUSER参数,这样才能导入数据,FROMUSER和TOUSER分别是源用户和目标用户

3、表模式:

IMP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP TABLES=(SONIC)

这样用户SONIC的表SONIC就被导入。

  • 注意1:导入的用户必须和导出的用户一致
  • 注意2:如果加上【FROMUSER】和【TOUSER】,则【FROMUSER】需要DBA权限
  • 注意3:不止为何,表模式中,如果源数据是全导出的,则再怎么设置【tables】好像都不生效,效果跟用户模式一样会把所有的数据都导入进来,未找到原因

测试范例

准备测试数据

drop table test_jerry_1211_1;
drop table test_jerry_1211_2;
drop table test_jerry_1211_3;
drop table test_jerry_1211_4;
drop table test_jerry_1211_5;


select * from  test_jerry_1211_1;
select * from  test_jerry_1211_2;
select * from  test_jerry_1211_3;
select * from  test_jerry_1211_4;
select * from  test_jerry_1211_5;
select * from v_test1;
select * from v_test2;

--1
create table test_jerry_1211_1
as 
select 'hijerry_1' t_c from dual;
--2
create table test_jerry_1211_2
as 
select 'hijerry_2' t_c from dual;
--3
create table test_jerry_1211_3
as 
select 'hijerry_3' t_c from dual;
--4
create table test_jerry_1211_4
as 
select 'hijerry_4' t_c from dual;
--5
create table test_jerry_1211_5
as 
select 'hijerry_5' t_c from dual;

create or replace view v_test1 as
select '1' as tc from dual;

create or replace view v_test2 as 
select * from v_test1;

导出脚本.bat

exp system/[yourpwd]@orcl buffer=64000 file=E:\backups\dmp\%Date:~0,4%-%Date:~5,2%-%Date:~8,2%.dmp owner=scott

导入脚本.bat

imp system/[yourpwd]@orcl buffer=64000 file=E:\backups\dmp\2018-12-11.dmp full=y

ddl_backup.bat

sqlplus scott/[yourpwd] @E:\backups\ddl_backup.sql

ddl_backup.sql

set term off pagesize 1000 heading on verify off  
set markup html on entmap ON preformat off spool on  
set linesize 2000  
set pagesize 30000  
set long 999999999  
set longchunksize 999999  
column today new_value today_file
select  to_char(sysdate,'yyyymmdd') as today from dual;
spool d:\file&today_file..html  
spool E:\backups\ddl\PROCEDURE&today_file..html  
select object_name, DBMS_METADATA.GET_DDL('PROCEDURE', object_name) ddl from user_objects  where object_type = 'PROCEDURE';
spool E:\backups\ddl\VIEW&today_file..html  
select object_name,DBMS_METADATA.GET_DDL('VIEW',object_name) ddl from user_objects where object_type = 'VIEW';
spool E:\backups\ddl\MVIEW&today_file..html  
select object_name,DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW',object_name) ddl  from user_objects  where object_type = 'MATERIALIZED VIEW' ;
spool E:\backups\ddl\FUNCTION&today_file..html  
select object_name, DBMS_METADATA.GET_DDL('FUNCTION',object_name) ddl from user_objects  where object_type = 'FUNCTION';
spool off  
quit

你可能感兴趣的:(oracle)