#!/bin/bash #ocpyang@126.com ########脚本说明 #1.默认读取当前目录下database.txt文件,备份部分数据库 #2.如果database.txt为空,则执行全备或备份指定的个别数据库 #set parameter mydumper=/usr/local/bin/mydumper #根据自己的实际情况设置 dir_backup=/backup #根据自己的实际情况设置 mysql_host=localhost #根据自己的实际情况设置 mysql_port=33306 #根据自己的实际情况设置 mysql_user=root #根据自己的实际情况设置 mysql_pass=123456 #根据自己的实际情况设置 now_date=`date +%Y%m%d%H%M` if [ ! -d $dir_backup ]; then echo -e "\e[1;31m 保存备份的主目录:$dir_backup不存在,将自动新建. \e[0m" mkdir -p ${dir_backup} fi export black='\033[0m' export boldblack='\033[1;0m' export red='\033[31m' export boldred='\033[1;31m' export green='\033[32m' export boldgreen='\033[1;32m' export yellow='\033[33m' export boldyellow='\033[1;33m' export blue='\033[34m' export boldblue='\033[1;34m' export magenta='\033[35m' export boldmagenta='\033[1;35m' export cyan='\033[36m' export boldcyan='\033[1;36m' export white='\033[37m' export boldwhite='\033[1;37m' cecho () ## -- Function to easliy print colored text -- ## # Color-echo. # 参数 $1 = message # 参数 $2 = color { local default_msg="No message passed." message=${1:-$default_msg} # 如果$1没有输入则为默认值default_msg. color=${2:-black} # 如果$1没有输入则为默认值black. case $color in black) printf "$black" ;; boldblack) printf "$boldblack" ;; red) printf "$red" ;; boldred) printf "$boldred" ;; green) printf "$green" ;; boldgreen) printf "$boldgreen" ;; yellow) printf "$yellow" ;; boldyellow) printf "$boldyellow" ;; blue) printf "$blue" ;; boldblue) printf "$boldblue" ;; magenta) printf "$magenta" ;; boldmagenta) printf "$boldmagenta" ;; cyan) printf "$cyan" ;; boldcyan) printf "$boldcyan" ;; white) printf "$white" ;; boldwhite) printf "$boldwhite" ;; esac printf "%s\n" "$message" tput sgr0 # tput sgr0即恢复默认值 printf "$black" return } cechon () # Color-echo. # 参数1 $1 = message # 参数2 $2 = color { local default_msg="No message passed." # Doesn't really need to be a local variable. message=${1:-$default_msg} # 如果$1没有输入则为默认值default_msg. color=${2:-black} # 如果$1没有输入则为默认值black. case $color in black) printf "$black" ;; boldblack) printf "$boldblack" ;; red) printf "$red" ;; boldred) printf "$boldred" ;; green) printf "$green" ;; boldgreen) printf "$boldgreen" ;; yellow) printf "$yellow" ;; boldyellow) printf "$boldyellow" ;; blue) printf "$blue" ;; boldblue) printf "$boldblue" ;; magenta) printf "$magenta" ;; boldmagenta) printf "$boldmagenta" ;; cyan) printf "$cyan" ;; boldcyan) printf "$boldcyan" ;; white) printf "$white" ;; boldwhite) printf "$boldwhite" ;; esac printf "%s" "$message" tput sgr0 # tput sgr0即恢复默认值 printf "$black" return } judegedate_01="judegedate01.`date +%y%m%d%h%m%s`.txt" schema_judege01="select schema_name from information_schema.schemata ;" mysql -h${mysql_host} -P${mysql_port} -u${mysql_user} -p${mysql_pass} -e"${schema_judege01}" >${judegedate_01} echo -e "\e[1;31m The databases name in current instance is: \e[0m" awk 'NR==2,NR==0 { print $1}' ${judegedate_01} echo " " #Get the current script path and create a file named database.txt #in order to save the backup databases name. filepath=$(cd "$(dirname "$0")"; pwd) if [ ! -s "${filepath}/data