基于dos与shell 实现数据库批量跑库脚本(dm/mysql)

  1. bat

    mysql跑库

    @echo off
    chcp 65001
    %~d0
    cd %~dp0
    set basepath=%~dp0
    set | findstr mysql
    if "%ERRORLEVEL%" == "0" ( goto paraminit) ELSE ECHO THERE IS NOT MYSQL ENV !
    goto end
    
    :paraminit
    set /p mysqlhost="ip 地址,默认值 ==> (127.0.0.1) : "
    set /p mysqlport="请输入mysql服务端口,默认值 ==> (3306) :"
    set /p rootpwd="请输入mysql超管密码,默认值 ==> (root) :"
    set /p kupwd="请输入oneaccess各个库拥有者对应的密码,默认值 ==> (Bamboocloud@1234) : "
    set /p suffix="请输入oneaccess对应的库及用户的后缀,默认为空串: "
    
    if "%mysqlhost%"=="" set mysqlhost=127.0.0.1
    if "%mysqlport%"=="" set mysqlport=3306
    if "%rootpwd%"=="" set rootpwd=root
    if "%kupwd%"=="" set kupwd=Bamboocloud@1234
    if "%suffix%"=="" set suffix=
    set sql_dir=sql_cmd_execute
    rmdir /s/q %basepath%\%sql_dir%
    mkdir -p %basepath%\%sql_dir% 
    xcopy /y/e/i %basepath%\bam %basepath%\%sql_dir% 
    xcopy /y/e/i %basepath%\bim %basepath%\%sql_dir% 
    xcopy /y/e/i %basepath%\epass %basepath%\%sql_dir% 
    xcopy /y/e/i %basepath%\apphub %basepath%\%sql_dir% 
    goto replacepwdfohcs
    
    
    :replacepwdfohcs
    @setlocal enabledelayedexpansion
    for /f "delims=" %%i in (%basepath%\%sql_dir%\bam_mysql_db_create.sql) do (
    set content=%%i
    set content=!content:bam_password=abcdefghijklmnopqrst!
    set content=!content:bam=bam%suffix%!
    set content=!content:abcdefghijklmnopqrst=%kupwd%!
    echo !content!>>$1)
    move $1 %basepath%\%sql_dir%\bam_mysql_db_create.sql || EXIT \B
    
    for /f "delims=" %%i in (%basepath%\%sql_dir%\bim_mysql_db_create.sql) do (
    set content=%%i
    set content=!content:bim_password=abcdefghijklmnopqrst!
    set content=!content:bim=bim%suffix%!
    set content=!content:abcdefghijklmnopqrst=%kupwd%!
    echo !content!>>$2)
    move $2 %basepath%\%sql_dir%\bim_mysql_db_create.sql || EXIT \B
    
    for /f "delims=" %%i in (%basepath%\%sql_dir%\apphub_mysql_db_create.sql) do (
    set content=%%i
    set content=!content:apphub_password=abcdefghijklmnopqrst!
    set content=!content:apphub=apphub%suffix%!
    set content=!content:abcdefghijklmnopqrst=%kupwd%!
    echo !content!>>$3)
    move $3 %basepath%\%sql_dir%\apphub_mysql_db_create.sql || EXIT \B
    
    for /f "delims=" %%i in (%basepath%\%sql_dir%\epass_mysql_db_create.sql) do (
    set content=%%i
    set content=!content:epass_password=abcdefghijklmnopqrst!
    set content=!content:epass=epass%suffix%!
    set content=!content:abcdefghijklmnopqrst=%kupwd%!
    echo !content!>>$4)
    move $4 %basepath%\%sql_dir%\epass_mysql_db_create.sql || EXIT \B
    goto mysqlsqlexec
    
    :mysqlsqlexec
    set _timeTemp=0
    call:GetTimeSeconds %time%
    set time1=%_timeTemp%
    rem choice  等待5秒 再计算另外一个时间点
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "mysql" <  %basepath%\%sql_dir%\bam_mysql_db_create.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "mysql" <  %basepath%\%sql_dir%\bim_mysql_db_create.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "mysql" <  %basepath%\%sql_dir%\apphub_mysql_db_create.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "mysql" <  %basepath%\%sql_dir%\epass_mysql_db_create.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "bam%suffix%" <  %basepath%\%sql_dir%\bam\bam_mysql_table_and_data.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "bim%suffix%" <  %basepath%\%sql_dir%\bim_mysql_table_and_data.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "apphub%suffix%" <  %basepath%\%sql_dir%\apphub_mysql_table_and_data.sql -vvv
     mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -D "epass%suffix%" <  %basepath%\%sql_dir%\epass_mysql_table_and_data.sql -vvv
    @echo off
    @REM "
    goto printexetimes
    
    :printexetimes
    call:GetTimeSeconds %time%
    set time2=%_timeTemp%
    set /a interTime=%time2% - %time1%
    echo 
    echo 已全量跑库完成 hcs_mysql_bat 耗时: %interTime% 秒 !!
    goto end
    
    
    
    
    :GetTimeSeconds
    set tt=%1
    rem windows下%time%时间的格式一般为hour:minute:second.number 下面两行只是去掉冒号和顿号变成hour minute second number ,此处只是取hour minute second
    set tt=%tt:.= %
    set tt=%tt::= %
    set index=1
    for %%a in (%tt%) do (
     if !index! EQU 1 (
         set hh=%%a
     )^
     else if  !index! EQU 2 (
         set mm=%%a
     
     )^
     else if  !index! EQU 3 (
         set ss=%%a
     )
    set /a index=index+1
    )
    set /a _timeTemp=(%hh%*60+%mm%)*60+%ss%
    
    goto:end
    
    
    :end

    mysql删库

    @echo off
    chcp 65001
    set | findstr mysql
    if "%ERRORLEVEL%" == "0" (goto paraminit)
    echo there is no mysql env
    goto end
    
    
    :paraminit
    set /p mysqlhost="请输入ip:"
    set /p mysqlport=请输入端口
    set /p rootpwd=请输入密码
    set /p suffix=请输入后缀
    
    if "%mysqlhost%"=="" set mysqlhost=127.0.0.1
    if "%mysqlport%"=="" set mysqlport=3306
    if "%rootpwd%"=="" set rootpwd=root
    if "%suffix%"=="" set suffix=136
    goto mysqldelexec
    
    :mysqldelexec
    mysql -h %mysqlhost% -uroot -p%rootpwd% -P %mysqlport% -e "use mysql;DROP USER IF EXISTS 'bam%suffix%'@'%%';DROP USER IF EXISTS 'bam%suffix%'@'localhost';DROP DATABASE IF EXISTS `bam%suffix%`;DROP USER IF EXISTS 'bim%suffix%'@'%%';DROP USER IF EXISTS 'bim%suffix%'@'localhost';DROP DATABASE IF EXISTS `bim%suffix%`;DROP USER IF EXISTS 'apphub%suffix%'@'%%';DROP USER IF EXISTS 'apphub%suffix%'@'localhost';DROP DATABASE IF EXISTS `apphub%suffix%`;DROP USER IF EXISTS 'epass%suffix%'@'%%';DROP USER IF EXISTS 'epass%suffix%'@'localhost';DROP DATABASE IF EXISTS `epass%suffix%`;" -vvv
    goto end
    
    :end
    pause
  2. bash

    mysql跑库

    #!/bin/bash
    set -oe
    #-u为数据库用户名 -p为密码如果数据库密码不是root记得修改这里
    read -p "ip 地址,默认值 ==> (127.0.0.1) : " mysqlhost
    read -p "请输入mysql服务端口,默认值 ==> (3306) : " mysqlport
    read -p "请输入mysql超管密码,默认值 ==> (root) : " rootpwd
    read -p "请输入oneaccess各个库拥有者对应的密码,默认值 ==> (Bamboocloud@1234) : " kupwd
    read -p $'\033[40;32m 请输入oneaccess对应的库及用户的后缀,默认为空串: \033[0m' suffix
    starttime=`date +'%Y-%m-%d %H:%M:%S'`
    
    suffix=${suffix:-""}
    rootpwd=${rootpwd:-"root"}
    mysqlport=${mysqlport:-"3306"}
    mysqlhost=${mysqlhost:-"127.0.0.1"}
    kupwd=${kupwd:-"Bamboocloud@1234"}
    basepath=$(
     cd $(dirname $0)
     pwd
    )
    rm -rf ${basepath}/sql_execute
    mkdir -p ${basepath}/sql_execute
    \cp -r ${basepath}/{bam,bim,apphub,epass} ${basepath}/sql_execute
    
    sed -i "s/bam_password/kupwd/g" ${basepath}/sql_execute/bam/bam_mysql_db_create.sql
    sed -i "s/bam/bam${suffix}/g" ${basepath}/sql_execute/bam/bam_mysql_db_create.sql
    sed -i "s/kupwd/${kupwd}/g" ${basepath}/sql_execute/bam/bam_mysql_db_create.sql
    
    sed -i "s/bim_password/kupwd/g" ${basepath}/sql_execute/bim/bim_mysql_db_create.sql
    sed -i "s/bim/bim${suffix}/g" ${basepath}/sql_execute/bim/bim_mysql_db_create.sql
    sed -i "s/kupwd/${kupwd}/g" ${basepath}/sql_execute/bim/bim_mysql_db_create.sql
    
    sed -i "s/epass_password/kupwd/g" ${basepath}/sql_execute/epass/epass_mysql_db_create.sql
    sed -i "s/epass/epass${suffix}/g" ${basepath}/sql_execute/epass/epass_mysql_db_create.sql
    sed -i "s/kupwd/${kupwd}/g" ${basepath}/sql_execute/epass/epass_mysql_db_create.sql
    
    sed -i "s/apphub_password/kupwd/g" ${basepath}/sql_execute/apphub/apphub_mysql_db_create.sql
    sed -i "s/apphub/apphub${suffix}/g" ${basepath}/sql_execute/apphub/apphub_mysql_db_create.sql
    sed -i "s/kupwd/${kupwd}/g" ${basepath}/sql_execute/apphub/apphub_mysql_db_create.sql
    
    mysql -h$mysqlhost -uroot -p$rootpwd -P$mysqlport -e \
    "use mysql;
    source ${basepath}/sql_execute/bam/bam_mysql_db_create.sql;  
    source ${basepath}/sql_execute/bim/bim_mysql_db_create.sql; 
    source ${basepath}/sql_execute/apphub/apphub_mysql_db_create.sql;
    source ${basepath}/sql_execute/epass/epass_mysql_db_create.sql; 
    use bam${suffix}; 
    source ${basepath}/sql_execute/bam/bam_mysql_table_and_data.sql; 
    use bim${suffix}; 
    source ${basepath}/sql_execute/bim/bim_mysql_table_and_data.sql; 
    use apphub${suffix}; 
    source ${basepath}/sql_execute/apphub/apphub_mysql_table_and_data.sql; 
    use epass${suffix}; 
    source ${basepath}/sql_execute/epass/epass_mysql_table_and_data.sql;" -vvv
    
    endtime=`date +'%Y-%m-%d %H:%M:%S'`
    start_seconds=$(date --date="$starttime" +%s);
    end_seconds=$(date --date="$endtime" +%s);
    echo
    echo "运行 hcs-mysql 跑库脚本......"
    echo
    echo "====================hcs============================"
    echo "开始时间:" $starttime
    echo "结束时间:" $endtime
    echo "本次跑库耗时: "$((end_seconds-start_seconds))"s"
    echo "====================hcs============================"
    

mysql删库

#!/bin/bash
#-u为数据库用户名 -p为密码如果数据库密码不是root记得修改这里
read -p "ip 地址,默认值 ==> (127.0.0.1) : " mysqlhost
read -p "请输入mysql服务端口,默认值 ==> (3306) : " mysqlport
read -p "请输入mysql超管密码,默认值 ==> (root) : " rootpwd
read -p $'\033[40;32m 请输入需要删除oneaccess对应的库及用户的后缀,默认为空串: \033[0m' suffix

suffix=${suffix:-""}
rootpwd=${rootpwd:-"root"}
mysqlport=${mysqlport:-"3306"}
mysqlhost=${mysqlhost:-"127.0.0.1"}
mysql -h$mysqlhost -uroot -p$rootpwd -P$mysqlport -e \
    "use mysql;
DROP USER IF EXISTS 'bam${suffix}'@'%';
DROP USER IF EXISTS 'bam${suffix}'@'localhost';
DROP DATABASE IF EXISTS \`bam${suffix}\`;
DROP USER IF EXISTS 'bim${suffix}'@'%';
DROP USER IF EXISTS 'bim${suffix}'@'localhost';
DROP DATABASE IF EXISTS \`bim${suffix}\`;
DROP USER IF EXISTS 'apphub${suffix}'@'%';
DROP USER IF EXISTS 'apphub${suffix}'@'localhost';
DROP DATABASE IF EXISTS \`apphub${suffix}\`;
DROP USER IF EXISTS 'epass${suffix}'@'%';
DROP USER IF EXISTS 'epass${suffix}'@'localhost';
DROP DATABASE IF EXISTS \`epass${suffix}\`;" -vvv
  1. dm/shell

跑库

#!/bin/bash
#==============================================================#
# 脚本名     :   hcs_dm_initall.sql
# 创建时间   :   2022-12-28 10:02:09
# 更新时间   :   2022-12-29 10:30:18
# 描述      :   使用脚本,根据输入的后缀不同 一键跑完DM的数据库脚本
# 路径      :   需与oneaccess dm版本的sql目录在同一路径下
# 版本      :   1.0.0
# 作者      :   dongjelababy([email protected])
# Copyright (C) 2021-2022 HCS
#==============================================================#

#==============================================================#
#                         HCS_DM                              #
#==============================================================#
# 获取达梦的安装目录
dmdispwd=$(find / -path *bin/disql | head -1)
read -p "请输入dm服务端口,默认值 ==> (5236) : " dmport
read -p "请输入dm超管密码,默认值 ==> (SYSDBA) : " sysdbapwd
read -p "请输入oneaccess各个库拥有者对应的密码,默认值 ==> (Bamboocloud@1234) : " kupwd
read -p "请输入oneaccess对应的库及用户的后缀,默认为空串: " suffix
suffix=${suffix:-""}
sysdbapwd=${sysdbapwd:-"SYSDBA"}
dmport=${dmport:-"5236"}
kupwd=${kupwd:-"Bamboocloud@1234"}

if [ "$suffix" = "" ]; then
read -p "后缀为空,请确认(yes/no):yes继续,no退出" input
input=$(echo "$input" | tr "[A-Z]" "[a-z]")
if [ "$input" = "yes" ]; then
   echo "yes go"
else 
echo "exit zero"
exit
fi
else
   echo "go"
fi
starttime=`date +'%Y-%m-%d %H:%M:%S'`

dy="'"
sy='"'

dir=hcs_dm_execute
rm -rf ./${dir}
mkdir ./${dir}
# bam 
cat bam/bam_dm_db_create.sql >./${dir}/bam_dm_db_create.sql
cat >>./${dir}/bam_dm_db_create.sql<./${dir}/bam_dm_20.0.2_all.sql
cat >>./${dir}/bam_dm_20.0.2_all.sql<./${dir}/bim_dm_db_create.sql

cat >>./${dir}/bim_dm_db_create.sql<./${dir}/bim_dm_table_create.sql
cat >>./${dir}/bim_dm_table_create.sql<./${dir}/business_dm_table_create.sql
cat >>./${dir}/business_dm_table_create.sql<./${dir}/bim_dm_trigger.sql
cat >>./${dir}/bim_dm_trigger.sql<./${dir}/bim_dm_data_init.sql
cat >>./${dir}/bim_dm_data_init.sql<./${dir}/business_dm_data_init.sql
cat >>./${dir}/business_dm_data_init.sql<./${dir}/epass_dm_db_create.sql
cat >>./${dir}/epass_dm_db_create.sql<./${dir}/epass_dm_20.0.2_all.sql
cat >>./${dir}/epass_dm_20.0.2_all.sql<./${dir}/apphub_dm_db_create.sql
cat >>./${dir}/apphub_dm_db_create.sql<./${dir}/apphub_dm_table_create.sql
cat >>./${dir}/apphub_dm_table_create.sql<./${dir}/apphub_dm_data_sample.sql
cat >>./${dir}/apphub_dm_data_sample.sql<

删库

#!/bin/bash
#==============================================================#
# 脚本名     :   hcs_dm_destroyall.sh
# 创建时间   :   2022-12-28 10:02:09
# 更新时间   :   2022-12-29 10:30:18
# 描述      :   使用脚本,根据输入的后缀不同 一键删除dm数据库的表空间和表
# 路径      :   需与oneaccess dm版本的sql目录在同一路径下
# 版本      :   1.0.0
# 作者      :   dongjelababy([email protected])
# Copyright (C) 2021-2022 HCS
#==============================================================#

#==============================================================#
#                         HCS_DM                              #
#==============================================================#
# 获取达梦的安装目录
dmdispwd=$(find / -path *bin/disql | head -1)
read -p "请输入dm服务端口,默认值 ==> (5236) : " dmport
read -p "请输入dm超管密码,默认值 ==> (SYSDBA) : " sysdbapwd
read -p "请输入需要删除的oneaccess库及用户的后缀,默认为空串: " suffix
suffix=${suffix:-""}
sysdbapwd=${sysdbapwd:-"SYSDBA"}
dmport=${dmport:-"5236"}

if [ "$suffix" = "" ]; then
    read -p "后缀为空,请确认是否删除(yes/no):" input
    input=$(echo "$input" | tr "[A-Z]" "[a-z]")
    if [ "$input" = "yes" ]; then
        echo "yes go"
    else
        exit
    fi
else
    echo go!
fi

$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop user bam${suffix} cascade;"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop TABLESPACE bam${suffix};"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop user bim${suffix} cascade;"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop TABLESPACE bim${suffix};"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop user epass${suffix} cascade;"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop TABLESPACE epass${suffix};"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop user apphub${suffix} cascade;"
$dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e "drop TABLESPACE apphub${suffix};"

# $dmdispwd SYSDBA/${sysdbapwd}:${dmport} -e <

你可能感兴趣的:(脚本dosshell自动化)