article/使用 ora2pg 将oracle 数据导入 postgresql.md
2022-08-28 09:57:48 +08:00

11 KiB
Raw Blame History

使用 ora2pg 将oracle 数据导入 postgresql

安装edb

安装edb 源

yum -y install https://yum.enterprisedb.com/edbrepos/edb-repo-latest.noarch.rpm

设置账号密码

sed -i "s@<username>:<password>@start:oIVqaoGTDFL5aYHD@" /etc/yum.repos.d/edb.repo

安装 扩展包 源

yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

安装edb

yum -y install edb-as14-server 

初始化

PGSETUP_INITDB_OPTIONS="-E UTF-8 -D /home/edbdata" /usr/edb/as14/bin/edb-as-14-setup initdb

PGSETUP_INITDB_OPTIONS="-E UTF-8 -D /home/edbdata" /usr/edb/as12/bin/edb-as-12-setup initdb



PGSETUP_INITDB_OPTIONS="-E UTF-8 -D /home/edbdata" /usr/edb/as12/bin/edb-as-12-setup initdb

启动 edb

systemctl start edb-as-14

连接edb

sudo su - enterprisedb
psql edb

常见问题

  • 远程连接数据库失败 修改文件 data/pg_hba.conf 添加

    
    host all all 0.0.0.0/0 md5
    
    
  • 修改数据库密码在edb 库执行)

    
    ALTER USER enterprisedb WITH PASSWORD 'yiing&654';
    
    

安装 ora2pg

安装 perl 环境

yum install -y perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
yum -y install perl-CPAN

安装Oracle客户端

官网下载 Oracle Instant Client 等包 上传服务器安装

rpm -ivh oracle-instantclient19.15-basic-19.15.0.0.0-1.x86_64.rpm
rpm -ivh oracle-instantclient19.15-devel-19.15.0.0.0-1.x86_64.rpm
rpm -ivh oracle-instantclient19.15-jdbc-19.15.0.0.0-1.x86_64.rpm
rpm -ivh oracle-instantclient19.15-sqlplus-19.15.0.0.0-1.x86_64.rpm

设置环境变量ORACLE_HOME

export ORACLE_HOME=/usr/lib/oracle/19.15/client64/

本地已安装有Oracle数据库

ORACLE_HOME如下设置

export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

安装 perl 依赖

perl -MCPAN -e 'install DBD::Oracle'
perl -MCPAN -e 'install DBI'
perl -MCPAN -e 'install DBD::Pg'

按照自己 的路径设置 环境变量

export POSTGRES_HOME=/usr/edb/as14
export POSTGRES_HOME=/var/lib/edb/as14

常见问题

Can't locate Test/NoWarnings.pm in @INC (@INC contains: t/lib /root/.cpan/build/DBD-Oracle-1.83-LDK8k0/blib/lib /root/.cpan/build/DBD-Oracle-1.83-LDK8k0/blib/arch /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at t/26exe_array.t line 15.

安装 perl-Test-NoWarnings-1.04-2.el7.noarch 解决

提示测试不通过 手动下载源码 安装

下载地址:https://metacpan.org/release/DBI

tar -zxvf DBI-1.642.tar.gz
cd DBI-1.642
perl Makefile.PL
make
make install

安装ora2pg

下载软件包 https://github.com/darold/ora2pg/releases

默认安装在/usr/local/bin/ 目录下

[root@Test01 ~]# tar -xjf  ora2pg-20.0.tar.bz2
[root@Test01 ~]# cd ora2pg-xx/
[root@Test01 ~]# perl Makefile.PL  PREFIX=<your_install_dir>
[root@Test01 ora2pg-18.2]# make && make install

简单检查 ora2pg --help

迁移数据

初始化项目

//初始化项目
ora2pg --init_project ora2pg


ora2pg -t SHOW_VERSION -c config/ora2pg.conf
ora2pg -t SHOW_SCHEMA -c config/ora2pg.conf
ora2pg -t  SHOW_TABLE -c config/ora2pg.conf
ora2pg -t  SHOW_COLUMN   -c config/ora2pg.conf
ora2pg -t  SHOW_ENCODING    -c config/ora2pg.conf

配置配置文件

默认配置文件即可 需要修改

# Set Oracle database connection (datasource, user, password)
ORACLE_DSN	dbi:Oracle:host=dev-db.yldev.net;sid=hcc;port=1521
ORACLE_USER	icc		
ORACLE_PWD	ICCICC

# Oracle schema/owner to use
SCHEMA	ICC



# Set which object to export from. By default Ora2Pg export all objects.
# Value must be a list of object name or regex separated by space. Note
# that regex will not works with 8i database, use % placeholder instead
# Ora2Pg will use the LIKE operator. There is also some extended use of
# this directive, see chapter "Limiting object to export" in documentation.


# 可以使用正则  如  t_.*   可以排除 不想导出的对象 如!t_t2
#配置中文释义  https://blog.csdn.net/Loiterer_Y/article/details/110927688
#ALLOW		TABLE_TEST

# The value can be a comma delimited list of schema but not when using TABLE
# export type because in this case it will generate the CREATE SCHEMA statement
# and it doesn't support multiple schema name. For example, if you set PG_SCHEMA
# to something like "user_schema, public", the search path will be set like this
#        SET search_path = user_schema, public;
# forcing the use of an other schema (here user_schema) than the one from Oracle
# schema set in the SCHEMA directive. You can also set the default search_path
# for the PostgreSQL user you are using to connect to the destination database
# by using:
#        ALTER ROLE username SET search_path TO user_schema, public;
#in this case you don't have to set PG_SCHEMA.
#PG_SCHEMA	LLOW	
PG_SCHEMA	icc	

测试连接

执行以下命令 返回oracle 版本则连接成功

ora2pg -t SHOW_VERSION -c config/ora2pg.conf

导出表结构

先安装 screen 防止意外断开 导出失败

yum install -y screen

screen 常用命令

//开启一个screen
screen -S name 
//进入一个 screen 
screen -r name
// screen 列表
screen -ls

可以用快捷键 Ctrl+a d(即按住 Ctrl依次再按 a,d),而会话中的程序不会关闭

导出数据

该命令根据数据库大小 表大小等待时间不一 建议使用screen 运行

ora2pg -t COPY -o data.sql -b ./data -c ./config/ora2pg.conf

修改校验表结构

导出完成后 在 ./schema/tables 下有转换好的表结构(主要修改关键字段)

oid
tableoid
xmin
cmin
xmax
cmax
ctid

导入数据库

切换到enterprisedb 用户

执行 sh import_all.sh 按照提示导入数据

附录

修改用户组 权限

chown [-R] ownname:groupname filename
chmod -R  777 filename

追踪服务日志

journalctl -f  -u icc-analysis.service

查看文件大小

df -hl
df -h path
ls -lht

du -ah --max-depth=1 /path

查看删除的文件
lsof -n | grep deleted

du -bsh /

Linux 替换命令 sed

参数说明:

-e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
-f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
-h或--help 显示帮助。
-n或--quiet或--silent 仅显示script处理后的结果。
-V或--version 显示版本信息。

动作说明:

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何东东;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
实例

可以使用正则  特殊字符\转义 


sed ”s/要被取代的字串/新的字串/g“ fileName


psql 导入导出文件

//导入
pg_restore 
psql -Uicc -dicc -f ./xxx.sql
psql -Uicc -dicc -h10.23.101.52  -f ./schema/tables/table.sql

//导出表 模式  数据
pg_dump -h10.23.101.52 -Uicc -dicc> ~/dum.sql
pg_dump -h10.23.101.52 -Uicc -dicc -f icc.dmp

报错停止 -v ON_ERROR_STOP=1 

-a  只要数据 
-s	只要表结构
-O	不设置表归属
-n, --schema=SCHEMA      只转储指定名称的模式
-T 排除表
-t  指定表
-Z 压缩0-9


pg_dump -U postgres vendemo | gzip > /vendemo.gz 备份下来只有30多k

// 导出压缩文件
pg_dump -h10.23.101.52 -Uicc -dicc  -t 表名 -t表名 -a -O | gzip > iccData.gz
pg_dump -Uicc -dicc | gzip > iccData.gz


//导入压缩文件
gzip -d iccData.gz | psql -Uicc -dicc -h10.23.101.52
//没测试过
psql -Uicctestedb -dicctestedb  < gzip -d iccData.gz







./pg_dump -h10.23.101.119 -Uicc -dicc  -o | gzip > ~/iccData_202206231632.gz


psql -U iccedb -d iccedb -h 192.168.53.118 -n 'icc2'  | gzip > ~/iccedb_icc2_202206301142.gz

psql -U iccedb -d iccedb -h 192.168.53.118 -v schema=icc2  | gzip > ~/iccedb_icc2_202206301142.gz


gzip -d ~/iccData_202206231634.gz | psql -Uiccedb -diccedb -h192.168.53.118 





pg_dump -Uenterprisedb -diccedb -h192.168.53.118 -n icc2  | gzip >  ~/iccedb_icc2_202206301214.gz



pg_dump  -Uenterprisedb -dicc -h10.23.101.119 -n icc  | gzip >  ~/icc_icc_202207011859.gz


pg_dump -t 'icc.*' -T icc.t_icc_inv_leg -T icc.t_icc_inv_leg_rate -T tcn_stat_new-dicc >  ~/icc_icc_202207081700.sql



pg_dump  -Uenterprisedb -dicc -h10.23.101.119 \
-T t_icc_inv_leg \
-T t_icc_inv_leg_rate \
-T tcn_stat_new  | gzip >  ~/icc_icc_202207081500.gz



./pg_dump  -Uenterprisedb -dicc -h10.23.101.119  -O \
-n icc  -s \
-f ~/icc_data_20220719_2130.sql

yiing&654






#导出 icc 模式下  排除 t_icc_inv_seg 等表  采用自定义格式压缩等级9最大 

./pg_dump  -Uenterprisedb -dicc -h10.23.101.119  \
-O -v -F c -Z 9  \
-t 'icc.*' \
-T '*.t_icc_inv_seg'  \
-T '*.t_icc_inv_leg_rate' \
-T '*.t_icc_hsd_inv_leg' \
-T '*.t_icc_inv_leg' \
-T '*.tcn_stat_new' \
-T '*.tcn_stat_sales_volume_total' \
-T '*.t_foreign_ticket_record' \
-T '*.tcn_stat_sales_volume_tmp' \
-T '*.t_icc_hsd_inv_seg' \
-T '*.tcn_stat_sales_volume' \
-T '*.t_icc_inv_seg_cabin' \
-f ~/icc_data_20220719_test.sql


恢复
./pg_restore -Uenterprisedb -dtest2 -h10.23.101.119 \
-F c  -c   --strict-names ~/icc_data_20220719_test.sql






./pg_dump -Uenterprisedb -diccedb -h192.168.53.118 -O -a -f ~/diccedb_202207_29.data.sql




./pg_dump -h10.23.101.242 -Uenterprisedb -dalgorithm   | gzip > /home/algorithmEdbData.gz


算法私服
/usr/edb/as12/bin/psql -Uenterprisedb -dalgorithm -h172.27.127.101 -f /home/wayne/algorithmEdbData
/usr/edb/as12/bin/pg_dump -h10.23.101.242 -Uenterprisedb -dalgorithm   | gzip > /home/algorithmEdbData.gz



算法
./psql -Uenterprisedb -dalgorithm -h10.23.101.242
./pg_dump -h10.23.101.242 -Uenterprisedb -dalgorithm   | gzip > /home/algorithmEdbData.gz


uat
psql -Uiccedb -diccedb -h192.168.53.118 -f ~/


sit
psql -Uicctestedb -dicctestedb -h192.168.53.123 -f ~/


查询连接数

show max_connections;
SELECT COUNT(*) from pg_stat_activity;

edb9 启动方式

Linux
/etc/init.d/ppas-9.x <start|stop|restart|status|reload>
service ppas-9.x <start|stop|restart|status|reload>
通用方式:
sudo su - enterprisedb
$PGHOME/bin/pg_ctl <start|stop|restart|status|reload> [options]

opt/PostgresPlus/9.5AS