others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

oracle 12c expdp/impdp 使用

访问量:1379 创建时间:2021-04-12

创建测试用户

[oracle@esbqasdb ~]$ mkdir /oracle/u01/bak
[oracle@esbqasdb ~]$ sqlplus /  as  sysdba
SQL> create directory dump_dir as '/oracle/u01/bak'; #新建目录对象dump_dir,
#使用 命令 select * from dba_directories 查看上面定义的目录
#创建cdb用户
SQL> create user c##scott identified by scott123  default tablespace users temporary tablespace temp quota unlimited on users;
#授权连接、管理数据库以及dba管理员权限
SQL> grant connect,resource,dba to c##scott; 
#授权用户对directory目录进行读、写执行权限
SQL>  grant read,write on directory dump_dir to c##scott;  
#使用"scott"用户连接数据库 
SQL> conn c##scott/scott123
#建表
SQL> create table student(
       sno   varchar2(3) not null,
       sname varchar2(9) not null,
       ssex  varchar2(3) not null,
       sbirthday date,
       sclass varchar2(5),
       constraint pk_student primary key(sno)
);
#插入数据
SQL> insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'zh','nan',to_date('1977-09-01','yyyy-mm-dd'),95033);

1 row created.
SQL> commit ;
##查看当前用户所存在的表
SQL> select table_name from user_tables;

TABLE_NAME
--------------------------------------------------------------------------------
STUDENT

备份恢复测试

[oracle@esbqasdb ~]$ expdp c##scott/scott123 directory=dump_dir dumpfile=scotttab.dmp tables=student
#directory    #指定其路径映射的别名名称,导出数据的路径
#dumpfile   #指定转储文件的名称,默认名称为expdat.dmp
#tables           #指定表模式导出
[oracle@esbqasdb ~]$ ls /oracle/u01/bak/
export.log  scotttab.dmp

恢复

[oracle@esbqasdb ~]$ sqlplus c##scott/scott123
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> drop table student;
Table dropped.

SQL> exit
#####恢复
[oracle@esbqasdb ~]$ impdp c##scott/scott123 directory=dump_dir dumpfile=scotttab.dmp tables=student
#####查看数据恢复情况
SQL> select table_name from user_tables; 

TABLE_NAME
--------------------------------------------------------------------------------
STUDENT

SQL> select * from STUDENT;

SNO SNAME     SSE SBIRTHDAY SCLAS
--- --------- --- --------- -----
108 zh        nan 01-SEP-77 95033

数据迁移到其他用户

创建创建"c##jack"测试用户

[oracle@esbqasdb ~]$ sqlplus /  as  sysdba
SQL> create user c##jack identified by abc123;   #创建"jack"测试用户
SQL> grant connect,resource to c##jack;              #授权"jack"测试用户
SQL> grant read,write on directory dump_dir to c##jack; 
SQL> grant unlimited tablespace to c##jack;     #不限制"jack"用户磁盘配额限制
SQL> conn c##jack/abc123 
Connected.
# #"scott"用户下无数据
SQL> select table_name from user_tables; 

no rows selected

导入数据,#将"scott"用户中的"student"表导入到"jack"用户中

[oracle@esbqasdb ~]$ impdp system/abcdefg directory=dump_dir dumpfile=scotttab.dmp tables=c##scott.student remap_schema=c##scott:c##jack;

登陆jack验证数据

SQL> conn c##jack/abc123 
Connected.
SQL> select * from student;

SNO SNAME     SSE SBIRTHDAY SCLAS
--- --------- --- --------- -----
108 zh        nan 01-SEP-77 95033

SQL> select table_name from user_tables; 

TABLE_NAME
--------------------------------------------------------------------------------
STUDENT

其他细节

[oracle@oracle ~]$ expdp c##scott/scott123 directory=dump_dir dumpfile=scottschema.dmp schemas=c##scott #导出c##scott用户模式
[oracle@oracle ~]$ impdp c##scott/scott123 directory=dump_dir dumpfile=scottschema.dmp schemas=c##scott #导入c##scott用户模式
[oracle@oracle ~]$ expdp c##scott/scott123 directory=dump_dir dumpfile=scottschema.dmp parallel=40 job_name=scott3
[oracle@oracle ~]$ impdp system/123 directory=dump_dir dumpfile=scottschema.dmp schemas=c##scott remap_schema=c##scott:c##jack;
#将c##scott中所有对象导入c##jack中
[oracle@oracle ~]$ expdp scott/tiger@orcl TABLES=emp,dept dumpfile=expdp.dmp DIRECTORY=dir;
[oracle@oracle ~]$ expdp scott/tiger@orcl directory=dir dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
[oracle@esbqasdb ~]$ cat DSGC_LOG_BIZKEY20200101-20210101.txt
directory=dump_dir
dumpfile=DSGC_LOG_BIZKEY20200101-20210101.dmp
tables=DSGC_LOG_BIZKEY
query=DSGC_LOG_BIZKEY:"where CREATION_DATE>=to_date('2020-01-01','yyyy-mm-dd')  and CREATION_DATE<=to_date('2021-01-01','yyyy-mm-dd')"
[oracle@oracle ~]$ expdp system/123 directory=dump_dir dumpfile=tablespaceusers.dmp tablespaces=users #导出users表空间
[oracle@oracle ~]$ impdp system/123 directory=dump_dir dumpfile=tablespaceusers.dmp tablespaces=users #导入users表空间
[oracle@esbqasdb ~]$ expdp system/abcdefg directory=dump_dir dumpfile=full.dmp full=y #导出整个数据库
[oracle@esbqasdb ~]$ impdp  system/abcdefg directory=dump_dir dumpfile=full.dmp full=y #导入整个数据库
impdp system/manager DIRECTORY=dir DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION
登陆评论: 使用GITHUB登陆