Automatically back up MySQL database scripts

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.