Get the App
SLTechnology News&Howtos  ›  Database  › 

Shell script backing up MySQL database

Shulou Source: shulou.com Published: 2022-06-01 14:52:59 10月16日 Update

(1) thinking

Install the MySQL database

Create databases, tables, insert data

Authorize a user name and password for backing up the database

Command to back up the database: mysqldump-u root-p 1234-d MySQLDB > 1.sql

Introducing if statement to back up all databases

Introducing for loop statement to back up multiple databases

(2) actual combat cases

Write MySQL database backup script, you can backup any database, input A, backup A, enter B, back up B, enter C, backup C, enter ALL, and back up all databases.

(3) installation steps

Install the MySQL database

Yum install mysql-server mysql-devel mysql-libs-y

Restart the MySQL database

Service mysqld restart

Enter database command line mode

Create 3 databases, create 3 tables in each of the 3 databases, and insert a piece of data in the table

Create database jfedu

Use jfedu

Create table T1 (id varchar (20), name varchar (20))

Insert into T1 values ("1", "daqi")

Desc t1

Select * from T1

Note:

Don't forget to verify that the database, table, and data have been created successfully

Authorize a user name and password for backing up the database

Grant all on *. * to backup@localhost identified by "123456"

#! / bin/bash

# 10:28:54, December 6, 2017

# by author daqi

# mysqldump MySQLDB

BAKDIR=/data/backup/mysql/ `date +% Ymuri% mmury% d`

MYSQLDB=$*

MYSQLPW=123456

MYSQLUSR=backup

# must use root user run scripts must be run with the root user, and $UID is the system variable

If [$UID-ne 0]; then

Echo This script must use the root user!

Sleep 2

Exit

Fi

# determine the number of parameters entered by the user

If [- z "$1"]; then

Echo-e "\ 033 [36mPlease Select mysqldump MySQLDB:jfedu | taobao | jd | all\ 033 [0m"

Exit 0

Fi

# Define DIR and mkdir DIR determines whether the directory exists. If it does not exist, create a new one

If

[!-d $BAKDIR]; then

Mkdir-p $BAKDIR

Fi

# Use mysqldump backup Databases

If [$1 = "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

Tags: Data database backup input user command password user name statement script success number parameters variables multiple actual combat ideas cases patterns steps Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi MySQL Linux OPPO Reno