工作中时常会遇到Oracle数据库迁移到达梦的场景,有时候客户给你一个expdp导出的文件。当你拿到后基本会按照如下方式迁移:
处理方法分2种情况:
1、 如果拿到的是全库的expdp备份,那么使用类似下面的语句可以生成一个sql文件,里面包含所有的DDL语句,当然也存在表空间的创建语句。
impdp system/oracle@swpdb directory=dp dumpfile=sw_full.dmp logfile=sw_pdb_sql.log SQLFILE=sw_pdb_full.sql
打开文件 sw_pdb_full.sql,可以看到如下语句,自行提取出来,执行即可。达梦目标端修改后也可以提前建好所有所需的表空间,当然,如果只想迁移部分用户,创建部分表空间也可轻松完成。
2、 拿到的是按用户导出的dmp文件,这时候用下面的语句导出sqlfile文件,里面是不存在表空间的创建语句的。此时分两步完成:
2.1 和上面一样,导入sqlfile文件
impdp system/oracle@swpdb directory=dp dumpfile=sw_user.dmp logfile=sw_userxxx_sql.log schemas=userxxx SQLFILE=sw_userxxx.sql
只能在文件的建表语句中中找到类似这样的表空间制定语句。
2.2 执行下面的python3脚本,完成表空间的提取和创建脚本自动输出
#!/usr/local/bin/python3
import re
import time
tablespace_name_list = []
def findtb(_file_path):
with open(_file_path, 'r') as f:
line_str = f.readline()
while line_str:
res = re.search(r'TABLESPACE "(\w+)"', line_str)
if res:
tb_name = res.group(1)
if not tb_name in tablespace_name_list:
tablespace_name_list.append(tb_name)
line_str = f.readline()
st = time.time()
findtb(r"/opt/gzdw_oracle/gzdw_sqlfile_sys.sql")
et = time.time()
print("time is spent %s" %(et-st))
for tb_name in tablespace_name_list:
crt_tbsp = "create tablespace %s datafile '%s' size 1G autoextend on;" %(tb_name, '+dgapp01')
print(crt_tbsp)
note = '''tablespace number is: %s
sql for create tablespace:
'''
print(tablespace_name_list)
print(len(tablespace_name_list))
输出如下,根据自己的实际,还可以定制修改:
time is spent 1.4894912242889404 --耗时
--表空间创建语句
create tablespace MPAC datafile '+dgapp01' size 1G autoextend on;
create tablespace TEMP datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_ISC datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_S datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_T datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_D datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_LC datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_MIP datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_V datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_U datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_B datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_R datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_IO datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_M datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_A datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_C datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_L datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_LOG datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_P datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_O datafile '+dgapp01' size 1G autoextend on;
create tablespace USERS datafile '+dgapp01' size 1G autoextend on;
create tablespace VSZ datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_N datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_MY datafile '+dgapp01' size 1G autoextend on;
create tablespace LCB_CALC_TABLESPACE datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_MW datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_V_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_B_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_T_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_C_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_LC_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_MIP_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_O_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_M_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_L_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_IO_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_R_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_S_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_D_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_P_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_A_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_U_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_N_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_LOG_IDX datafile '+dgapp01' size 1G autoextend on;
create tablespace MPAC_MY_IDX datafile '+dgapp01' size 1G autoextend on;
--表空间列表
['MPAC', 'TEMP', 'MPAC_ISC', 'MPAC_S', 'MPAC_T', 'MPAC_D', 'MPAC_LC', 'MPAC_MIP', 'MPAC_V', 'MPAC_U', 'MPAC_B', 'MPAC_R', 'MPAC_IO', 'MPAC_M', 'MPAC_A', 'MPAC_C', 'MPAC_L', 'MPAC_LOG', 'MPAC_P', 'MPAC_O', 'USERS', 'VSZ', 'MPAC_N', 'MPAC_MY', 'LCB_CALC_TABLESPACE', 'MPAC_MW', 'MPAC_IDX', 'MPAC_V_IDX', 'MPAC_B_IDX', 'MPAC_T_IDX', 'MPAC_C_IDX', 'MPAC_LC_IDX', 'MPAC_MIP_IDX', 'MPAC_O_IDX', 'MPAC_M_IDX', 'MPAC_L_IDX', 'MPAC_IO_IDX', 'MPAC_R_IDX', 'MPAC_S_IDX', 'MPAC_D_IDX', 'MPAC_P_IDX', 'MPAC_A_IDX', 'MPAC_U_IDX', 'MPAC_N_IDX', 'MPAC_LOG_IDX', 'MPAC_MY_IDX']
--表空间个数
46
通过以上方法,就可以准确建立所需要用到的表空间了。
文章
阅读量
获赞