Mysql database management script

Preface

test environment

System : cenros7
database version : Mysql5.7

Features

Insert picture description here

  1. List all database usage [data capacity of database records (MB) index capacity (MB)]
  2. List the usage of all tables [data capacity (MB), index capacity (MB)]
  3. Export all data of the library and table to the current script path
  4. Export all data of the specified table to the current script
  5. Custom create a new library, import and restore the backup data of the specified library or table
  6. Exit script

#!/bin/bash
#作用:Mysql数据库管理小脚本
#日期:2020-12-23

read -p"请输入数据库账号:  " USERNAME 
if [ $? -eq 0  ] ;then echo  "请输入密码 : "  ; else  echo "请输入规范账号!!" ; fi
read -p  ""  -s  PASSWORD
while :
do

[ $? -eq 0 ] &&  echo -e  "*******************************************************************\n
*|     1. 查 看 库 测 容 量      |     2.  查 看 表 的 容 量     |*\n
**************************************************************\n
*|     3. 导出数据库到当前目录   |     4. 导出数据表到当前目录   |*\n                                                                                                  
*******************************************************************\n
*|     5. 导入还原备份的库       |     6.     退  出  脚  本     |*\n" || echo "查看账号密码的输入i"
echo "输入操作项的编号:   "
read -p "" ku
if 
    [ $ku -eq 1 ]
then
mysql -u$USERNAME -p$PASSWORD <<EOF
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema;
EOF
[ $? -eq 0  ] && echo "    ↑↑↑↑↑↑ 库的使用情况如上所示 ↑↑↑↑↑↑     " || echo " 请检查数据库是否启动 "
elif
    [ $ku -eq 2  ]
then
mysql -u$USERNAME  -p$PASSWORD  <<EOF
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;
EOF
[ $? -eq 0   ] && echo "    ↑↑↑↑↑↑ 表的容量情况如上所示 ↑↑↑↑↑↑     " || echo " 请检查数据库是否启动 "
elif
    [ $ku -eq 3 ]
then 
    read -p "请输入要导出的库名:     " database
    read -p "请给备份库命名:     " databases
    mysqldump -u$USERNAME  -p$PASSWORD  $database  > $databases.sql
    [ $? -eq 0  ] && echo " $databases.sql  备份库已导入当前路径 " || echo "请检查要备份的库名输入是否正确"
elif
    [ $ku -eq 4  ]
then 
    read -p "请输入要导出的表所在的库:     "  database
    read -p "请输入要导出的表名:     "  table_name
   read -p "请给备份表命名:     " table_new
    mysqldump -u$USERNAME  -p$PASSWORD  $database  $table_name  > $table_new.sql
   [ $? -eq 0   ] && echo " $table_new.sql  备份表已导入当前路径 " || echo "请检查要备份的表名输入是否正确"
elif
    [ $ku -eq 5 ]
then
    read -p "请输入备份文件所在的位置:   "  location
    read -p "需要定义一个新库名来存放:   "  newdatabases
    mysql -u$USERNAME  -p$PASSWORD  <<EOF
    create database $newdatabases;
    exit
EOF
    mysql  -u$USERNAME  -p$PASSWORD $newdatabases < $location
    echo  "导入成功!!"
    mysql -u$USERNAME -p$PASSWORD <<EOF
    select
        table_schema as '数据库',
        sum(table_rows) as '记录数',
        sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
        sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'                                                                                                         
    
        from information_schema.tables
        group by table_schema;
EOF
elif
     [ $ku -eq 6  ]
then
    exit

else
    echo "请输入正确的序号"

fi
done

Guess you like

Origin blog.csdn.net/qq_26129413/article/details/111600439