Getting MySQL-- backup and recovery

Preface: 

Previous articles, we will introduce the various MySQL statement syntax and usage of user rights knowledge. 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, under the concept of physical backup and logical 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, 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 parameters, and the parameters 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:

#只备份testdb库中的test_tb表
mysqldump -uroot -pxxxxxx testdb test_tb > /tmp/test_tb.sql
#备份多张表
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 --where option of. 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, the 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

Then backs up only structure that can be used --no-data for short -d options; backup data can be used only --no-create-info for short -t options.

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 backup default is not to include stored procedures, custom functions and events. We can use --routines or -R option to back up stored procedures and functions, use --events or -E argument to back the event. For example: We want to back up the entire testdb library that contains stored procedures and events:

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

8. Back in the form of a transaction

If we want to ensure the consistency of the data in the dump process, reduce lock table, you can use --single-transaction that options for InnoDB data table is useful, and does not lock the table.

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

9. A full backup recovery

If we now have a full backup of yesterday, and now want to restore the whole, you can do this:

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

10. The recovery from single-database full backup of

There may be such a demand, for example, we only want to restore one library, but we have a backup of the entire instance, at this time we can not think of a single library backup isolated from the full backup, the answer is yes, the following simple Oh shell can help you:

sed -n '/^-- Current Database: `testdb`/,/^-- Current Database: `/p' all_databases.sql > testdb.sql

#分离完成后我们再导入testdb.sql即可恢复单个库

11. restore a single table from a single database backup

The demand is still relatively common, after all, a single library or restore full amount involved in the service, or more, recovery time is relatively long, for example, we know which misuse the table, then we can restore a single table with a way to recover. For example: We now have a backup testdb entire library, but due to misuse tb1 table, separate out the need to restore this table, then we can do this:

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语法分离出创建表的语句及插入数据的语句后 再依次导出即可完成恢复

to sum up: 

This article gives the backup and restore methods in different scenarios, production may also have more complex scenarios, we need to be flexible. At this point, "Getting MySQL" series has ended, although this is not so in-depth articles written interesting, but we still hope that after reading to have a knowledge base of MySQL. Here also like to thank you for reading, reading again and again is that you let me write down power! I will share MySQL follow-up related articles, I hope you continue to focus.

Guess you like

Origin blog.51cto.com/10814168/2436443