#! /bin/bash

logintool=/home/yx/server/mysql/mysql/bin/mysql
dumptool=/home/yx/server/mysql/mysql/bin/mysqldump
username=root
passwd=`cat /home/yx/stt/shellmysql/dumpsql/.passwd`
socketfile=/var/run/mysqld/mysqld.sock
storepath=/home/yx/backup/hnf_file
now=`date +%Y%m%d%H%M%S`

password=${passwd}
stty erase '^H'
read -p "请输入库名:" dbname

        if [[ -z ${dbname} ]];then

                echo -e "你输出的库名为空,请输入库名!\n"
                exit 100
        else

                dbcheck=`${logintool} -u ${username} -p${password} -e "show databases;" 2>/dev/null | grep -w "${dbname}"`

                if [[ -z ${dbcheck} ]];then

                        echo -e "你要导出的库不存在!请重新输入正确的dbname!\n"
                        exit 100

                fi
        fi

${dumptool} -u ${username} -p${password} -S ${socketfile} --set-gtid-purged=off ${dbname}  > ${storepath}/${dbname}-${now}.sql

echo -e "${dbname}库 已经备份完毕!\n"
echo "保存路径为:${storepath}/${dbname}-${now}.sql"``

然后执行这个脚本,会提示你输入想要备份的数据库
利用shell脚本去备份所指定的数据库
这样就比用正常的命令备份方便了很多,也不用每次输入密码和端口号了