GBase 8a
其他
文章
使用dblink在集群间同步数据的脚本
发表于2024-02-23 14:48:1290次浏览5个评论
<<!
**********************************************************
* Author : BaoZhenPeng
* Last modified : 2024-02-23
* Version : V2
* Description : 该版本实现了使用dblink并行同步表数据,且实现了完整的日志打印功能。
* *******************************************************
!
#!/bin/bash
#当前脚本执行的全路径
script_path=$(cd `dirname $0`; pwd)
#目标端dblink配置文件路径
target_dblink_config_path=${script_path}/"config/dblink_sync.conf"
#目标端口数据库用户名
db_user=$(cat ${target_dblink_config_path} | grep user | cut -d "=" -f 2)
#目标端数据库密码
db_passwd="$(cat ${target_dblink_config_path} | grep pwd | cut -d "=" -f 2)"
#要同步的库的名称
target_db_name="$(cat ${target_dblink_config_path} | grep target_database_name | cut -d "=" -f 2)"
#目标端表的名称
#target_table_name="$(cat ${target_dblink_config_path} | grep target_table_name | cut -d "=" -f 2)"
#配置好dblink名称 to do
dblink_name="$(cat ${target_dblink_config_path} | grep dblink_name | cut -d "=" -f 2)"
#源表的配置文件路径
origin_table_list_path=$1
#时间戳
sjc=$(date +"%Y-%m-%d %H:%M:%S")
#并发大小
threadNum="$(cat ${target_dblink_config_path} | grep threadNum | cut -d "=" -f 2)"
#日志目录
log_dir_path=${script_path}/"log"
#日志文件时间戳
log_sjc=$(date +"%Y%m")
#日志名称
log_name=${log_dir_path}/"dblink_sync_result_"${log_sjc}".log"
#校验参数输入是否合法及创建日志文件方法
function checkOptions(){
#如果不存在日志文件,则创建
if [ ! -d "${log_dir_path}" ];then
mkdir -p ${log_dir_path}
fi
#判断用户是否传入表配置文件路径
if [ ! -n "${origin_table_list_path}" ];then
echo "[ERROR: ]table config path can't be empty,please input it!"
echo "Usage as: sh dblik_sync_v2.sh xxx.conf"
exit;
fi
#判断用户输入的文件是否存在
if [ ! -f "${origin_table_list_path}" ];then
echo "[ERROR: ]The imported file does not exist,please check it!"
exit
fi
}
function main(){
checkOptions
mkfifo sdbfifo
exec 7<> sdbfifo
rm -rf sdbfifo
for i in $(seq 1 ${threadNum})
do
echo >&7
done
while read tablename
do
read -u7
{
#数据同步开始前,先将目标表的数据清空
echo ${sjc}" [INFO: ]begin to truncate table ${tablename} ......" >> ${log_name}
gccli -u ${db_user} -p${db_passwd} -e "truncate table ${target_db_name}.${tablename};" 2>>${log_name}
if [ $? -eq 0 ]; then
echo ${sjc}" [INFO: ]table "${tablename}" truncate success!" >> ${log_name}
else
echo ${sjc}" [ERROR: ]table "${tablename}" truncate faild!" >> ${log_name}
fi
#开始使用dblink同步数据
echo ${sjc}" [INFO: ]begin to synchronization table ${tablename} ......" >> ${log_name}
gccli -u ${db_user} -p${db_passwd} -e "use ${target_db_name};insert into ${tablename} select * from ${tablename}@${dblink_name};" 2>>${log_name}
if [ $? -eq 0 ]; then
echo ${sjc}" [INFO: ]table "${tablename}" synchronization success!" >> ${log_name}
else
echo ${sjc}" [ERROR: ]table "${tablename}" synchronization faild!" >> ${log_name}
fi
sleep 2
echo >&7
}&
done < ${origin_table_list_path}
wait
exec 7>&-
}
main
热门帖子
- 12025-12-01浏览数:182762
- 22023-05-09浏览数:25056
- 42023-09-25浏览数:18525
- 52020-05-11浏览数:17528