07: mysql backup and recovery (1) - mysqldump command

Note: mysql different engines different backup methods. Let's recall the knowledge and backup-related.

1, the storage engine
(1) InnoDB (default engine, is also commonly used in business)
Hot Standby
separate table space (one table per table space)
redo: redo logs, roll former used to
undo: rollback log for rollback (uncommitted transactions)
row-level locking, based on the index to achieve, GAP lock
support transactions.
(2) MyISAM
warm standby
three files constituting the
table lock


2, the binary log
(1) What is the record?
DDL, DCL these kinds of statements, recording is operating statement
DML: he recorded the transaction log has been submitted, and supports a variety of formats record (Row, of Statement, Mixed)
(2) Events
start position (at xxx)
end up (under a at)
a transaction, there are more events made (begin to the commit)
(3) intercepts the binary log
1, the binary log analysis
found to be intercepted log starting position (start-position) and end position (STOP-position)
mysqlbinlog - Output = =-decode -base64 -vvv rows
2, log intercept
the mysqlbinlog = -d Test --start-position = position XXX-XXX --stop
------------------ -------------------------------------------------- ---
MySLQL backup

1, the backup is for what?
In order to restore the database is corrupted
2, database corruption,
physical damage
highly available architecture can be solved
logical corruption
can only be solved with a backup

3, backup type

Cold backup
backups while the database is not working. High data consistency and high reliability. Affects all read and write.
Warm backup
database does not stop working, but the table is locked at the time of the backup, does not affect the reading, but the impact of the write operation.
Hot standby
database does not stop working, based on the database online backup features affairs, reading and writing small impact.
Limited by the storage engine type (innoDB).
4, backup
logical backup (text representation: SQL statement)
physical backup (binary copy of the data file)

Based backup copy of
incremental backups (refresh the binary log)

5, the backup tool
logical backup:
1, mysqldump
- logical backup tool that comes with mysql native good use
- Backup Features: SQL Backup
- Benefits: high readability, high compression ratio
- Disadvantages: backup and recovery need to spend more time
- more dependent on the performance of the CPU and IO
2, the mysqlbinlog
- original ecological backup command binlog achieve
physical backup:
. 3, xtrabackup (third party, need to be individually configured)
- high performance precona developed physical backup tool
- backup features : backup is the actual database files
- advantage: the backup and rapid recovery
- disadvantages: poor readability, low compression ratio
- mainly consumed IO

How to choose the backup tool:
1: less than 100G, mysqldump generally can be controlled at about 60 minutes is acceptable
2: 100G or more, to the level of TB, xtrabackup appropriate, backup and recovery faster than the speed
large data two-level, or more TB , the how selection? ? ? ? ? ?

6, the backup tool:

(1) mysqldump backup commands:
data amount calculation:
SELECT SUM (Index_length the data_length +) / 1024/1024/1024 from information_schema.tables;

Common parameters:
0> -u -p -h -P -S and client-related parameters

1> -A full backup
mysqldump -uroot -p123 -A> /backup/full.sql

2> -B backup one or more single reservoir
the mysqldump MySQL -uroot--p123 -B World> /backup/data.sql
the mysqldump -uroot--p123 -B World> /backup/world.sql

3> single database or a single table backup
to the backup unit library (Consideration of this indiscriminate -B)
the mysqldump -uroot--p123 world> /backup/world1.sql
City world database table in the backup <careful not to add -B, -B behind only full database>
mysqldump-uroot--p123 world City> /backup/city.sql

** thinking and verify the following command difference:
mysqldump-uroot--p123 -B world> /backup/world.sql
with
mysqldump -uroot -p123 world> /backup/world1.sql

vimdiff wordpres.sql world1.sql

Conclusion:
1, -B when the backup will add create database and use sentence
2, -B backup and recovery, direct source
3, when no -B backup and recovery, need to create a good database, perform source into use

4> -d only backup table structure
mysqldump -uroot -p123 -A -d> /backup/aa.sql

5> Production options will be added (. 1)
-R & lt backup data stored procedures and functions
--triggers backup trigger data
mysqldump -uroot -p123 -A -R --triggers> /backup/full.sql


<6> -F flush logs
during backup, refresh a new binary log file
(there are several database will refresh a few binlog file - so generally we rarely use, using the following parameters <7>)
mysqldump - uroot -p123 -A -R --triggers -F> /backup/full.sql

<7> Production will increase options (2)
--master Data-recording increases. 1 = binlog log file name and location points corresponding executable statement
--master-data = 2 into the recording statements - Notes, It does not perform (use this default)

Features:
1, the start time of the backup, then the binary log file record number, location number, the future can be used as the interception of the binary log starting position
2. If this parameter is added automatically backed up on a table lock for added --single-transaction parameters of time,
if the table will innodb implement "hot standby"

mysqldump -uroot -p123 -A -R --triggers --master-data=2>/backup/full.sql

<8> Production options will be added (. 3)
--single-Transaction main function is that for innodb table implementation "hot standby" (snapshot)
the mysqldump -A -R & lt --triggers -p123 -uroot-Data---master = 2 --single-transaction> /backup/full.sql

What The principle mysqldump hot spare is?
1, the premise is hot standby, can only be achieved innodb table of
2, hot standby implementation principle, based on a snapshot point in time of
3, my understanding is that the number of employees. . . . .

-----------------------------
<. 9> enterprise backup achieve the mysqldump
mysqldump -uroot -p123 -A -R --triggers - master-data = 2 --single-transaction | gzip> / backup / alL _ $ (date +% F) .sql.gz

Based mysqldump backup strategy?
0, the order of data within 100G, more appropriate time control within 1 hour.
1 hour binary log backup, remote backup can be set.
2, and the backup data to be stored on different devices.
3, the above operations are made crontab.

Backup Strategy 1: full backup daily, hourly backup binlog
00 23 * * * full.sh
00 * * * * binlog.sh
----------------
backup strategy 2: Weekly Japan backup all equipment, other times the backup binlog

00 23 * * 7 full.sh (mysqldump all equipment)
00 23 * * 1-6 binlog.sh (ideas find out binlog files, packaged pushed to the remote backup)

----------------
from the remote backup:
the mysqldump -uroot--p123 -H 10.0.0.51 -A -R & lt --triggers --single --master-2-Transaction Data = | gzip> / backup / alL _ $ (date +% F) .sql.gz

------------------------
<10> using the fault data backup and recovery

Failure Case basic background:
1, single-node database MySQL 5.6.34
2, the amount of data: 30G
3, backup strategy: mysqldump + mysqlbinlog
description of the problem:
developers, Monday 10:00, accidentally deleted the production library oss_base (billing, tents, His, Boss) all data, core online applications Crash.
Fails:
1, businesses will be disconnected to prevent secondary victimization
2, set up a standby database, restore the full backup on Sunday.
3, the binary log intercept, to restore the backup repository.
4, testing the backup library data, availability and integrity
5, recovery applications
Method 1: Apply directly to the cutting by the user, open the application
two ways: the failure data, production fell back to RMB library, open the application
project Results: 30 minute processing, business returned to normal.


Fault Simulation and Recovery:
1, Sunday 23:00 all equipment
mysqldump -uroot -p123 -A -R --triggers --master- data = 2 --single-transaction | gzip> / backup / alL _ $ (date +% F) .sql.gz

2, the analog data changes Monday
MySQL> Create Database oss_base charset UTF8;
MySQL> oss_base use
MySQL> Create Table oss_tab (int ID, name VARCHAR (20 is));

mysql> insert into oss_tab values(1,'a');
mysql> insert into oss_tab values(2,'b');
mysql> insert into oss_tab values(3,'c');
mysql> insert into oss_tab values(4,'d');
mysql> commit;

3, simulated fault
mysql> drop database oss_base;

4, Recovery
(1) Check all equipment, to find the starting position of the binary log taken
- the MASTER the TO of MASTER_LOG_FILE the CHANGE = 'MySQL-bin.000009', MASTER_LOG_POS = 120;
(2) Preparation of Full Recovery
gunzip alL_2019-10-03. sql.gz
SET SQL_LOG_BIN = 0;
Source /backup/alL_2019-10-03.sql
Heretofore, data has been restored to the last time point of 23:00.
(3) taken binary log

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000009 | 975 | | | |
+------------------+----------+--------------+------------------+-------------------+

mysql> show binlog events in 'mysql-bin.000009';

| mysql-bin.000009 | 871 | Query | 6 | 975 | drop database oss_base

mysqlbinlog --start-position=120 --stop-position=871 /data/mysql/mysql-bin.000009 >/backup/binlog.sql

(4) Recovery binary log
MySQL> SQL_LOG_BIN SET = 0;
MySQL> Source /backup/binlog.sql
MySQL> Show Databases;
MySQL> use oss_base;
MySQL> SELECT * from oss_tab;

_______________________________________________________
Test 1: rm -rf / application / mysql / data / database restore deleted idea?
Note: The binary log / data / mysql not deleted, generation binlog files and data files are open-minded, we are here to put together

(1) data deletion
RM -rf / file application / MySQL / Data / *
(2) to close the database
the pkill mysqld / the kill -9 ID / killall mysqld
(. 3) initialization data
/ application / mysql / scripts / mysql_install_db --user = mysql = --basedir / file application / MySQL --datadir = / file application / MySQL / Data /
(. 4) recovery
(1) check all equipment, to find the starting position of the binary log taken
- CHANGE MASTER tO MASTER_LOG_FILE = 'mysql -bin. 000009 ', MASTER_LOG_POS = 120;
(2) Preparation of full recovery
gunzip alL_2010-10-03.sql.gz
SET SQL_LOG_BIN = 0;
Source /backup/alL_2019-10-03.sql
Heretofore, data has been restored to the last 23 point point in time.
(3) binary logs taken

MySQL> Show Master Status;
+ ------------------ + + ------- ---------- ------------------ + ------- + ------------------- +
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000009 | 975 | | | |
+------------------+----------+--------------+------------------+-------------------+

mysql> show binlog events in 'mysql-bin.000009';

| mysql-bin.000009 | 871 | Query | 6 | 975 | drop database oss_base

mysqlbinlog --start-position=120 --stop-position=871 /data/mysql/mysql-bin.000009 >/backup/binlog.sql

(4) Recovery binary log
MySQL> SQL_LOG_BIN SET = 0;
MySQL> Source /backup/binlog.sql
MySQL> Show Databases;
MySQL> use oss_base;
MySQL> SELECT * from oss_tab;

-------------------------------------------------- ------------------------
think, if they do drop after a little modification normal operation of other libraries, how to recover?
For example:
Create Test Database;
use Test;
Create Table Tab (int ID, name VARCHAR (20 is));
INSERT INTO Tab values (. 1, 'A'), (2, 'B');
the commit;

Solution:
find binlog which point drop in that position until the end point of the binlog, this paragraph imported into the database inside it.

______________________________________________

Guess you like

Origin www.cnblogs.com/jim-xu/p/11619613.html