生产环境下,oracle不同用户间的数据迁移。第三部分
任务名称:生产环境下schema ELON数据迁移至schema TIAN
########################################
前期准备:
1:确认ELON用户下的对象状态
select owner,constraint_name,constraint_type,table_name,status,validated from dba_constraints where owner='ELON'
owner constraint_name constraint_type table_name status validated
ELON FK_DUDECT_RESULT_AGREEMENT_ID R T_DUDECT_RESULT DISABLED NOT VALIDATED
有一个外键约束状态为DISABLED NOT VALIDATED
2:确认两个用户的对象有无重名
SQL>select distinct(object_type) from dba_objects where owner='ELON';
SEQUENCE
TRIGGER
TABLE
INDEX
SQL>select count(*) from dba_objects where owner='ELON';
56
SQL>select count(*) from dba_objects where owner='TIAN';
283
[oracle@PROD-DB ~]$ diff TIAN_object.txt ELON_object.txt
或者:select count(*) from dba_objects where owner='ELON' and object_name not in (select object_name from dba_objects where owner='TIAN');
56
结论:无重名对象
3:确定两个用户的约束有无重名
SQL>select count(*) from dba_constraints where owner='TIAN';
1441
SQL>select count(*) from dba_constraints where owner='ELON';
179
[oracle@PROD-DB ~]$ sqlplus / as sysdba <TIAN_con.sql
[oracle@PROD-DB ~]$ diff TIAN_con1.txt ELON_con1.txt > diff.txt
也可以这样排查:
SQL>select count(*) from dba_constraints where owner='ELON' and constraint_name not in (select constraint_name from dba_constraints where owner='TIAN');
179
结论:无重名的约束
4:确定ELON默认数据表空间、临时表空间、索引表空间
SQL>select username,default_tablespace,temporary_tablespace from dba_users where username = 'ELON';
ELON ELON_DATA01 ELON_TEMP
SQL>select username,default_tablespace,temporary_tablespace from dba_users where username = 'TIAN';
TIAN TIAN_DATA01 TIAN_TEMP
SQL>select owner,index_name,tablespace_name from dba_indexes where owner='ELON';
ELON IDX_REQ_DATE ELON_DATA01
ELON FK_ID ELON_DATA01
ELON IDX_T_DU_RES ELON_DATA01
ELON PK_T_TEMP ELON_IDX01
ELON PK_T_SYS_EXCEPTION ELON_IDX01
ELON PK_T_MESSAGE ELON_IDX01
5:确定dump目录及目录大小
SQL> select * from dba_directories;
OWNER DIRECTORY_NAME DIRECTORY_PATH ORIGIN_CON_ID
---------- ------------------------- -------------------------------------------------- -------------
SYS EXPDP_DIR /data/backup
6:停服务,确保无新数据写入:
7:数据泵导出ELON数据和备份TIAN数据:
7.1数据泵导出ELON数据和备份TIAN数据
expdp system/xxxxxx SCHEMAS=ELON directory=EXPDP_DIR dumpfile =ELON_`date +"%Y%m%d%H%M%S"`.dmp logfile=ELON_`date +"%Y%m%d%H%M%S"`_exp.log
expdp system/xxxxxx SCHEMAS=TIAN directory=EXPDP_DIR dumpfile =TIAN_`date +"%Y%m%d%H%M%S"`.dmp logfile=TIAN_`date +"%Y%m%d%H%M%S"`_exp.log
7.2向TIAN的schema,利用数据泵导入ELON数据:
1)向TIAN导入数据前,查询表空间利用情况:
SQL> select df.tablespace_name ,totalspace total_size, (totalspace-freespace) used_size,freespace avail_size ,round((1-freespace/totalspace)*100) as used_ratio
2 from (select tablespace_name,round(sum(bytes)/1024/1024) totalspace
from dba_data_files group by tablespace_name) df,(select tablespace_name,round(sum(bytes)/1024/1024) freespace
4 from dba_free_space group by tablespace_name) fs where df.tablespace_name=fs.tablespace_name;
TABLESPACE_NAME TOTAL_SIZE USED_SIZE AVAIL_SIZE USED_RATIO
------------------------------ ---------- ---------- ---------- ----------
TIAN_DATA01 20480 8087 12393 39
ELON_IDX01 10240 88 10152 1
SYSAUX 3072 1739 1333 57
UNDOTBS1 83910 8225 75685 10
ELON_DATA01 20480 1979 18501 10
TIAN_IDX01 10240 3591 6649 35
USERS 1024 2 1022 0
SYSTEM 2048 845 1203 41
11 rows selected.
SQL> select a.use_sp 表空间,
round((use_size + free_size),4) || 'G' 总空间,
round(use_size,4) || 'G' 已使用,
round(free_size,4) || 'G' 可用,
round(free_size / (use_size + free_size),4) * 100 || '%' 可用百分比
from (select/*+rule(a)+*/ tablespace_name use_sp,
sum(bytes) / 1024 / 1024 / 1024 use_size
from dba_segments
group by tablespace_name) a,
(select tablespace_name use_sp,
sum(bytes) / 1024 / 1024 / 1024 free_size
from dba_free_space
group by tablespace_name) b
where a.use_sp = b.use_sp;
表空间 总空间 已使用 可用 可用百分比
--------------- ---------- --------------- --------------- --------------------
TIAN_DATA01 20.0723G 7.9698G 12.1025G 60.29%
TIAN_IDX01 10.0049G 3.5115G 6.4934G 64.9%
ELON_DATA01 19.998G 1.9305G 18.0676G 90.35%
ELON_IDX01 9.999G .0852G 9.9138G 99.15%
SYSAUX 3.0801G 1.7782G 1.3019G 42.27%
SYSTEM 1.999G .8244G 1.1746G 58.76%
UNDOTBS1 81.9404G 8.0297G 73.9107G 90.2%
USERS .999G .0007G .9984G 99.93%
10 rows selected.
2)导入数据
[oracle@PROD-DB ~]$impdp system/xxxxxx directory=EXPDP_DIR dumpfile=ELON_20180412224710.dmp logfile=TIAN_`date +"%Y%m%d%H%M%S"`_impdp.log remap_schema=ELON:TIAN remap_tablespace=ELON_DATA01:TIAN_DATA01,ELON_IDX01:TIAN_IDX01,ELON_TEMP:TIAN_TEMP TABLE_EXISTS_ACTION=SKIP
2)完成之后,查看表空间利用率,数据增长是否正常
SQL> select a.use_sp 表空间,
round((use_size + free_size),4) || 'G' 总空间,
round(use_size,4) || 'G' 已使用,
round(free_size,4) || 'G' 可用,
round(free_size / (use_size + free_size),4) * 100 || '%' 可用百分比
group by tablespace_name) b
from (select/*+rule(a)+*/ tablespace_name use_sp,
sum(bytes) / 1024 / 1024 / 1024 use_size
from dba_segments
group by tablespace_name) a,
(select tablespace_name use_sp,
sum(bytes) / 1024 / 1024 / 1024 free_size
from dba_free_space
group by tablespace_name) b
where a.use_sp = b.use_sp;
表空间 总空间 已使用 可用 可用百分比
--------------- ---------- --------------- --------------- --------------------
TIAN_DATA01 20.0723G 9.9618G 10.1105G 50.37%
TIAN_IDX01 10.0049G 3.6025G 6.4023G 63.99%
ELON_DATA01 19.998G 1.9305G 18.0676G 90.35%
ELON_IDX01 9.999G .0852G 9.9138G 99.15%
SYSAUX 3.0801G 1.7806G 1.2996G 42.19%
SYSTEM 1.999G .8244G 1.1746G 58.76%
UNDOTBS1 81.9404G 8.1352G 73.8052G 90.07%
USERS .999G .0007G .9984G 99.93%
10 rows selected.
7.3监控数据泵的工作状态:
SQL> select * from DBA_DATAPUMP_JOBS;
SQL> select * from DBA_DATAPUMP_SESSIONS;
SQL> SELECT OPNAME, TARGET_DESC, SOFAR, TOTALWORK FROM V$SESSION_LONGOPS;
8:验证
迁移之后TIAN总的对象数目
SQL> select count(*) from dba_objects where owner='TIAN';
339
即:283+56
ELON的对象全部迁移到TIAN
SQL>select count(*) from dba_objects where owner='ELON' and object_name not in (select object_name from dba_objects where owner='TIAN');
0
迁移之后TIAN总的约束数目
SQL> select count(*) from dba_constraints where owner='TIAN';
1620
select count(*) from dba_constraints where owner='ELON' and constraint_name not in (select constraint_name from dba_constraints where owner='TIAN');
155
#因为ELON下包含155个非空约束、17个主键约束、7个外键约束;非空约束,系统生成,在导出导入的过程中,约束名发生改变(如SYS_C0017556)
9:回收ELON权限
SQL> select * from dba_sys_privs where grantee='ELON'
ELON CREATE TABLE NO NO
ELON CREATE VIEW NO NO
ELON DEBUG CONNECT SESSION NO NO
ELON DEBUG ANY PROCEDURE NO NO
ELON CREATE DATABASE LINK NO NO
SQL> revoke