Incremental Backup and Restore Mysql database

                  Mysql数据库的增量备份与还原

First, the purpose of backup
do disaster recovery: recover the damaged data and restore.
Second, the problem needs to be considered backups
can accept how much data loss;
recovery time, how long the data;
whether continuing to provide service when restoring data;
the object of recovery, a library, multiple tables, a single table or the entire database.

Third, the type of backup
1, a full backup of the data each time a full backup of a database, backup the entire database. Therefore, a full backup takes longer.
2, on the basis of incremental backup the last backup, the backup data is updated.

Fourth, how full backup
in smaller databases, the library can be used to back up less than mysql mysqldump backup tool that comes with a full backup of the database or table (50G) time.
Mysqldump backup tool
advantages:
backup granularity flexible, both back up the entire mysql service can also back up individual database or table.
Disadvantages:
1, accuracy of data loss can occur when a float.
2, the backup process is Mysqldump serialization will not be backed up in parallel, when a large amount of data backup, takes too inefficient.
Mysqdump tools innodb storage engine supports hot backup, that backed up without affecting the service used.
Use the syntax;
.. In the name of mysqldump -uroot -p'password 'database name> command line backup files ending in SQL
-B option to export certain specified / certain libraries separated by a space containing create database statement.

5, how to incremental backup
binary log, binary log
records all add, delete, change, and other operations log database. You can use mysqlbinlog view the log at the command line. May backup and restore data using binlog log file.
See binlog data function is enabled
Show variables like 'log_bin%';
To turn function can be modified my.cnf file binlog
add log-bin = / data / mysql / log / mysql_bin end of the file
server-id = 1
Restart Service allowed mysql can take effect.
Mysqlbinlog serves the specified binlog file | mysql -uroot -p password to restore all the operations specified log file recorded.
VI, database backup and recovery experiments.

Preparation experimental environment
1, enters into the mysql create experimental database Database Shiyan Create;
the Use Shiyan; switch the current database Shiyan
the Create Table kdata1 (ID int, char name (200 is)); Create Table
2, modify the my.cnf mysql configuration . in the last line of the file into
the log-bin = / the Data / mysql / log / mysql_bin
Server-the above mentioned id = 1
restart mysql service for the changes to take effect of configuration items is open binlog service
3,
the first experimental database to do a full backup
Mysqldump -uroot - p password --single-transaction --flush-logs -B shiyan > shiyan_data _ $ (date +% Y% m% d% H% m% S) .sql
when -single-transaction is only hot backup storage engine is innodb effective
-flush-logs is to generate a new refresh two binlog file system log.
4,
inserted in kdata1 table showing experimental data library insert into shiyan.kdata1 values (1, ' zhangsan');
if data is successfully select * from inserted shiyan.kdata1;
Here Insert Picture Description
you can see the data is successfully inserted.
Mysqlbinlog serves -vv /data/mysql/log/mysql_bin.000001
Here Insert Picture Description

Can be seen that the operation just inserted data have been recorded in the log file.
5,
the use of flush logs; generates a new log file
and create a new table in the two experiments library
Create table kdata2 (u_id int, b_name char (20 ));
inserting a data
insert into shiyan.kdata2 values (2, ' wangwu');
View data
Here Insert Picture Description
Set sql_log_bin = 0 binlog temporary closure function
data 6, a table of simulated loss
Drop table kdata2; table II deleted
7, using binlog log . to recover deleted table II
Mysqlbinlog /data/mysql/log/mysql_bin.000002 | mysql -uroot -p password at the command line and then
into the mysql use shiyan; switch to the experimental library
Show tables; view the current library table
Here Insert Picture Description
can see table II has been restored to
look at the record table II
Here Insert Picture Description
data in table II also looking back.
8, lost simulation database
Drop database shiyan; deleted experimental database
and then restore the database
1, the first full backup is performed using the file recovery
Mysql -uroot -p password </root/shiyan_data_20200112131142.sql data reduction
And then view the table experiment library
Show tables;
Here Insert Picture Description
find a table but did not restore back Table II
then want the greatest degree of recovery data file binlog need to use as much as possible to restore the data before the database is lost.
Mysqlbinlog serves / the Data / MySQL / log / mysql_bin.00000.2 | mysql -uroot -p password
then go to the library to view the test tables will see table II also restored

Experimental Summary:
The individual using the full backup is undoubtedly a great disadvantage when increasing the efficiency of data backed up for a long time but we have less and less efficient use of backup full backup plus incremental backup binlog log data will not be much the impact and can also restore restore to the state before the greatest degree of data loss, minimize data loss.

Published an original article · won praise 3 · views 44

Guess you like

Origin blog.csdn.net/weixin_46182162/article/details/103948309