Shell Copy线上DB数据到Beta库脚本
#!/bin/bash
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:: -- "$@"`
declare -a tables
declare -a conditions
eval set -- "$OPTION_TEMP"
while true ; do
case "$1" in
--s-host)
source_db_host=$2
shift 2;;
--s-db)
source_DB=$2
shift 2 ;;
--s-user)
source_user=$2
shift 2 ;;
--s-pass)
source_pass=$2
shift 2;;
--s-port)
source_port=$2
shift 2;;
--t-host)
shift 2;;
--t-db)
target_DB=$2
shift 2 ;;
--t-user)
target_user=$2
shift 2 ;;
--t-pass)
target_pass=$2
shift 2;;
--t-port)
target_port=$2
shift 2;;
--tables)
if [[ $2 == "" ]]
then
echo "缺少参数";
exit;
fi
OLD_IFS="$IFS"
IFS="|"
arr=($2)
IFS="$OLD_IFS"
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)
dir=$2;
shift 2;;
--help)
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
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 "$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