#!/bin/bash #auto backup mysql DB #by author ale #2019年4月29日12:17:23 ################ #Define Variable BAK_DIR=/data/backup/$(date +%F) SQL_USR="backup" SQL_PWD="123456" SQL_DB="$*" SQL_HOST="localhost" SQL_CMD="/usr/bin/mysqldump" if [ $# -eq 0 ];then echo -e "\033[32m----------------\033[0m" echo -e "\033[32mUsage:{/bin/bash $0 discuz|zabbix|all|help}\033[0m" exit 1 fi if [ "$SQL_DB" == "help" ];then echo -e "\033[32m----------------\033[0m" echo -e "\033[32mUsage:{/bin/bash $0 discuz|zabbix|all|help}\033[0m" exit 1 fi if [ ! -d $BAK_DIR ];then mkdir -p $BAK_DIR fi for SQL_DB in $SQL_DB do mysql -h$SQL_HOST -u$SQL_USR -p$SQL_PWD -e "show databases"|grep "$SQL_DB" if [ $? -eq 0 ];then $SQL_CMD -h$SQL_HOST -u$SQL_USR -p$SQL_PWD $SQL_DB>$BAK_DIR/${SQL_DB}.sql if [ $? -eq 0 ];then echo -e "\033[32mThe Mysql DB ${SQL_DB} Backup success.\033[0m" echo -e "\033[32mls -l $BAK_DIR/${SQL_DB}.sql\033[0m" ls -l $BAK_DIR/${SQL_DB}.sql else echo -e "\033[32m--------------------\033[0m" echo -e "\033[32mThis Mysql DB ${SQL_DB} Backup Failed.Please check\033[0m" exit 1 fi else if [ $SQL_DB == "all" ];then $SQL_CMD -h$SQL_HOST -u$SQL_USR -p$SQL_PWD --all-databases>$BAK_DIR/${SQL_DB}.sql if [ $? -eq 0 ];then echo -e "\033[32mThe Mysql DB ${SQL_DB} Backup success.\033[0m" echo -e "\033[32mls -l $BAK_DIR/${SQL_DB}.sql\033[0m" ls -l $BAK_DIR/${SQL_DB}.sql exit 0 else echo -e "\033[32m--------------------\033[0m" echo -e "\033[32mThis Mysql DB ${SQL_DB} Backup Failed.Please check\033[0m" exit 1 fi fi echo -e "\033[32m-----------------\033[0m" echo -e "\033[32mThe MySQL DB ${SQL_DB} Unknown,Backup Failed Please check.\033[0m" exit 1 fi done