Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell script backing up MySQL database

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

(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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report