Using the shell for MySQL backup

Source: Internet
Author: User
Tags mysql backup

When executing the script, you need to keep up with the database name you need to back up, as follows

#!/bin/bash
#auto backup MySQL
#20180522 by Jzh
#################################

#Define PATH definition Variable

back_dir=/data/backup/mysql/' Date +%y-%m-%d '
Mysqldb=$1
Mysqlpw=backup
Mysqlusr=backup


#You must use root user run scripts must be run with root, $UID as system variable

if [[-Z $]];then
echo "Usage:{sh $ DB_NAME,EXAMPLE:SH $ discuz|mysql}"
Exit
Fi


if [[$UID-ne 0]];then
echo "This script must use the root user!"
Sleep 5
Exit
Fi

#Define dir and mkdir dir to determine if the directory exists and does not exist the new

if [[!-D Back_dir]];then
Mkdir-p $BACK _dir
Fi

#Use mysqldump backup MySQL uses mysqldump to back up the database

if [[$1-ne "All"]];then
/usr/bin/mysqldump-u$mysqlusr-p$mysqlpw-d $MYSQLDB > $BACK _dir/{$1}_db.sql
Else
/USR/BIN/MYSQLDUMP-U$MYSQLUSR-P$MYSQLPW--all-databases > $BACK _dir/{$1}_db.sql
Fi


if [[$?-eq 0]];then
echo "The MySQL backup successfully in $BACK _dir"
Else
echo "The MySQL backup failed"
Fi

Using the shell for MySQL backup

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.