auto_mysqldump_data.sh
#!/bin/bash#2019年10月31日22:57:25#auto back mysql's databaseIPADDR="10.0.0.7"BAK_TIME=
if [ ! -d $SQL_DIR ]; then mkdir -p $SQL_DIRfi
for SQL_DB in $SQL_DBSdo$MYSQL_COM2 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS -e "show databases"|grep "$SQL_DB"if [ $? == 0 ];then$MYSQL_COM1 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS --database $SQL_DB >${SQL_DIR}${SQL_DB}-${BAK_TIME}.sqlif [ $? == 0 ];thenecho "this $SQL_DB was already backup .."continueelseecho -e "\033[32m@You input the database is not exist..Please resume !\033[0m"exitfielseif [ $SQL_DBS == all ];then$MYSQL_COM1 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS --all-databases >${SQL_DIR}all-${BAK_TIME}.sqlif [ $? == 0 ];thenecho "all databases was already backup .."exit elseecho -e "\033[32m@@You input the database is not exist..Please resume !\033[0m"exitfifi
fidone
date +%F-%T
MYSQL_USER="backup"MYSQL_PASS="123456"MYSQL_COM1="mysqldump"MYSQL_COM2="mysql"SQL_DIR="/data/mysqlback/"SQL_DBS="$*"if [ $# -eq 0 ]; thenecho -e "\033[33m-----------------------------------\033[0m"echo -e "\033[31m-->{Usage:(eg:$0 jf1 | jf1 jf2 .. | all )}\033[0m"echo -e "\033[33m-----------------------------------\033[0m"elif [ $1 == help ];thenecho -e "\033[33m-----------------------------------\033[0m"echo -e "\033[31m-->{Usage:(eg:$0 jf1 | jf1 jf2 .. | all )}\033[0m"echo -e "\033[33m-----------------------------------\033[0m"fi if [ ! -d $SQL_DIR ]; then mkdir -p $SQL_DIRfi
for SQL_DB in $SQL_DBSdo$MYSQL_COM2 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS -e "show databases"|grep "$SQL_DB"if [ $? == 0 ];then$MYSQL_COM1 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS --database $SQL_DB >${SQL_DIR}${SQL_DB}-${BAK_TIME}.sqlif [ $? == 0 ];thenecho "this $SQL_DB was already backup .."continueelseecho -e "\033[32m@You input the database is not exist..Please resume !\033[0m"exitfielseif [ $SQL_DBS == all ];then$MYSQL_COM1 -h$IPADDR -u$MYSQL_USER -p$MYSQL_PASS --all-databases >${SQL_DIR}all-${BAK_TIME}.sqlif [ $? == 0 ];thenecho "all databases was already backup .."exit elseecho -e "\033[32m@@You input the database is not exist..Please resume !\033[0m"exitfifi
fidone