#!/bin/bash echo ' =============================== - 生產操做謹慎執行 - - - 提早預置SQL文件 - - =============================== ' read -p '請輸入數據庫用戶:' mysql_user read -p '請輸入數據庫密碼:' mysql_pass read -p '請輸入數據庫庫名:' db_name read -p '請輸入表名:' table_name read -p '請輸入sql文件:' sql_name sql_path="/data1/sql" dump_path="/data1/mysql_dump" mysql_host="xxxx" mysql_port=3306 local_time=$(date +%Y%m%d-%k%M%S) /usr/bin/mysqldump -u${mysql_user} -p${mysql_pass} -h${mysql_host} -P${mysql_port} ${db_name} ${table_name} > ${dump_path}/${table_name}_${ local_time} if [ $? -ne 0 ];then echo "${db_name}.${table_name}備份失敗" exit 255 else back_info=`/usr/bin/ls -shl ${dump_path}/${table_name}_${local_time}|awk '{print $1}'` echo "備份成功,備份文件爲:${table_name}_${local_time} 大小爲:${back_info}" echo "請等待,正在導入SQL文件..." /usr/bin/mysql -u${mysql_user} -p${mysql_pass} -h${mysql_host} -P${mysql_port} ${db_name} < ${sql_path}/${sql_name} if [ $? -ne 0 ];then echo "${sql_name} 執行導入失敗,如有報錯請判斷是否須要恢復" echo "${dump_path}/${table_name}_${local_time} 爲本次腳本的備份文件" else echo "${sql_name} 導入成功,請聯繫測試人員驗證" fi fi