#!/bin/sh
# -------------------------------------------------------------------------------
# Filename:    db_gen_config.sh
# Revision:    1.0
# Date:        2016/12
# Author:      dekun.dai
# Description: 生成一个同步表结构用的配置文件
# -------------------------------------------------------------------------------
source ~/.bash_profile

work_dir=/home/qa-deploy-utils/db-utils
source_db_host=$1
# source_db_pwd=`grep source_db_pwd $work_dir/config/db_config | cut -d "=" -f 2`

target_db_host=$4
target_db_user=$5
target_db_pwd=$6

sync_db_name=$7
database_name=$sync_db_name
source_db_user=`curl -s http://192.168.4.3:10088/config_server/get_db_config/$database_name/user`
source_db_pwd=`curl -s http://192.168.4.3:10088/config_server/get_db_config/$database_name/pwd`

config_file_name=$8
db_backup_sub_folders=$9
use_cache=${10}
target_db_port=${11}

port=""
sys_table_list=""

sync_config_folder=~/db_sync_configs
base_config_file=$work_dir/config/base_config.json
cache_dir=/home/daily_db_backup

# if [ $# -ne 9 ]; then
# 	echo "db_gen_config.sh"
# 	echo "Total Number of Parameters : $#"
# 	echo "source_db_infos：source_db_host: $1, source_db_user: $2"
# 	echo "target_db_infos：target_db_host: $4, target_db_user: $5"
# 	echo "sync_db_name: $7"
# 	echo "config_file_name: $8"
# 	echo "db_backup_sub_folders: $9"
# 	exit 1
# fi

echo "prepare to get source_port"
# if [[ $database_name = 'xyqb' ]]; then
# 	# 信用钱包database
# 	sys_table_list='baitiao_merchant baitiao_secure_key channel funding_contract funding_corp loan_product merchant_channel payeco_return_url_info red_packet t_phonenum zmxy_return_url configuration secure_proxy funding_corp_config funding_info_map baitiao_preferential_rule funding_corp_policy'
# 	port='3306'
# elif [[ $database_name = 'xyqb_app' ]]; then
# 	# app-backend使用的数据库
# 	sys_table_list='activities app_banners app_config app_versions banners blanknote_entries credit_questions hot_games loan_banners loan_questions migrations sharings widgets js_patch upgrade_version'
# 	port='3307'
# elif [[ $database_name = 'payment_center' ]]; then
# 	# 支付中心
# 	sys_table_list='card_switcher merchant merchant_config pay_approach_config merchant_pay_approach_relation merchant_bank_config quota'
# 	port='3308'
# elif [[  $database_name = 'risk_control'  ]]; then
# 	# 谛听database
# 	sys_table_list='company product_strategy_dispatcher quantify_product red_packet_info user'
# 	port='3309'
# elif [[  $database_name = 'dujiangyan'  ]]; then
# 	# dujiangyan
# 	sys_table_list='external_coopr'
# 	port='3310'
# elif [[  $database_name = 'mall'  ]]; then
# 	# 白条商城
# 	sys_table_list='activity_3c_product ad_detail admin_banner advertisement_type album app_channel app_channel_ad app_name app_name_ad category category_banner category_nature commodity_slogan commodity_slogan_product configuration edx_city faster_entrance faster_entrance_image goods_after_sale_config goods_after_sale_step home_config home_page hot_category jd_address jd_after_sale_config jd_brand jd_category jd_product_sku_image jd_property jd_sku jd_sku_detail_image jd_sku_specs life_faster_entrance life_service mall_category mall_config merchant nature_value new_3c_product product product_category product_nature product_nature_value_bak product_sku product_sku_image product_specification product_spu recommend_brand recommend_product sale_product_detail sale_product_theme spu_image supplier tab_navigate title_banner loan_config virtual_brand virtual_category virtual_product_discount virtual_product_hot virtual_product_sku virtual_product_spu virtual_product_template'
# 	source_db_host='192.168.3.19'
# 	port='3306'
# elif [[  $database_name = 'qinqiong'  ]]; then
# 	# qinqiong
# 	sys_table_list='secure_key secure_proxy'
# 	port='3310'
# elif [[  $database_name = 'cash_loan_flow'  ]]; then
# 	# cash_loan_flow
# 	sys_table_list='order_callback callback_configuration channel_configuration filter_configuration'
# 	source_db_host='192.168.4.80'
# 	port='8001'
# elif [[  $database_name = 'quartz'  ]]; then
# 	# 定时任务
# 	sys_table_list='certain_time_job'
# 	port='3310'
# elif [[ $database_name = 'contract' ]]; then
# 	# 合同
# 	sys_table_list='contract_template_new'
# 	port='3311'
# elif [[ $database_name = 'fund_manage' ]]; then
# 	# fund_manager
# 	sys_table_list='business_info email_info fund_info merchant'
# 	port='3310'
# elif [[  $database_name = 'financial_system'  ]]; then
# 	# 财务系统
# 	sys_table_list='collection_user sys_dictionary sys_status_edge sys_status_vertice'
# 	port='6603'
# elif [[  $database_name = 'financial_system_api'  ]]; then
# 	# 财务系统api
# 	sys_table_list='mailinfo'
# 	port='6603'
# elif [[  $database_name = 'gyxd'  ]]; then
# 	# 广源小贷系统
# 	sys_table_list='administrative_districts templet user'
# 	source_db_host='192.168.4.116'
# 	port='6602'
# elif [[  $database_name = 'offline'  ]]; then
# 	# 线下白条
# 	sys_table_list='goods_detail merchant operator_user'
# 	port='3311'
# elif [[  $database_name = 'redpacket'  ]]; then
# 	# 红包系统
# 	sys_table_list='tb_activity_channel tb_dict_channel tb_dict_out_channel'
# 	source_db_host='192.168.4.116'
# 	port='6001'
# elif [[  $database_name = 'spider_center'  ]]; then
# 	# 授信中心
# 	sys_table_list='merchant merchant_config'
# 	source_db_host='192.168.4.253'
# 	port='3317'
# elif [[  $database_name = 'silk_road'  ]]; then
# 	# silk_road
# 	sys_table_list=''
# 	source_db_host='192.168.4.80'
# 	port='9999'
# elif [[  $database_name = 'xyqb_user'  ]]; then
# 	# 用户中心
# 	sys_table_list='merchant_config merchant'
# 	port='3313'
# elif [[ $database_name = 'merchant' ]]; then
# 	# 商户
# 	sys_table_list='authority product_nature role role_authority role_supplier role_user user'
# 	port='3314'
# elif [[ $database_name = 'xyqb_btzt' ]]; then
# 	# 白条直投
# 	sys_table_list='zt_channel zt_fund zt_fund_channel zt_fund_credit zt_fund_term'
# 	port='9002'
# elif [[ $database_name = 'user_credit' ]]; then
# 	# user_credit
# 	sys_table_list=''
# 	port='9001'
# elif [[ $database_name = 'display' ]]; then
# 	# display
# 	sys_table_list='discovery_config'
# 	source_db_host='192.168.3.19'
# 	port='3307'
# elif [[ $database_name = 'clotho' ]]; then
# 	sys_table_list=''
# 	source_db_host='192.168.3.19'
# 	port='3308'
# elif [[ $database_name = 'traffic_distribution' ]]; then
# 	sys_table_list='merchant merchant_carousel merchant_credit merchant_detail merchant_section merchant_sort ppdai_exclude_channel t_module t_module_role t_role t_user t_user_role traffic_channel_banner_ids traffic_channel_ids traffic_channel_sort traffic_infos'
# 	source_db_host='192.168.3.36'
# 	port='3313'
# elif [[ $database_name = 'notify' ]]; then
# 	sys_table_list='merchant merchant_config'
# 	source_db_host='192.168.4.6'
# 	port='7306'
# elif [[ $database_name = 'mall_settlement' ]]; then
# 	sys_table_list='role role_user user'
# 	source_db_host='192.168.4.253'
# 	port='3315'
# elif [[ $database_name = 'score' ]]; then
# 	sys_table_list=''
# 	source_db_host='192.168.4.249'
# 	port='3322'
# elif [[ $database_name = 'reconciliation' ]]; then
# 	sys_table_list=''
# 	source_db_host='192.168.3.19'
# 	port='3308'
# elif [[ $database_name = 'account_center' ]]; then
# 	sys_table_list=''
# 	source_db_host='192.168.4.116'
# 	port='6001'
# elif [[ $database_name = 'urge_dispatcher' ]]; then
# 	sys_table_list=''
# 	source_db_host='192.168.4.116'
# 	port='9901'
# fi

sys_table_list=`curl -s http://192.168.4.3:10088/config_server/get_db_config/$database_name/sys_table_list`
source_db_host=`curl -s http://192.168.4.3:10088/config_server/get_db_config/$database_name/host`
port=`curl -s http://192.168.4.3:10088/config_server/get_db_config/$database_name/port`

echo "source_port is $port"
echo "copy ${base_config_file} to ${config_file_name}"
cp -f $base_config_file $config_file_name

echo "sed -i s/source_db_host/${source_db_host}/g $config_file_name" 
sed -i "s/source_db_host/${source_db_host}/g" $config_file_name
sed -i "s/source_db_user/${source_db_user}/g" $config_file_name
sed -i "s/source_db_pwd/${source_db_pwd}/g" $config_file_name
sed -i "s/target_db_host/${target_db_host}/g" $config_file_name
sed -i "s/target_db_user/${target_db_user}/g" $config_file_name
sed -i "s/target_db_pwd/${target_db_pwd}/g" $config_file_name
sed -i "s/target_db_port/${target_db_port}/g" $config_file_name
sed -i "s/sync_db_name/${sync_db_name}/g" $config_file_name
sed -i "s/source_port/${port}/g" $config_file_name

#同步基础数据
if [ $use_cache = true ];then
	echo "\cp -rf $cache_dir/$database_name/. $db_backup_sub_folders/"
	\cp -rf $cache_dir/$database_name/. $db_backup_sub_folders/
else
	if [ $database_name = 'quartz' ];then
		for table_name in $sys_table_list
		do
			file_name=$db_backup_sub_folders/$table_name.sql
			echo "mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --tables $table_name --where \"1=1 order by id desc limit 1000\"> $file_name"
			mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --skip-lock-tables --tables $table_name --where "1=1 order by id desc limit 10000" --skip-add-locks --no-create-info --complete-insert > $file_name
		done
	else
		for table_name in $sys_table_list
		do
			file_name=$db_backup_sub_folders/$table_name.sql
			echo "mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --tables $table_name > $file_name"
			mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --skip-lock-tables --tables $table_name --skip-add-locks --no-create-info --complete-insert > $file_name
		done
	fi
fi
