浅谈oracle逻辑备份数据泵备份及冷备份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浅谈oracle逻辑备份数据泵备份及冷备份相关的知识,希望对你有一定的参考价值。
逻辑备份(数据迁移):
以逻辑结构为为单位进行的备份
跨用户移动数据
跨数据库移动数据库
为测试保存原始的数据状态
对数据库进行版本升级
逻辑导出的注意事项:
exp程序在目录中发现同名文件时会直接覆盖,不提示!!
exp无法备份无段的空表
执行逻辑导出时一定要注意字符集!最好使用包含中文的小表做测试!!
导入时的数据和导出时的数据一模一样,导出之后数据库中表的数据变化全都丢失!!
逻辑导出:所有版本都可用,服务器端和客户端都可用
mkdir -p /home/oracle/expbk
SQL> create table scott.t01 as select * from dba_objects; Table created. Elapsed: 00:00:00.32
SQL> select count(*) from scott.t01; COUNT(*) ---------- 86259 Elapsed: 00:00:00.01
(1)备份单张表:生成备份路径及日志目录
exp userid=scott/tiger tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log
[[email protected] expbk]$ ll total 9748 -rw-r--r-- 1 oracle oinstall 9977856 Sep 26 13:25 t01.dmp -rw-r--r-- 1 oracle oinstall 552 Sep 26 13:25 t01.log
逻辑导入:
drop table t01 purge;
SQL> drop table scott.t01 purge; Table dropped. Elapsed: 00:00:00.11
SQL> select * from scott.t01; select * from scott.t01 * ERROR at line 1: ORA-00942: table or view does not exist Elapsed: 00:00:00.00
imp userid=scott/tiger tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log
SQL> select count(*) from scott.t01; COUNT(*) ---------- 86259 Elapsed: 00:00:00.00
(2)通过网络向远程导入:
环境:准备2个oracle服务器
网络配置如下:
vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
75 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.201.xx.75)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) 74 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.201.xx.74)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) )
#####################
vim /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = s74sit)(PORT = 1521)) ) )
连接到75,明显的是s75sit没有这张表scott.t01,如下:
[[email protected] ~]$ sqlplus scott/[email protected] SQL*Plus: Release 11.2.0.4.0 Production on Mon Sep 26 14:19:17 2016 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> select count(*) from scott.t01; select count(*) from scott.t01 * ERROR at line 1: ORA-00942: table or view does not exist Elapsed: 00:00:00.00 SQL>
远程导入到75这台服务器
imp userid=scott/[email protected] tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log
在查看
SQL> select count(*) from scott.t01; COUNT(*) ---------- 86259 Elapsed: 00:00:00.00
成功了!!!!!!!!
(3)导出数据时带有查询条件:select * from scott.emp where deptno=30;
exp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp query=\‘where deptno=30\‘ buffer=1000000 log=/home/oracle/expbk/emp_30.log
(4)导入时追加数据:ignore=y
imp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp ignore=y buffer=1000000 log=/home/oracle/expbk/imp_emp_30.log
(5)只显示导入流程,不做导入操作:show=y
imp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp ignore=y buffer=1000000 log=/home/oracle/expbk/imp_emp_30.log show=y
(6)闪回导出(依赖于undo数据):system
exp system/uplooking tables=scott.emp file=/home/oracle/expbk/emp_1040.dmp buffer=1000000 flashback_time=\"to_timestamp\(\‘2016-09-24 10:45:00\‘,\‘yyyy-mm-dd hh24:mi:ss\‘\)\" log=/home/oracle/expbk/emp_1040.log
(7)全备份导入数据:
imp system/uplooking file=/home/oracle/expbk/e03_1015.dmp full=y ignore=y show=y
只导出表结构(元数据)不导出数据:rows=n 备份模型,不备份数据
exp scott/tiger tables=ob1 rows=n file=/home/oracle/expbk/ob1_metadata.dmp log=/home/oracle/expbk/ob1_metadata.log
(8)写脚本定时备份
vi /home/oracle/expbk/exp.sh
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1 export ORACLE_SID=orcl export LANG=zh_CN.utf8 export NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252 name=`date ‘+%Y%m%d_%H%M%S‘` `$ORACLE_HOME/bin/exp userid=scott/tiger tables=ob1 file=/home/oracle/expbk/ob1\_$name.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/ob1\_$name.log`
chmod +x /home/oracle/expbk/exp.sh
(9)导出用户:owner=scott
exp userid=scott/tiger owner=scott file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/scott.log
导入用户:导入用户数据时,要在数据库中将用户创建好!
drop user scott cascade;
create user scott identified by tiger;
imp userid=scott/tiger full=y file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/impscott.log
(10)跨用户导入数据:scott-->tom
imp userid=system/uplooking file=scott.dmp fromuser=scott touser=tom tables=dept,emp,salgrade buffer=1000000 log=imptom.log
(11)使用主机管道压缩备份文件:
mknod /home/oracle/expbk/exp_pipe p
exp userid=scott/tiger owner=scott log=/home/oracle/expbk/scott.log file=/home/oracle/expbk/exp_pipe & gzip </home/oracle/expbk/exp_pipe> scott02.dmp.gz
(12)导出表空间:
exp system/uplooking tablespaces=data01 file=/home/oracle/expbk/exp_data01.dmp buffer=1000000 log=/home/oracle/expbk/exp_data01.log
导入表空间:导入时原来的表空间如果不存在,表会被导入到用户的默认永久表空间
imp system/uplooking full=y file=/home/oracle/expbk/exp_data01.dmp buffer=1000000 log=/home/oracle/expbk/imp_data01.log
(13)传输表空间模式:数据库版本必须一致,字符集必须一致
create tablespace teach10 datafile ‘/home/oracle/teach10.dbf‘ size 10m;
create table blake.crm (x int) tablespace teach10;
insert into blake.crm values (1990);
commit;
表空间需要在只读模式下
alter tablespace teach10 read only;
导出表空间元数据
exp \‘sys/uplooking as sysdba\‘ tablespaces=teach10 transport_tablespace=y file=/home/oracle/expbk/teach10.dmp log=/home/oracle/expbk/teach10.log
将数据文件传到远程
scp /home/oracle/teach10.dbf [email protected]:/u01/app/oracle/oradata/zccdb/
创建用户
sqlplus sys/[email protected] as sysdba
grant connect,resource to blake identified by blake;
将元数据导入到远程数据库
imp \‘sys/[email protected] as sysdba\‘ tablespaces=teach10 transport_tablespace=y file=/home/oracle/expbk/teach10.dmp datafiles=\‘/u01/app/oracle/oradata/zccdb/teach10.dbf\‘ log=/home/oracle/expbk/imp_teach10.log
表空间read write:
alter tablespace TEACH10 READ write;
(14)全库模式:
exp system/uplooking full=y file=full.dmp buffer=10000000 log=full.log
create table scott.t2
(
id number(10),
name1 varchar2(15),
name2 varchar2(15),
name3 varchar2(15),
name4 varchar2(15),
name5 varchar2(15),
name6 varchar2(15),
name7 varchar2(15),
name8 varchar2(15),
name9 varchar2(15),
name10 varchar2(15),
name11 varchar2(15),
name12 varchar2(15),
name13 varchar2(15),
name14 varchar2(15),
name15 varchar2(15),
name16 varchar2(15),
name17 varchar2(15),
name18 varchar2(15),
name19 varchar2(15),
name20 varchar2(15),
name21 varchar2(15),
name22 varchar2(15),
name23 varchar2(15),
name24 varchar2(15),
name25 varchar2(15),
name26 varchar2(15),
name27 varchar2(15),
name28 varchar2(15),
name29 varchar2(15),
name30 varchar2(15),
name31 varchar2(15),
name32 varchar2(15),
name33 varchar2(15),
name34 varchar2(15),
name35 varchar2(15),
name36 varchar2(15),
name37 varchar2(15),
name38 varchar2(15),
name39 varchar2(15),
name40 varchar2(15),
name41 varchar2(15),
name42 varchar2(15),
name43 varchar2(15),
name44 varchar2(15),
name45 varchar2(15),
name46 varchar2(15),
name47 varchar2(15),
name48 varchar2(15),
name49 varchar2(15),
name50 varchar2(15));
begin
for i in 1..100000 loop
insert into scott.t2(id,name1) values (i,‘A‘);
end loop;
commit;
end;
/
##########################################################################################
数据泵:oracle 10g之后的版本可用,只有在服务端可用
1.创建逻辑目录,使用逻辑目录保存数据泵的备份文件
conn / as sysdba
select * from all_directories;
create or replace directory expbk as ‘/home/oracle/expbk‘;
2.授予scott用户读写逻辑目录的权限
grant read,write on directory expbk to scott;
3.使用数据泵备份数据
只备份元数据信息
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_metadata.dmp content=metadata_only logfile=ob1_metadata.log
只数据信息
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_data.dmp content=data_only logfile=ob1_data.log
两者都备份
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_all.dmp content=all logfile=ob1_all.log
impdp scott/tiger directory=expbk dumpfile=ob1.dmp logfile=imp_ob1.log
expdp scott/tiger directory=expbk dumpfile=scott.dmp parallel=8 job_name=scott_job schemas=scott content=all logfile=scott.log
expdp system/oracle directory=expbk dumpfile=users.dmp parallel=8 job_name=users_job tablespaces=users content=all logfile=users.log
~~~~~~~~~~~~
alter tablespace teach10 read only;
expdp \‘sys/oracle as sysdba\‘ directory=expbk dumpfile=teach10.dmp tablespaces=teach10 transport_tablespace=y logfile=teach10.log
scp teach10.dmp [email protected]:/home/oracle/
scp /home/oracle/teach10.dbf [email protected]:/demo/teach10.dbf
create or replace directory expbk as ‘/home/oracle‘;
impdp \‘sys/oracle as sysdba\‘ directory=expbk dumpfile=teach10.dmp logfile=impteach10.log transport_datafiles=\‘/demo/teach10.dbf\‘
alter tablespace teach10 read write;
alter tablespace teach10 read write;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
数据泵备份全库模式:
expdp system/oracle directory=expbk dumpfile=orcl.dmp parallel=8 job_name=orcl_job full=y logfile=orcl.log
expdp scott/tiger job_name=exp_e directory=expbk include=table:\"like \‘E%\‘\" dumpfile=e.dmp logfile=e.log
expdp scott/tiger job_name=exp_e directory=expbk include=view dumpfile=v.dmp
expdp scott/tiger job_name=exp_e directory=expbk include=procedure,package,function dumpfile=p.dmp
expdp scott/tiger job_name=exp_e directory=expbk exclude=table:\"like \‘E%\‘\" dumpfile=scott_not_e.dmp logfile=e.log
########################################################################################
用户管理的物理备份:一停二拷三启动
1.冷备份(datafile offline backup)
数据库在shutdown状态进行的备份
2.热备份(datafile online backup)
需要归档模式支持
冷备份:
mkdir -p /home/oracle/coldbk/
vi /home/oracle/coldbk/shut.txt conn / as sysdba shutdown immediate exit
vi /home/oracle/coldbk/start.txt conn / as sysdba startup exit
select ‘cp -v ‘||name||‘ /home/oracle/coldbk/‘
from
(select name from v$controlfile
union all
select name from v$datafile
union all
select member from v$logfile);
vi /home/oracle/coldbk/bk.sh
export ORACLE_SID=orcl sqlplus /nolog @/home/oracle/coldbk/shut.txt cp -v $ORACLE_HOME/dbs/orapw$ORACLE_SID /home/oracle/coldbk/ cp -v $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora /home/oracle/coldbk/ cp -v $ORACLE_HOME/dbs/init$ORACLE_SID.ora /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/control01.ctl /home/oracle/coldbk/ cp -v /u01/app/oracle/fast_recovery_area/orcl/control02.ctl /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/system01.dbf /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/sysaux01.dbf /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/undotbs01.dbf /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/users01.dbf /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/redo03.log /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/redo02.log /home/oracle/coldbk/ cp -v /u01/app/oracle/oradata/orcl/redo01.log /home/oracle/coldbk/ sqlplus /nolog @/home/oracle/coldbk/start.txt
chmod +x /home/oracle/coldbk/bk.sh
后面继续讲热备份及rman备份,请多多关照,写得不好,有问题请指出,谢谢!!!!
本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1856658
以上是关于浅谈oracle逻辑备份数据泵备份及冷备份的主要内容,如果未能解决你的问题,请参考以下文章