#!/bin/bash
#2020年04月27日15:56:21
#auto backup mysql db
#by author www.cnbugs.com
########################
SQL_DB="$*"
SQL_USR="backup"
SQL_PWD="123456"
SQL_CMD="/usr/bin/mysqldump"
SQL_DIR="/data/backup/`date +%F`"
if [ $# -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 jfedu001 jfedu002|jfedu003|help}\033[0m"
exit
fi
if [ ! -d $SQL_DIR ];then
mkdir -p $SQL_DIR
echo -e "\033[32mThe dir $SQL_DIR create succeded.\033[0m"
fi
if [ $1 == "all" ];then
$SQL_CMD -u$SQL_USR -p$SQL_PWD --all-databases > $SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mThe mysql ${SQL_DB} database backup succeded.\033[0m"
echo -e "\033[32mls -l $SQL_DIR\033[0m"
ls -l $SQL_DIR
exit
else
echo -e "\033[31m---------------\033[0m"
echo -e "\033[31mThe mysql ${SQL_DB} database backup failed,please check.\033[0m"
rm -rf $SQL_DIR/${SQL_DB}.sql
echo -e "\033[32mls -l $SQL_DIR\033[0m"
ls -l $SQL_DIR
exit
fi
fi
for i in $SQL_DB
do
echo $i
sleep 2
$SQL_CMD -u$SQL_USR -p$SQL_PWD $i > $SQL_DIR/${i}.sql
if [ $? -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mThe mysql ${i} database backup succeded.\033[0m"
echo -e "\033[32mls -l $SQL_DIR\033[0m"
ls -l $SQL_DIR
else
echo -e "\033[31m---------------\033[0m"
echo -e "\033[31mThe mysql ${i} database backup failed,please check.\033[0m"
rm -rf $SQL_DIR/${i}.sql
echo -e "\033[32mls -l $SQL_DIR\033[0m"
ls -l $SQL_DIR
fi
done