Oracle逻辑备份

备份方式
1.物理备份:块拷贝方式,rman
2.逻辑备份:exp|imp expdp|impdp

exp优势:
1.方便,开发人员经常使用
2.imp导入非常方便
3.数据迁移,alert日志

imp速度比impdp慢

exp导出4种模式:
完全、表空间、用户、表

exp导出文件的位置:
服务器,本地?
expdp存储在服务器上面
nfs挂载也可以存储在本地

测试案例:

-- 创建测试数据
SQL> create user fx identified by fx;



SQL> grant connect,resource to fx;
SQL> conn fx/fx
SQL> create table ftest(id int);
SQL> insert into ftest values(1);
SQL> conn / as sysdba
SQL> alter user fx quota unlimited on users;
SQL> conn fx/fx
SQL> truncate table ftest;
SQL> insert into ftest select level from dual connect by level <= 50;
SQL> commit;
-- 导出某个用户:(feedback=10[每10条显示进度] compress=y
[压缩])
C:\Windows\system32>exp fx/fx@127.0.0.1/orcl 
    file=C:/Users/LKZ/Desktop/11/fx.dmp 
    log=C:/Users/LKZ/Desktop/11/fx.log
C:\Windows\system32>exp fx/fx@127.0.0.1/orcl 
    file=C:\Users\LKZ\Desktop\11\fx.dmp 
    log=C:\Users\LKZ\Desktop\11\fx.log feedback=10 compress=y
  
-- 使用A用户导出B用户的数据(owner=fx )
C:\Windows\system32>exp system/654321@127.0.0.1/orcl 
    file=C:\Users\LKZ\Desktop\11\fx.dmp 
    log=C:\Users\LKZ\Desktop\11\fx.log owner=fx  
-- 导出结构不包含数据(rows=n )
C:\Windows\system32>exp system/654321@127.0.0.1/orcl 
    file=C:\Users\LKZ\Desktop\11\fx.dmp 
    log=C:\Users\LKZ\Desktop\11\fx.log owner=fx rows=n 
-- 导出某个用户下几个表?(tables=ftest)
    C:\Windows\system32>exp fx/fx@127.0.0.1/orcl 
    file=C:\Users\LKZ\Desktop\11\fx.dmp 
    log=C:\Users\LKZ\Desktop\11\fx.log tables=ftest
-- tables=%所有表 tables=f%导出f开头的表 tables=ftest,ftest2 
C:\Windows\system32>exp fx/fx@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\fx.log tables=f%
-- 除了一个表或者几个表,其他全部导出
-- 段延迟特性
    C:\Windows\system32>sqlplus / as sysdba

    SQL> show parameter def

    NAME                                 TYPE							VALUE
    ------------------------------
------------------------------------ ----------------------
    deferred_segment_creation            boolean						TRUE
    SQL> create table ftest3(id int);
    -- 创建新表,不插入数据,Oracle不会分配segment段
    -- 表空间->段->区->块
    select * from dba_segments t where t.segment_name='FTEST3';
    -- 只有给FTEST3插入数据,oracle才会给它分配段
    -- 关闭段延迟,就可以导出无数据的表
    SQL> alter system set deferred_segment_creation=false;
    C:\Users\LKZ>exp fx/fx@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\fx.log tables=%
    -- 也可以手动给表分配一个区
    SQL> alter table ftest3 allocate extent;
-- 导库
C:\Users\LKZ>exp \"/ as sysdba\" file=C:\Users\LKZ\Desktop\11\fill.dmp full=y
C:\Users\LKZ>exp system/654321@127.0.0.1/orcl file=d:/back/all.dmp log=d:/back/alldata.log full=y  
-- 导出指定sql数据query=\"where id < 10\"
C:\Users\LKZ>exp fx/fx@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\fx.log tables=ftest query=\"where id < 10\"  

exp导出consistent参数的重要性

导出一致性
tab1---tab2
insert---insert
触发器
exp fx/fx file=fx.dmp log=fx.log consitent=y
先导tab1 1000  scn
再tab2   1500  scn(在导出tab2时tab2被插入)
理论上导出时是一致的
-- consitent=y,原理是在导出tab1时系统会记录一个scn号,当导tab2时会根据这个scan号导对应的数据
create table tab1(id int, name varchar2(20));
create table tab2(id int, name varchar2(20));
-- 创建一个触发器tab1执行insert时自动向tab2插入数据
create or replace trigger fx.TG_TEST
  before insert on fx.tab1
  referencing new as new old as old
  for each row
declare

begin
  insert into tab2 (id, name) values (:New.id, :New.name);
end TG_TEST;
---
> begin
for i in 1 .. 50000 loop
insert into tab1 values(100, 'fx');
commit;
end loop;
end;
/
-- consistent=y 的重要性,此参数会占用undo,数据太大,会造成undo空间不足问题,
    生成环境根据实际情况指定这个参数
    fx 非常多的表 scn
C:\Users\LKZ>exp fx/fx@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\fx.log consistent=y

3.imp导入案例及参数详解

#ignore=y 如果没有表创建表并导入数据,已存在的不重复创建直接导入数据
C:\Users\LKZ>imp kz/kz@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\kz.log ignore=y
# rows=n只导入结构
C:\Users\LKZ>imp kz/kz@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\ll.log rows=n
# data_only=y 只导入数据
C:\Users\LKZ>imp kz/kz@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\cc.log data_only=y
-- 查看指定用户表空间
select * from dba_users where username = 'KZ'
-- 查看表空间位置
select * from v$dbfile
# 重建fx用户指定表空间user2
C:\Users\LKZ>sqlplus / as sysdba
SQL> drop user fx cascade;
SQL> create tablespace user2 datafile 'F:\APP\LKZ\ORADATA\ORCL\T01.dbf' size 50m;
SQL> create user fx identified by fx default tablespace user2;
SQL> grant connect,resource to fx;
# fromuser=fx touser=fx2 如果导出导入不是同一个用户
C:\Users\LKZ>imp fx2/fx2 file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11\fx.log fromuser=fx touser=fx2
# 数据量大的时候设置缓冲(buffer=1048576) 1M
C:\Users\LKZ>imp fx2/fx2@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11/fx.log fromuser=fx touser=fx2 buffer=1048576 data_only=y
# feedback=1每导入多少条显示进度
C:\Users\LKZ>imp fx2/fx2@127.0.0.1/orcl file=C:\Users\LKZ\Desktop\11\fx.dmp log=C:\Users\LKZ\Desktop\11/fx.log fromuser=fx touser=fx2 buffer=1048576 data_only=y feedback=1
# sys导入,全库导入,sys,system(数据字典)
full=y 导入不推荐使用
full=y 全库导出可以使用

4.imp导入版本问题解决

1.10g导出的dmp文件可以用11g导入
2.11g导出的dmp文件不能10导入
解决办法:
1.网络中转
2.软件修改版本
使用UltraEdit打开dmp文件直接修改对应版本即可(但是数据泵就不行了)

5.expdp导出、impdp导入

参数:
-- content(ALL全部)(DATA_ONLY仅数据)(METADATA_ONLY仅结构)

content {ALL | DATA_ONLY | METADATA_ONLY}

directory

dumpfile

logfile

full=y|n

schemas

tables

version

flashback_scn 保证数据导出的一致性
参数:

deferred_segment_creation

true false
-- 在服务器下创建目录,建立目录并授权:
create directory mypump as '/u01/temp';
-- 给begin用户授权
grant read, write on directory mypump to begin;
-- 导出某用户
 expdp begin/begin directory=mydump dumpfile=begin.dmp
 expdp system/Oracle123 directory=mydump dumpfile=begin.dmp 

schemas=begin
--用 SYS 导出全库:

expdp \"/ as sysdba\" directory=mydump dumpfile=full.dmp full=y
-- 用普通用户导出全库:
grant datapump_exp_full_database to begin;

expdp begin/begin directory=mydump dumpfile=full.dmp full=y
-- 导出表:
expdp begin/begin directory=mydump dumpfile=begin.dmp tables= test, 

test3

expdp system/Oracle123 directory=mydump dumpfile=begin.dmp 

tables=begin.test%
-- 排出特定对象

expdp begin/begin directory=mydump dumpfile=begin.dmp 

exclude=table:"in ('TEST','TEST')"
--导出结构:

expdp begin/begin directory=mydump dumpfile=begin.dmp 

content=metadata_only
--指定版本导出:

expdp begin/begin directory=mydump dumpfile=begin2.dmp 

version=10.2


保证导出数据的一致性:

exp 用 consistent=Y 参数

expdp 用 flashback_scn 或者 flashback_time=sysdate 参数

expdp begin/begin directory=mydump dumpfile=begin.dmp logfile=begin.log fla

shback_time=sysdate

CREATE OR REPLACE TRIGGER TEST.TG_TEST

 BEFORE INSERT ON TEST.T1

 REFERENCING NEW AS New OLD AS Old

 FOR EACH ROW

BEGIN

 insert into t2 (id, name) values (:New.id, :New.name);

END tg_test;

/
-- 配置自动 expdp 导出备份,使用 crontab -l
--每天晚上 3 点自动运行 crontab -e

0 3 * * * /u01/expdp.sh > /dev/null 2>&1

--导入表:

impdp begin/begin directory=mydump dumpfile=begin2.dmp 

tables=tab1

impdp begin/begin directory=mydump dumpfile=begin2.dmp 

tables=tab1 content=metadata_only

impdp begin/begin directory=mydump dumpfile=begin2.dmp 

tables=tab1 content=data_only
--导入用户:

impdp begin/begin directory=mydump dumpfile=begin2.dmp

impdp begin3/begin3 directory=mydump dumpfile=begin2.dmp 

remap_schema=begin:begin3

impdp begin/begin directory=mydump dumpfile=begin2.dmp 

tables=begin.test3 content=data_only
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值