"Getting MySQL- Backup and Recovery"


This article will explain the main MySQL database backup and recovery-related knowledge, mainly focusing on the logical backup, and introduce the use of mysqldump tool recovery method.

Here simply, the concept of logical and physical backup of backup:

Physical backup: back up your data files, dump the database physical files to a directory. Physical backup and recovery faster, but space is relatively large, MySQL can be used xtrabackup tools for physical backup.

Logical backup: database objects using the tools to export work, aggregate into the backup file. Logical backup and recovery is slow, but a small footprint and more flexible. MySQL backup tool commonly used logic to mysqldump.

1. Back up the entire database

To use the entire backup mysqldump example, you may be used --all-databases or -A parameters:

mysqldump -uroot -pxxxxxx --all-databases > /tmp/all_database.sql mysqldump -uroot -pxxxxxx -A > /tmp/all_database.sql

2. Back up the database part

Sometimes we encounter only need to backup needs some libraries, this time we can use --databases or -B parameter, and the parameter followed by the database name, separated by a space among multiple databases.

mysqldump -uroot -pxxxxxx --databases testdb1 testdb2 > /tmp/testdb.sql mysqldump -uroot -pxxxxxx -B testdb1 testdb2 > /tmp/testdb.sql

Table 3. Backup section

Usually we will have to back up the demand part of the table, for example, make a backup of the table before the change, then we can do this:

# Only backup testdb library test_tb table mysqldump -uroot -pxxxxxx testdb test_tb> /tmp/test_tb.sql # backup multiple tables mysqldump -uroot -pxxxxxx testdb tb1 tb2 tb3> /tmp/tb.sql

4. The part of the data to the backup unit table

Sometimes a large amount of data to a table, we only need part of the data, then how to do it? This time you can use the option --where. Back where proviso to be met. For example: We just need to tb1 table 2019-08-01 create_time greater than the data, then you can export this:

mysqldump -uroot -pxxxxxx testdb tb1 --where=" create_time >= '2019-08-01 00:00:00' " > /tmp/tb1.sql

5. exclude tables derived

If we want to back up a library, but some tables or large amounts of data associated with a small business, this time can be considered to exclude these tables, the same option --ignore-table can perform this function.

mysqldump -uroot -pxxxxxx testdb --ignore-table=testdb.tb1 > /tmp/testdb.sql

6. Only the backup configuration data backup or just

Only if the backup structure --no-data may be used for short -d option; only back up data using --no-create-info for short -t option.

mysqldump -uroot -pxxxxxx testdb --no-data > /tmp/testdb_jiegou.sql mysqldump -uroot -pxxxxxx testdb --no-create-info > /tmp/testdb_data.sql

7. Backup contains stored procedures, functions, events

mysqldump备份默认是不包含存储过程,自定义函数及事件的。我们可以使用 --routines 或-R 选项来备份存储过程及函数,使用 --events 或 -E 参数来备份事件。例如:我们想备份整个testdb库,包含存储过程及事件:

mysqldump -uroot -pxxxxxx -R -E --databases testdb > /tmp/testdb.sql

8.以事务的形式备份

如果我们想在dump过程中保证数据的一致性,减少锁表,则可以用 --single-transaction 选项,这个选项对InnoDB的数据表很有用,且不会锁表。 加q群:478052716 免费领取(Java架构资料,视频资料,BATJ面试资料)

mysqldump -uroot -pxxxxxx --single-transaction --databases testdb > /tmp/testdb.sql

9.全量备份恢复

如果我们现在有昨天的全量备份,现在想整个恢复,则可以这样操作:

mysql -uroot -pxxxxxx < /tmp/all_database.sql

10.从全量备份中恢复单库

可能有这样的需求,比如说我们只想恢复某一个库,但是我们有的是整个实例的备份,这个时候我们想到能不能从全量备份中分离出单个库的备份,答案是可以的,下面这个简单的shell可以帮到你哦:

sed -n '/^-- Current Database: `testdb`/,/^-- Current Database: `/p' all_databases.sql > testdb.sql #分离完成后我们再导入testdb.sql即可恢复单个库

11.从单库备份中恢复单表

这个需求还是比较常见的,毕竟单库或全量恢复涉及的业务还是比较多的,恢复时间也比较长,比如说我们知道哪个表误操作了,那么我们就可以用单表恢复的方式来恢复。例如:现在我们有testdb整库的备份,但是由于tb1表误操作,需要单独恢复出这张表,那么我们可以这么做:

cat testdb.sql | sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `tb1`/!d;q' > /tmp/tb1_jiegou.sql cat testdb.sql | grep --ignore-case 'insert into `tb1`' > /tmp/tb1_data.sql #用shell语法分离出创建表的语句及插入数据的语句后 再依次导出即可完成恢复

总结:

本篇文章给出了在不同场景下的备份及恢复方法,可能生产中还会有更复杂的场景,需要大家灵活应变。

郑州不孕不育医院:http://www.zzchyy110.com/

Guess you like

Origin blog.51cto.com/14510269/2437056