Shell script backup MySQL database

Source: Internet
Author: User

(1) Ideas


<1> Install MySQL Database


<2> Create a database, table, insert data

<3> authorize a user name and password to back up the database


<4> Backup DATABASE commands: Mysqldump-u root-p 1234-d mysqldb > 1.sql


<5> introduce an If statement to make a backup of all databases


<6> introducing A For loop statement for backing up multiple databases


(2) Practical cases

Write MySQL database backup script, you can backup any database, enter a, backup a library, enter B, backup B library, enter C, backup C library, enter all, back up all databases


(3) Installation procedure

<1> Install MySQL Database

Yum Install mysql-server mysql-devel mysql-libs-y

<2> Restart MySQL Database

Service mysqld Restart


<3> Enter database command line mode

Create 3 databases, create 3 tables in 3 databases, insert a single piece of data into the table

Create Database jfedu;

Use jfedu;

CREATE TABLE T1 (ID varchar (), name varchar (20));

INSERT into T1 values ("1", "Daqi");

Desc T1;

SELECT * from T1;

Comments:

Do not forget to verify that the database, table, data has been created successfully


<4> authorize a user name and password to back up the database

Grant all on * * to [e-mail protected] identified by "123456";


<5>

#!/bin/bash

#2017年12月6日10:28:54

#by author Daqi

#mysqldump MySQLdb

bakdir=/data/backup/mysql/' Date +%y-%m-%d '

mysqldb=$*

mysqlpw=123456

Mysqlusr=backup

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

If [$UID-ne 0];then

echo This script must use the root user!!!

Sleep 2

Exit

Fi

#判断用户输入参数的个数

If [-Z "$"];then

Echo-e "\033[36mplease Select Mysqldump mysqldb:jfedu|taobao|jd|all\033[0m"

Exit 0

Fi


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


If

[!-D $BAKDIR];then

Mkdir-p $BAKDIR

Fi


#Use mysqldump Backup Databases

if [= = = "All"];then

/USR/BIN/MYSQLDUMP-U$MYSQLUSR-P$MYSQLPW--all-databases > $BAKDIR/all_db.sql

echo "The $BAKDIR/all_db.sql Mysql Database Backup successfully"

Else

For i in ' echo $MYSQLDB '

Do

/usr/bin/mysqldump-u$mysqlusr-p$mysqlpw-d $i > $BAKDIR/${i}_db.sql

echo "The MySQL databases $BAKDIR/${i}_db.sql successfully"

Done

Fi



Shell script backup MySQL database

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.