Shell Copy线上DB数据到Beta库脚本

Shell Copy线上DB数据到Beta库脚本

#!/bin/bash

#导出线上数据库到Sql, 并且做事务批量提交。五千提交一次
# Example;sh dump_db.sh --s-db csg_bill --tables "prepay_settle_bill_index#1=1 order by id desc limit 13000" --t-db csg_bill_d
#导入参数:
# --s-db 必选
#   需要导出数据库       
#   如:--s-db csg_bill
#
# --t-db 
#       需要导入的数据库
#   如: --t-db csg_bill_d
#
# --tables 可选
#         需要导出的table,多个table使用|进行分割,可选(括号里面表示查询过滤条件)
#        如 --table prepay_settle_bill_index#id>1000 limit 10,10|prepay_bill_detail_content#1=1 order by id  desc limit 1000|prepay_settle_bill_content 默认为所有table表
#
# -d 可选
#         需要导出的目录地址,默认为当前目录
#
# --s-host 可选 默认为线上从库
#
# --s-port 可选 默认为3306
#
# --s-user 可选 默认为线上从库user
#
# --s-pass 可选 默认为现傻过你从库password
#
# --t-host 可选 默认为beta host
#
# --t-port 可选 默认为3306
#
# --t-user 可选 默认为beta库user
#
# --t-pass 可选 默认为beta password
#
#
#数据库配置信息 如果不采用默认会通过输入进行替换
source_db_host="192.168.242.123"
source_port="3306"
source_user="source_user"
source_pass="source_pass"
source_DB=""

target_db_host="127.0.0.1"
target_port="3306"
target_user="target_user"
target_pass="target_pass"
target_DB=""

dir="/tmp/"
# 设置解析参数规则
OPTION_TEMP=`getopt -o t:d: --long help,s-db:,t-db:,tables:,s-host:,s-porti:,s-user:,t-host:,t-port:,t-user:,t-pass:: -- "$@"`

#need export tables
declare -a tables
#need export conditions
declare -a conditions

eval set -- "$OPTION_TEMP"

# get input args map to parameter

while true ; do
    case "$1" in 
    --s-host)
        ## set source_db_host param
        source_db_host=$2  
        shift 2;;
    --s-db)
        ##set source_db param
        source_DB=$2
        shift 2 ;;

    --s-user)
        ## set source_user param
        source_user=$2
        shift 2 ;;

    --s-pass)
        ## set source_pass param 
        source_pass=$2
        shift 2;;
    --s-port)
        ## set source_port param
        source_port=$2
        shift 2;;

    --t-host)
        ## set source_db_host param
             shift 2;;

    --t-db)
        ##set source_db param
        target_DB=$2
           shift 2 ;;

    --t-user)
          ## set source_user param
        target_user=$2
           shift 2 ;;

    --t-pass)
          ## set source_pass param 
        target_pass=$2
           shift 2;;

    --t-port)
          ## set source_port param
        target_port=$2
          shift 2;;
    --tables)
        ## set tables and conditions
        if [[ $2 == "" ]] 
        then
            echo "缺少参数";
            exit;
        fi
        OLD_IFS="$IFS"
        IFS="|" 
        arr=($2) 
        IFS="$OLD_IFS" 
        #echo ${arr[0]}
        #echo ${arr[1]}
        for (( i = 0 ; i < ${#arr[@]} ; i++ ))
        do
            item=${arr[$i]}
            table=$(echo $item | awk -F '#' '{print $1}')
            condition=$(echo $item | awk -F '#' '{print $2}')
            if [[ $table == "" ]]
            then
                echo '--tables option param error'
            fi
            tables[$i]=$table
            conditions[$i]=$condition
        done



        shift 2;;

    -d)
        ## set directory 
        dir=$2;
        shift 2;;
    --help)
        ## help menu
        echo '导出线上数据库到Sql, 并且做事务批量提交。五千提交一次
 Example;sh dump_db.sh --s-db csg_bill --tables "prepay_settle_bill_index1=1 order by id desc limit 13000" --t-db csg_bill_d
导入参数:
 --s-db 必选
       需要导出数据库       
       如:--s-db csg_bill
 --t-db 
       需要导入的数据库
       如: --t-db csg_bill_d
 --tables 可选
         需要导出的table,多个table使用|进行分割,可选(括号里面表示查询过滤条件)
        如 --table prepay_settle_bill_indexid>1000 limit 10,10|prepay_bill_detail_content1=1 order by id  desc limit 1000|prepay_settle_bill_content 默认为所有table表
 -d 可选
         需要导出的目录地址,默认为当前目录
 --s-host 可选 默认为线上从库
 --s-port 可选 默认为3306
 --s-user 可选 默认为线上从库user
 --s-pass 可选 默认为现傻过你从库password
 --t-host 可选 默认为beta host
 --t-port 可选 默认为3306
 --t-user 可选 默认为beta库user
 --t-pass 可选 默认为beta password'
    exit;;
    --)shift;break;;

    *)echo "unkown param $1";exit;
    esac

done

if [[ $source_DB == "" ]] 
then
    echo 'source dataSource can not be "" , set by option --s-db'
    exit;
fi

#if [[ $target_DB == "" ]]
#then
#   echo 'source dataSource can not be "" , set by option --t-db'
#   exit;
#fi

#dunp source data to dir
echo 'Dump sourceData........'
for (( i = 0 ; i < ${#tables[@]} ; i++ ))
do
    table=${tables[$i]}
    condition=${conditions[$i]}
    if [[ $condition == "" ]]
    then
        mysqldump --skip-opt -h $source_db_host -P $source_port   -u $source_user -p$source_pass $source_DB  $table >"$dir""$table"_temp.sql

    else
        mysqldump --skip-opt -h $source_db_host -P $source_port   -u $source_user -p$source_pass $source_DB  $table --where "$condition">"$dir""$table"_temp.sql
    fi
    #批量提交
    cat "$dir""$table"_temp.sql | awk '{if(NR%5000==0){print "commit;\nbegin;\n"$0}else{ print $0}}END{print "commit;"}' > "$dir""$table".sql
    #rm temp file
    rm "$dir""$table"_temp.sql
done

echo 'Dump sourceData Done .......'

ls $dir

if [[ $target_DB != "" ]]
then

    echo "import data into targetSource $target_DB ......."

    for (( i = 0 ; i < ${#tables[@]} ; i++ ))
    do
        table=${tables[$i]}
        filename="$dir""$table".sql
        echo excute "$dir""$table".sql start 
        mysql -s --default-character-set=utf8  -u$target_user -p$target_pass $target_DB -h  $target_db_host -P $target_port -e "source $filename"
        echo excute "$dir""$table".sql end
    done
    echo "Done ........"
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值