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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章