Mysql sub-library sub-table backup methods to achieve

1.mysqldump tool introduced

mysqldump command is a MySQL database that comes with a backup command, it supports full database backup can also specify the library for backup, the backup files that are backed up as a file, and the file contents are SQL statements in the form of existence.

2. Use basic commands

(1) mysqldump syntax

mysqldump [options] -u user name -p password database name> File Backup

(2) mysqldump [options] parameter list in the following table:

 

parameter

significance

-A

Back up all databases

-B

Indicates that the specified backup multiple libraries, while performing the use db command and create databases db

-F

Refresh binlog log

-d

Back up only the table structure

-t

Back up only the data table

-l

Specify the table lock, allowing users to specify the data in a database table can not be written to access

-x

All table locks

--master-data

--master-data=1

--master-data=2

(1) increasing binlog log file name and location of the corresponding point record.

(2) no recording dot position of the annotation information from the library to be used.

(3) position of the annotation information recording dots

--single-transaction

Answer innodb database backup Affairs

--events

Dump events, the events show a warning event canceled

--flush-privileges

Refresh Database Update

mysqldump -uroot -p'123456' --all-databases \

--flush-privileges  –-lock-all-tables \

--master-data=1 --flush-logs --triggers --routines --events \

--hex-blob > /opt/mysql_bak.sql

Mysql all equipment commands data (for MyISAM engine)

mysqldump -uroot -p'123456' --all-databases \

--flush-privileges --single-transaction \

--master-data=1 --flush-logs --triggers --routines --events \

--hex-blob > /opt/mysql_bak.sql

Mysql all equipment commands data (for InnoDB engine)

(3) connected to the command mysql

[root@bigdata3 ~]# mysql -h10.9.1.43 -uroot -p123456 -e "show databases;"

 

Remove border around the -s command

[root@bigdata3 ~]# mysql -h10.9.1.43 -uroot -p123456@3306 -s -e "show databases;"

Command execution result as shown below:

 (4) mysqldump command to back up multiple databases

[root@bigdata3 ~]# mysqldump -h10.9.1.43 -uroot -p123456 -B dandan_test oozie > /home/centos/mysql_back.bak

Under cd to / home / centos catalog generated the file

 See enter the file, the backup of all data generated in the form sql command, as shown in FIG.

(5) mysqldump backup compression

[root@bigdata3 ~]# mysqldump -h10.9.1.43 -uroot -p123456 -B dandan_test | gzip > /home/centos/mysql_back_B.sql.gz

View:

 

(6) database backup and recovery

删掉dandan_test:drop database dandan_test;

Uncompressed recovery:

[root@bigdata3 centos]# mysql -h10.9.1.43 -uroot -p123456 </home/centos/mysql_back.bak

Compression recovery:

[root@bigdata3 centos]# gunzip < /home/centos/mysql_back_B.sql.gz | mysql -h10.9.1.43 -uroot -p123456

verification:

mysql -h10.9.1.43 -uroot -p123456 -e "use dandan_test;select * from tb_score1;"

[root@bigdata3 centos]# mysql -h10.9.1.43 -uroot -p123456 -e "use dandan_test;select * from tb_score1;"

(7) mysql backup table structure

[root@bigdata3 centos]# mysqldump -h10.9.1.43 -uroot -p123456 -d dandan_test >/home/centos/dandan_test_back.bak

View:

 (8) mysql backup table data

[root@bigdata3 ~]# mysqldump -h10.9.1.43 -uroot -p123456 -t dandan_test >/home/centos/dandan_test_back.bak

See the results mentioned below:

 

3.mysql minute backup shell script

Specific shel script as follows:

#!/bin/bash

DATE=$(date '+%Y%m%d')

HOST=10.9.1.43

USER=root

PASS=123456

BACKUP_DIR=/home/centos/db_backup

DB_LIST=$(mysql -h$Host -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep "dandan_test|oozie")

if [ ! -d $BACKUP_DIR ]

then

mkdir -p "$BACKUP_DIR"

fi

for DB in $DB_LIST; do

    BACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sql

mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null

    if [ $? != 0 ]; then

        echo "$BACKUP_NAME 备份失败!"

    fi

done

4 mysql points table backup shell script

Concrete shell script is as follows:

#!/bin/bash

DATE=$(date '+%Y%m%d')

HOST=10.9.1.43

USER=root

PASS=123456

BACKUP_DIR=/home/centos/db_backup

DB_LIST=$(mysql -h$HOST -u$USER-p$PASS -s -e "show databases;" 2>/dev/null |egrep "dandan_test|oozie")

for DB in $DB_LIST; do

    BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}

    [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null

    TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)

    for TABLE in $TABLE_LIST; do

        BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql

        if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then

            echo "$BACKUP_NAME 备份失败!"

        fi

    done

done

5. Summary

   This paper describes a backup method Mysql sub-library sub-table, and mysqldump tool specific operation were analyzed, gives a specific backup shell script, can be backed up in accordance with the project requirements by crontab regular tasks.

  This article points:

  • (1) instruction mysql connection with the proviso mysql client needs to be installed.
  • (2) mysqldump command, note that sub-libraries, the specified parameters, and a method for the recovery of part tables.
  • Shell scripting (3) mysql sub-library
  • (4) mysql points table of shell scripting (Suggested writing, basic instructions need to be successful experiments in linux)
发布了11 篇原创文章 · 获赞 165 · 访问量 5377

Guess you like

Origin blog.csdn.net/godlovedaniel/article/details/104525006