#! /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"``
而後執行這個腳本,會提示你輸入想要備份的數據庫
這樣就比用正常的命令備份方便了不少,也不用每次輸入密碼和端口號了mysql