#!/bin/bash
#auto Backup any one or all db
#by Zhaoyanfeng
#2017-8-15 11:17:54
#defined
All_db= ' mysql-uroot-p123456-e "show databases;" | Grep-v Database | Xargs-n 1 '
Mysql_user=root
Mysql_passwd= "123456"
Mysql_backup_dir=/home/zhaoyanfeng/backup_mysql
If [-z]; Then
Echo-e "\033[31musage:/bin/bash if_backup_mysql.sh [dbname | All]\033[0m "
Echo-e "\033[31myou can use/bin/bash if_backup_mysql.sh help to get help\033[0m"
Exit 5
Fi
If [$#-gt 1]; Then
Echo-e "\033[31musage:/bin/bash if_backup_mysql.sh [dbname | All]\033[0m "
Echo-e "\033[31myou can use/bin/bash if_backup_mysql.sh help to get help\033[0m"
Exit 5
Fi
if [= = "Help"]; Then
Echo-e "\033[32m------------------------------\033[0m"
Echo-e "\033[31mselect one dbname or all to backup as blow.\033[0m"
echo $ALL _db | Xargs-n 1
Echo-e "\033[32m------------------------------\033[0m"
echo All
Echo-e "\033[32m------------------------------\033[0m"
Echo-e "\033[31musage:/bin/bash if_backup_mysql.sh [dbname | All]\033[0m "
Elif mysql-u$mysql_user-p$mysql_passwd-e ' show databases; ' | Grep-v Database | grep $ &>/dev/null; Then
If [-F $MYSQL _backup_dir/$1_ ' Date +%f '. sql]; Then
echo "has exist backup File,so backup old file to Random.bak" && mv $MYSQL _backup_dir/$1_ ' date +%f '. SQL $MYSQL _b Ackup_dir/$1_ ' Date +%f ' _$random.sql.bak && mysqldump-u$mysql_user-p$mysql_passwd $ > $MYSQL _backup_dir/$ 1_ ' Date +%f '. SQL && ECHO-E "\033[33mcongratulations!!! \033[0m "
elif [$UID-ne 0]; Then
echo "Bakcup MySQL databases!"
Exit 5
Else
MYSQLDUMP-U$MYSQL_USER-P$MYSQL_PASSWD $ > $MYSQL _backup_dir/$1_ ' Date +%f '. SQL && echo-e "\033[ 33mcongratulations!!! \033[0m "
Fi
elif [= = = "All"]; Then
If [$UID-ne 0]; Then
echo "Bakcup MySQL databases!"
Exit 5
Elif [-F $MYSQL _backup_dir/all_ ' Date +%f '. sql]; Then
echo "has exist backup File,so backup old file to Random.bak" && mv $MYSQL _backup_dir/$1_ ' date +%f '. SQL $MYSQL _b Ackup_dir/$1_ ' Date +%f ' _$random.sql.bak && mysqldump-u$mysql_user-p$mysql_passwd--all-databases > $MYSQL _backup_dir/all_ ' Date +%f '. SQL && ECHO-E "\033[33mcongratulations!!! \033[0m "Else
MYSQLDUMP-U$MYSQL_USER-P$MYSQL_PASSWD--all-databases > $MYSQL _backup_dir/all_ ' Date +%f '. sql && echo-e "\ 033[33mcongratulations!!! \033[0m "
Fi
Else
echo "Please check if your input was correct or not."
Fi
This article is from the "Zhaoyfcomeon-linux Enthusiasts" blog, please be sure to keep this source http://zhaoyfcomeon.blog.51cto.com/8429349/1956930
Automatically back up MySQL database scripts