[草稿]oracle的逻辑备份与恢复
[TOC]
概述
- exp与expdp 都是备份数据库的,不同之处在于:
对比项 |
exp |
expdp |
总结 |
效率 |
exp是串行备份 |
expdp是并行备份 |
expdp效率更高 |
导出机制 |
exp导出的是文本型 |
expdp导出的是二进制 |
expdp导出的二进制文件更小 |
导入命令 |
exp对应imp |
expdp对应impdp |
- |
逻辑目录(directory) |
exp不需要指定逻辑目录对象 |
expdp需要指定逻辑目录对象 |
- |
客户端 |
exp/imp可在客户端执行 |
expdp/impdp只能在服务端执行 |
- |
关于空表 |
11g后exp不导出空表 |
11g后expdp可以导出空表 |
- |
准备工作
角色 |
ip及端口 |
oracle版本 |
功能 |
实例1 |
192.168.31.101:1522 |
21c |
用于作为主实例 |
实例2 |
192.168.31.102:1522 |
21c |
用于验证恢复的实例 |
ocnt=`cat /etc/bashrc | grep oracle | grep -v grep | wc -l`
if [ $ocnt -eq 0 ];then echo 'export PATH=$PATH:/opt/oracle/bin' >> /etc/bashrc; source /etc/bashrc; fi
export ORACLE_SID=orcl
export ORACLE_HOME=/opt/oracle
mkdir -p /data/oracle_1522/{tablespace,dump}
chown -R oracle:oinstall /data/oracle_1522/tablespace
chown -R oracle:oinstall /data/oracle_1522/dump
--.1.登录oracle实例
su - oracle -c "sqlplus /nolog"
--.2.在管理员账号下,创建表空间(表空间200M,每次扩展100M,最大不限制),示例 tbs_backup
conn sys/oracle as sysdba;
drop tablespace tbs_backup including contents and datafiles;
create tablespace tbs_backup datafile '/data/oracle_1522/tablespace/tbs_backup.dbf' size 200M autoextend on next 100M maxsize unlimited;
--.3.在管理员账号下,新增备份管理员(指定表空间)并授予权限,示例 dba_backup
conn sys/oracle as sysdba;
alter session set "_ORACLE_SCRIPT"=true;
drop user dba_backup cascade;
create user dba_backup identified by Admin_147 default tablespace tbs_backup temporary tablespace TEMP;
grant connect,resource,dba to dba_backup with admin option;
--.4.在管理员账号下,新增测试账号,示例 pay01
conn sys/oracle as sysdba;
alter session set "_ORACLE_SCRIPT"=true;
create user pay01 identified by Admin_147;
grant connect,resource to pay01;
ALTER USER pay01 QUOTA UNLIMITED ON USERS;
--.5.在测试账号下,创建测试表
conn pay01/Admin_147@orcl;
CREATE TABLE tab01(id number(6,0) primary key,name varchar2(10),city varchar(10));
INSERT INTO tab01 VALUES(1,'sam','beijing');
CREATE TABLE tab02(id number(6,0) primary key,name varchar2(10),city varchar(10));
INSERT INTO tab02 VALUES(1,'joy','shanghai');
--.1.登录oracle实例
su - oracle -c "sqlplus /nolog"
--.2.在管理员账号下,创建表空间(表空间200M,每次扩展100M,最大不限制),示例 tbs_restore
conn sys/oracle as sysdba;
drop tablespace tbs_restore including contents and datafiles;
create tablespace tbs_restore datafile '/data/oracle_1522/tablespace/tbs_restore.dbf' size 200M autoextend on next 100M maxsize unlimited;
--.3.在管理员账号下,新增恢复管理员(指定表空间)并授予权限,示例 dba_restore
conn sys/oracle as sysdba;
alter session set "_ORACLE_SCRIPT"=true;
drop user dba_restore cascade;
create user dba_restore identified by Admin_258 default tablespace tbs_restore temporary tablespace TEMP;
grant connect,resource,dba to dba_restore with admin option;
--.4.在管理员账号下,新增测试账号,示例 pay02
conn sys/oracle as sysdba;
alter session set "_ORACLE_SCRIPT"=true;
create user pay02 identified by Admin_258;
grant connect,resource to pay02;
ALTER USER pay02 QUOTA UNLIMITED ON USERS;
利用 exp 与 imp
echo "select table_name from all_tables where table_name like 'TAB0%';" | su - oracle -c "sqlplus pay01/Admin_147@192.168.31.101:1522/orcl"
exp dba_backup/Admin_147@192.168.31.101:1522/orcl file=/mnt/orcl_exp_full.dmp log=/tmp/orcl_exp_full.log full=y
tail -n10 /tmp/orcl_exp_full.log | grep successfully
exp dba_backup/Admin_147@192.168.31.101:1522/orcl file=/mnt/orcl_exp_user.dmp log=/tmp/orcl_exp_user.log owner=pay01
tail -n10 /tmp/orcl_exp_user.log | grep successfully
exp dba_backup/Admin_147@192.168.31.101:1522/orcl file=/mnt/orcl_exp_tabs.dmp log=/tmp/orcl_exp_tabs.log tables=tab01,tab02
tail -n10 /tmp/orcl_exp_tabs.log | grep successfully
full=y 导出整个数据库
file=xxx.log 输出的文件
log=xxx.log 屏幕输出的日志文件
buffer=64000
imp dba_restore/Admin_258@192.168.31.102:1522/orcl file=/mnt/orcl_exp_full.dmp log=/tmp/orcl_imp_full.log full=y
tail -n10 /tmp/orcl_imp_full.log | grep successfully
imp dba_restore/Admin_258@192.168.31.102:1522/orcl file=/mnt/orcl_exp_user.dmp log=/tmp/orcl_imp_user.log fromuser=pay01 touser=pay02
tail -n10 /tmp/orcl_imp_user.log | grep successfully
imp dba_restore/Admin_258@192.168.31.102:1522/orcl file=/mnt/orcl_exp_tabs.dmp log=/tmp/orcl_imp_tabs.log tables=tab01 fromuser=pay01 touser=pay02
tail -n10 /tmp/orcl_imp_tabs.log | grep successfully
full=y 导入整个数据库
file=xxx.log 输入的文件
log=xxx.log 屏幕输出的日志文件
ignore=y 是忽略创建错误
echo "select table_name from all_tables where table_name like 'TAB0%';" | su - oracle -c "sqlplus pay02/Admin_258@192.168.31.102:1522/orcl"
echo "drop table tab01;" | su - oracle -c "sqlplus pay02/Admin_258@192.168.31.102:1522/orcl"
echo "drop table tab02;" | su - oracle -c "sqlplus pay02/Admin_258@192.168.31.102:1522/orcl"
利用 expdp 与 impdp
- [实例1] 使用 expdp/impdp 必须首先创建dump目录
--.登录oracle实例
su - oracle -c "sqlplus /nolog"
--.在管理员账号下,创建expdp/impdp所必须的dump目录,授予用户对导出目录的读写权限
conn sys/oracle as sysdba;
drop directory dumpdir;
create or replace directory dumpdir as '/data/oracle_1522/dump';
grant read,write on directory dumpdir to dba_backup;
select directory_path from dba_directories WHERE directory_name='DUMPDIR';
- [实例1] 使用 expdp 导出,通过 directory 导出,不可以在 logfile 再使用自定义路径,比如
logfile=/tmp/expdp_full.log
expdp dba_backup/Admin_147@192.168.31.101:1522/orcl directory=dumpdir dumpfile=orcl_expdp_full.dmp logfile=orcl_expdp_full.log full=y
tail -n10 /data/oracle_1522/dump/orcl_expdp_full.log | grep successfully
expdp dba_backup/Admin_147@192.168.31.101:1522/orcl directory=dumpdir dumpfile=orcl_expdp_user.dmp logfile=orcl_expdp_user.log schemas=pay01
tail -n10 /data/oracle_1522/dump/orcl_expdp_user.log | grep successfully
echo "grant dba to pay01;" | su - oracle -c "sqlplus dba_backup/Admin_147@192.168.31.101:1522/orcl"
expdp pay01/Admin_147@192.168.31.101:1522/orcl directory=dumpdir dumpfile=orcl_expdp_tabs.dmp logfile=orcl_expdp_tabs.log tables=tab01,tab02
tail -n10 /data/oracle_1522/dump/orcl_expdp_tabs.log | grep successfully
expdp dba_backup/Admin_147@192.168.31.101:1522/orcl directory=dumpdir dumpfile=orcl_expdp_tbsp.dmp logfile=orcl_expdp_tbsp.log tablespaces=tbs_backup
tail -n10 /data/oracle_1522/dump/orcl_expdp_tbsp.log | grep successfully
expdp dba_backup/Admin_147@192.168.31.101:1522/orcl directory=dumpdir dumpfile=orcl_expdp_qury.dmp logfile=orcl_expdp_qury.log tables=tab01 query='WHERE id=1'
tail -n10 /data/oracle_1522/dump/orcl_expdp_qury.log | grep successfully
parallel=4 增大并行度,可减少备份时间,但会增加CPU负载
- [实例2] 使用 expdp/impdp 必须首先创建dump目录
--.登录oracle实例
su - oracle -c "sqlplus /nolog"
--.在管理员账号下,创建expdp/impdp所必须的dump目录,授予用户对导出目录的读写权限
conn sys/oracle as sysdba;
drop directory dumpdir;
create or replace directory dumpdir as '/data/oracle_1522/dump';
grant read,write on directory dumpdir to dba_restore;
select directory_path from dba_directories WHERE directory_name='DUMPDIR';
impdp pay02/Admin_258@192.168.31.102:1522/orcl directory=dumpdir dumpfile=orcl_expdp_tabs.dmp logfile=orcl_impdp_tabs.log remap_schema=pay01:pay02
impdp amber/oracle@orcl tables=tb2,tb3 directory=oracle_bak dumpfile=bak01.dmp
impdp pay02/Admin_258@192.168.31.102:1522/orcl schemas=pay01 directory=dumpdir dumpfile=orcl_expdp_tabs.dmp logfile=orcl_expdp_tabs.log;
impdp amber/oracle@orcl full=y directory=oracle_bak dumpfile=bak03.dmp
impdp scott/tiger dumpfile=expdp:scott.bak tables=emp tables_exists_action=append
impdp scott/tiger dumpfile=expdp:scott.bak tables=emp tables_exists_action=replace
impdp scott/tiger dumpfile=expdp:scott.bak tables=emp tables_exists_action=truncate
impdp scott/tiger dumpfile=expdp:scott.bak tables=emp,dept,salgrade tables_exists_action=skip
impdp user/passwd directory=xxx dumpfile=xxx.dmp remap_schema=oldschema:newschema logfile=xxx.log
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=example;
以 load data 恢复/导入
load data infile '/tmp/insert_tab2.csv' into table "tab_import" fields terminated by ',';