#!/bin/bash

###############################################################################
# shell 脚本中执行 sql 语句
###############################################################################

host='192.168.0.180'
username='test'
password='Test@123456'

db1='tnb_account_tdb'
db2='tnb_content_tdb'
db3='reghao_bnt_tdb'
db4='reghao_oss_tdb'

sql1="CREATE DATABASE IF NOT EXISTS ${db1} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"
sql2="CREATE DATABASE IF NOT EXISTS ${db2} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"
sql3="CREATE DATABASE IF NOT EXISTS ${db3} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"
sql4="CREATE DATABASE IF NOT EXISTS ${db4} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"

mysql -h${host} -u${username} -p${password} << EOF
${sql1}
${sql2}
${sql3}
${sql4}
EOF

base_dir=`pwd`
cd ${base_dir}/schema

db11='tnb_account_tdb'
db21='tnb_content_tdb'
db31='reghao_bnt_tdb'
db41='reghao_oss_tdb'

# 还原整个数据库结构(不包含数据)
mysql -h${host} -u${username} -p${password} ${db11} < ${db1}.sql
mysql -h${host} -u${username} -p${password} ${db21} < ${db2}.sql
mysql -h${host} -u${username} -p${password} ${db31} < ${db3}.sql
mysql -h${host} -u${username} -p${password} ${db41} < ${db4}.sql

# 还原表数据到数据库
mysql -h${host} -u${username} -p${password} ${db11} < msg_email_account.sql
mysql -h${host} -u${username} -p${password} ${db11} < user_registry.sql
mysql -h${host} -u${username} -p${password} ${db11} < user_vip_plan.sql

mysql -h${host} -u${username} -p${password} ${db21} < file_store_config.sql
mysql -h${host} -u${username} -p${password} ${db21} < vod_video_category.sql

mysql -h${host} -u${username} -p${password} ${db31} < oss_store_node.sql
mysql -h${host} -u${username} -p${password} ${db31} < oss_store_volumes.sql
mysql -h${host} -u${username} -p${password} ${db31} < oss_upload_channel.sql
mysql -h${host} -u${username} -p${password} ${db31} < oss_user_key.sql
mysql -h${host} -u${username} -p${password} ${db31} < oss_user_node.sql
mysql -h${host} -u${username} -p${password} ${db31} < sys_menu.sql
mysql -h${host} -u${username} -p${password} ${db31} < sys_role.sql
mysql -h${host} -u${username} -p${password} ${db31} < sys_role_menu.sql
mysql -h${host} -u${username} -p${password} ${db31} < sys_user.sql

mysql -h${host} -u${username} -p${password} ${db41} < file_meta.sql
