用Shell写的一段PostgreSQL到Oracle的数据传输脚本

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
数据传输服务 DTS,数据迁移 small 3个月
推荐场景:
MySQL数据库上云
数据传输服务 DTS,数据同步 1个月
简介:

只为实现功能,不求效率和可管理性等等。

代码如下:包含有注释和讲解

#!/bin/bash

. /home/testuser/.bash_profile

# 定义异常通知邮件组

TO_MAIL=” ”

# 判断是否已经在运行

test -f /home/testuser/script/run/target_table.run

# 退出代码和超时通知代码

if [ $? -eq 0 ]; then

SYNC_TIME_CHECK=”‘`ls -1 -l –time-style=+%F\ %T /home/testuser/script/run/target_table.run|awk ‘{print $6″ “$7}’`'”

psql -t -h /tmp -p 1921 -U testuser -d edb <<EOF 1>/home/testuser/script/target_table.timeoutstats 2>&1

select ’sync_time_out’ where now()-$SYNC_TIME_CHECK::timestamp without time zone>interval ‘1 hour’;

EOF

TIME_OUT_ERROR=0

TIME_OUT_ERROR=`grep -c “sync_time_out” /home/testuser/script/target_table.timeoutstats`

if [ $TIME_OUT_ERROR -ne 0 ]; then

echo -e “`cat /home/testuser/script/target_table.timeoutstats`\n\n`date +%F%T`\n sync target_table timeout!\n\nPlease Call Digoal!\n”|mutt -s “Sync Target Table timeout!” $TO_MAIL

echo -e “sync timeout”

fi

exit 3

fi

# 生成在运行标记

touch /home/testuser/script/run/target_table.run

# 获取最大ID

MAX_T=”‘`psql -t -h /tmp -p 1921 -U testuser -d edb -c “select max(id) from -e ’s/ //g’`'”

# 同步代码

psql -t -h /tmp -p 1921 -U testuser -d edb <<EOF 1>/home/testuser/script/target_table.stats 2>&1

begin;

select ’start sync: ‘||now();

delete from 

where

id < $MAX_T ;

insert into 

(col1,col2,col3,col4,col5,col6)

select

col1,col2,col3,col4,col5,col6

from 

where

id > ${MAX_T}

;

insert into 

(col1,col2,col3,col4,col5,col6)

select

col1,col2,col3,col4,col5,col6

from 

where

id > ${MAX_T} ;

commit;

EOF

# 异常捕获发送,异常的话不删除/home/testuser/script/run/target_table.run,防止后续数据出错

ERROR=0

ERROR=`grep -c “ROLLBACK” /home/testuser/script/target_table.stats`

if [ $ERROR -ne 0 ]; then

echo -e “`cat /home/testuser/script/target_table.stats`\n\n`date +%F%T`\n sync target data error!\n\nPlease Call Digoal!\n”|mutt -s “Sync target data error!” $TO_MAIL

echo -e “sync error”

exit 2

fi

# 删除运行标记

sleep 10

rm -f /home/testuser/script/run/target_table.run

#代码完

几个问题补充:

从postgresql到oracle使用的EDB DBLINK,时间字段不能进行传输,甚至连default值都用不了,报ORA-01861错误

01861, 00000, “literal does not match format string”

// *Cause:  Literals in the input must be the same length as literals in

//  the format string (with the exception of leading whitespace).  If the

//  “FX” modifier has been toggled on, the literal must match exactly,

//  with no extra whitespace.

// *Action: Correct the format string to match the literal.

解决办法是把时间改成char(14)直接写成字符。

还有一种折中的解决办法是在target端,也就是ORACLE端使用行触发器before for each row,生成一个sysdate。

相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
Sqoop 企业级大数据迁移方案实战
Sqoop是一个用于在Hadoop和关系数据库服务器之间传输数据的工具。它用于从关系数据库(如MySQL,Oracle)导入数据到Hadoop HDFS,并从Hadoop文件系统导出到关系数据库。 本课程主要讲解了Sqoop的设计思想及原理、部署安装及配置、详细具体的使用方法技巧与实操案例、企业级任务管理等。结合日常工作实践,培养解决实际问题的能力。本课程由黑马程序员提供。
相关文章
|
3天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
4天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
11 1
|
4天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
4天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
24 5
|
7天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
7天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
15 3
|
13天前
|
弹性计算 运维 监控
|
13天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
11 0