当前位置:网站首页>Logical backup: mysqldump vs physical backup: xtrabackup
Logical backup: mysqldump vs physical backup: xtrabackup
2022-06-24 21:16:00 【Woqu database private cloud】
Logical backup :mysqldump
mysqldump yes MySQL An official tool for logical backup , The backup principle is to generate a set of data that can be imported into the database to reproduce the data and database objects in the original database SQL sentence , And then use SQL Statement to restore the database , So it is called logical backup , In addition, it supports the generation of CSV Format or XML File format .
Use mysqldump Certain permissions are required for backup , To back up table data, you need to check the table SELECT jurisdiction , Exporting views requires SHOW VIEW jurisdiction , Exporting triggers requires TRIGGER jurisdiction , Without using --single-transaction Option to lock the table LOCK TABLES jurisdiction . If you use more options , You may need more permissions .
in addition , If we need to use backup files for recovery , You must have permission to execute all statements in this file , For example, to perform CREATE Statements need to have corresponding objects CREATE jurisdiction , perform LOCK TABLES Statement LOCK TABLES jurisdiction .
Let's show you mysqldump Usage of .
mysqldump yes MySQL The original command of , Generally, we finish the installation MySQL after ,mysqldump The command can be used directly .
[[email protected] ~]# mysqldump --version
mysqldump Ver 10.13 Distrib 5.7.21, for linux-glibc2.12 (x86_64)
First, let's demonstrate the full database backup , First, we create some test data on the library to be backed up .
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> CREATE DATABASE aa;
Query OK, 1 row affected (0.04 sec)
mysql> USE aa
Database changed
mysql> CREATE TABLE t1(id INT PRIMARY KEY,name VARCHAR(20));
Query OK, 0 rows affected (0.12 sec)
mysql> CREATE TABLE t2(id INT PRIMARY KEY,name VARCHAR(20));
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO t1 VALUES(1,'aa'),(2,'bb'),(3,'cc'), ****(4,'dd'),(5,'ee');
Query OK, 5 rows affected (0.11 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> INSERT INTO t2 VALUES(1,'aaa'),(2,'bbb'),(3,'ccc'),
****(4,'ddd'),(5,'eee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
5 rows in set (0.00 sec)
here , We initiate a full database backup of the database .
[[email protected] ~]# mkdir backup
[[email protected] ~]# cd backup/
[[email protected] backup]# mysqldump -uroot -p12345678 -S /tmp/mysql-3306.sock --single-transaction --set-gtid-purged=OFF --all-databases > all_back.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] backup]# ls
all_back.sql
--single-transaction: This option applies to InnoDB Table opens a consistent snapshot , Backup InnoDB The table will not be locked , Invalid for tables from other engines .
--set-gtid-purged=OFF: In the open GTID Under the circumstances , This option determines whether to add... To the header of the backup file set global gtid_purged The sentence of ,ON To add ,OFF For not adding , Generally, the backup is set to... When it is used to build the slave database ON.
When the backup is complete , A backup file will be generated , We can check the contents of the backup file .
[[email protected] backup]# head -50 all_back.sql
-- MySQL dump 10.13 Distrib 5.7.21, for linux-glibc2.12 (x86_64)
--
-- Host: localhost Database:
-- Server version 5.7.21-log
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: aa
--
CREATE DATABASE /!32312 IF NOT EXISTS/ aa /*!40100 DEFAULT CHARACTER SET latin1 */;
USE aa;
--
-- Table structure for table t1
--
DROP TABLE IF EXISTS t1;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE t1 (
id int(11) NOT NULL,
name varchar(20) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table t1
--
LOCK TABLES t1 WRITE;
/*!40000 ALTER TABLE t1 DISABLE KEYS */;
INSERT INTO t1 VALUES (1,'aa'),(2,'bb'),(3,'cc'),(4,'dd'),(5,'ee');
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
UNLOCK TABLES;
--
By backing up the contents of the file , You can see , What is stored in the file is the data of the entire library reconstruction and re insertion SQL sentence , Logical backup is realized in this way .
Let's create a scenario of data loss , Then test the use of backup files for recovery .
mysql> USE aa
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.02 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
5 rows in set (0.00 sec)
mysql> DELETE FROM t1 WHERE id=1;
Query OK, 1 row affected (0.04 sec)
mysql> DELETE FROM t2 WHERE id=1;
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.01 sec)
Respectively in t1 and t2 In the table to delete id by 1 The data of , Then use the backup file for recovery .
[[email protected] backup]# mysql -uroot -p12345678 -S /tmp/mysql-3306.sock < all_back.sql
Verify that the data is restored to what we backed up .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
5 rows in set (0.00 sec)
From the above results , The data we deleted has been restored through backup .
Logical backup can also be used for backup recovery of specified tables or specified libraries , Let's do a demonstration , First of all, t1 Tables are backed up .
[[email protected] backup]# mysqldump -uroot -p12345678 -S /tmp/mysql-3306.sock --single-transaction --set-gtid-purged=OFF aa t1 > t1.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] backup]# ls
all_back.sql t1.sql
Check the contents of the backup file .
[[email protected] backup]# cat t1.sql
-- MySQL dump 10.13 Distrib 5.7.21, for linux-glibc2.12 (x86_64)
--
-- Host: localhost Database: aa
-- Server version 5.7.21-log
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table t1
--
DROP TABLE IF EXISTS t1;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE t1 (
id int(11) NOT NULL,
name varchar(20) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table t1
--
LOCK TABLES t1 WRITE;
/*!40000 ALTER TABLE t1 DISABLE KEYS */;
INSERT INTO t1 VALUES (1,'aa'),(2,'bb'),(3,'cc'),(4,'dd'),(5,'ee');
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET [email protected]_TIME_ZONE */;
/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;
/*!40014 SET [email protected]_UNIQUE_CHECKS */;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
/*!40111 SET [email protected]_SQL_NOTES */;
-- Dump completed on 2020-02-04 2:42:23
You can see , In the backup file is the reconstruction t1 Watch and go again t1 Table insert data SQL.
Then we manually delete some data .
mysql> USE aa
Database changed
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
5 rows in set (0.00 sec)
mysql> DELETE FROM t1 WHERE id=1;
Query OK, 1 row affected (0.01 sec)
mysql> DELETE FROM t2 WHERE id=1;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.00 sec)
utilize t1 Perform a data recovery for the backup file of the table .
[[email protected] backup]# mysql -uroot -p12345678 -S /tmp/mysql-3306.sock aa < t1.sql
Let's check the recovery status in the library .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.01 sec)
You can see t1 The table data has been recovered normally ,t2 The data of the table has not been recovered , In line with us only for t1 Table expectations for backup recovery .
The physical backup :XtraBackup
XtraBackup yes Pecona The company's open source free MySQL Backup software , It's the most popular at the moment MySQL One of the backup software , Can be non blocking to InnoDB and XtraDB Database backup . The backup principle is to back up the physical files of the database , Finally, through the application redo Log to keep data at the same point in time , Because the operation is a physical file , So it is called physical backup .
XtraBackup Need at least PROCESS,RELOAD,LOCK TABLES,REPLICATION CLIENT Several permissions ,PROCESS For viewing MySQL Related connected processes ,RELOAD and LOCK TABLES Used to perform FLUSH TABLES WITH READ LOCK、FLUSH ENGINE LOGS,REPLICATION CLIENT This node is used to view where binary logs are currently written .
Let's show you XtraBackup Usage of
First , We went to the percona Download on XtraBack Installation package , The download address is as follows .
Download Percona XtraBackup 2.4
We choose 2.4.4 Demo of version .
First, we'll download the good one XtraBackup Installation package for installation .
[[email protected] tool]# tar -xf xtrabackup-2.4.4.tar.gz
[[email protected] tool]# mv xtrabackup244/ /usr/local/xtrabackup
[[email protected] tool]# ln -s /usr/local/xtrabackup/bin/innobackupex /usr/local/bin/
[[email protected] tool]# innobackupex --version
innobackupex version 2.4.4 Linux (x86_64) (revision id: df58cf2)
Install well XtraBackup after , We do a backup of the database , Data is used mysqldump The data left over by this article , The backup command is as follows .
[[email protected] ~]# innobackupex --user=root --password=12345678 --socket=/tmp/mysql-3306.sock /root/backup/
When the backup is successful , The screen will print completed OK The words... , As shown below
200205 08:56:07 [00] Copying ib_buffer_pool to /root/backup/2020-02-05_08-56-05/ib_buffer_pool
200205 08:56:07 [00] ...done
200205 08:56:07 Backup created in directory '/root/backup/2020-02-05_08-56-05'
MySQL binlog position: filename 'mysql-bin.000002', position '777218', GTID of the last change 'ac3ef50f-17fd-11ea-9f95-0242ac12000e:1-170'
200205 08:56:07 [00] Writing backup-my.cnf
200205 08:56:07 [00] ...done
200205 08:56:07 [00] Writing xtrabackup_info
200205 08:56:07 [00] ...done
xtrabackup: Transaction log of lsn (3845814) to (3845823) was copied.
200205 08:56:07 completed OK!
Go to /root/backup View the files generated by backup in the directory
[[email protected] ~]# ls backup/
2020-02-05_08-56-05
[[email protected] ~]# cd backup/2020-02-05_08-56-05/
[[email protected] 2020-02-05_08-56-05]# ls
aa ibdata1 sys undo003 xtrabackup_info
backup-my.cnf mysql undo001 xtrabackup_binlog_info xtrabackup_logfile
ib_buffer_pool performance_schema undo002 xtrabackup_checkpoints
You can see backup There is a folder named by timestamp under the directory , This is XtraBackup Generated directory for storing backup files , If we don't want timestamp named folders , have access to --no-timestamp Parameters , Then you can customize the directory name , For example, we want to store the backup file as all_backup, The following commands can be executed during backup .
[[email protected] ~]# innobackupex --user=root --password=12345678
--socket=/tmp/mysql-3306.sock -no-timestamp /root/backup/all_backup
Go to the directory where the backup files are stored , You can see XtraBackup In fact, the physical files of the database copy A copy of , Include undo Information and generated during backup redo file (xtrabackup_logfile), Used to roll forward transactions entered during backup , Keep the data in the database at the same time .
And then there is xtrabackup_info、xtrabackup_binlog_info、xtrabackup_checkpoints Wait a few XtraBackup Generated files , For recording database binlog Information such as location and checkpoints , It can be used to build slave libraries .
Let's delete some data from the database , Then use backup to restore .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.01 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.00 sec)
mysql> DELETE FROM t1 WHERE id=5;
Query OK, 1 row affected (0.03 sec)
mysql> DELETE FROM t2 WHERE id=5;
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
+----+------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
+----+------+
3 rows in set (0.00 sec)
Delete t1、t2 surface id by 5 After the data of , We use backup to restore .
First, the backup set apply log, The purpose of this operation is to use xtrabackup_logfile file , Roll forward transactions entered during backup , Keep the data in the database at the same time .
[[email protected] 3306]# innobackupex --apply-log /root/backup/2020-02-05_08-56-05/
apply log After completion , We will clean up the database with lost data , And remove the data file , Create a new data directory .
[[email protected] 3306]# mysqladmin -uroot -p12345678 -S /tmp/mysql-3306.sock shutdown
[[email protected] 3306]# cd /dbase/
[[email protected] dbase]# mv 3306 3306bak
[[email protected] dbase]# mkdir 3306
[[email protected] dbase]# cd 3306/
[[email protected] 3306]# mkdir binlog data logs redo relaylog tmp undo
[[email protected] 3306]# **chown -R mysql:mysql ***
Execute the recovery command .
[[email protected] 3306]# innobackupex --defaults-file=/etc/mysql/my-3306.cnf --copy-back /root/backup/2020-02-05_08-56-05/
Recovery is divided into move-back and copy-back Two kinds of ,move back Is to move the data file to the data directory ,copy back Is to copy the data file to the data directory , Here's what we use copy back.
When the recovery is complete , Modify the permissions of the data file , Start the database .
[[email protected] 3306]# touch logs/err.log
[[email protected] 3306]# **chown -R mysql:mysql ***
[[email protected] 3306]# mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf --user=mysql &
Verify whether the data is recovered .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.00 sec)
You can see that the data has been restored to what it was before we deleted it .
It's about XtraBackup The process of full backup , However, there will be some databases with a large amount of data in production , If full backup is often performed, it will bring additional pressure to the database , In order to alleviate this situation , We can use XtraBackup Incremental backup function of .
Incremental backup is also called differential backup , It is based on full backup , Back up the data that changed when the incremental backup was initiated and the last backup , And full backup to form a copy of the latest data .
Let's demonstrate the process of incremental backup .
First, we do a full backup of the database .
[[email protected] 3306]# innobackupex --user=root --password=12345678
--socket=/tmp/mysql-3306.sock --no-timestamp /root/backup/all_backup
Then we insert some data into the database , Simulate incremental data .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
+----+------+
4 rows in set (0.01 sec)
mysql> INSERT INTO t1 VALUES(6,'ff'),(7,'gg');
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> INSERT INTO t2 VALUES (6,'ff'),(7,'gg');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | gg |
+----+------+
7 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | ff |
| 7 | gg |
+----+------+
6 rows in set (0.00 sec)
After inserting incremental data , We are all_backup Initiate incremental backup of the database based on .
[[email protected] 3306]# innobackupex --user=root --password=12345678 --socket=/tmp/mysql-3306.sock --incremental-basedir=/root/backup/all_backup --incremental --no-timestamp /root/backup/inc_backup_1
Then the simulation of incremental data is performed again .
mysql> INSERT INTO t1 VALUES (8,'hh'),(9,'ii');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> INSERT INTO t2 VALUES (8,'hh'),(9,'ii');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
| 9 | ii |
+----+------+
9 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
| 9 | ii |
+----+------+
8 rows in set (0.00 sec)
Then perform incremental backup inc_backup_1 And then do another increment on the basis of .
[[email protected] 3306]# innobackupex --user=root --password=12345678 --socket=/tmp/mysql-3306.sock --incremental-basedir=/root/backup/inc_backup_1 --incremental --no-timestamp /root/backup/inc_backup_2
View the directory of the backup , You can see the following folder .
[[email protected] 3306]# ls /root/backup/
all_backup inc_backup_1 inc_backup_2
all_backup、inc_backup_1、inc_backup_2 Each represents full backup 、 First incremental backup 、 Second incremental backup . The first incremental backup contains id by 6、7 Incremental data for , The second incremental backup contains id by 8、9 Incremental data for , Let's demonstrate how to delete some data , Then use the process of incremental backup recovery .
First delete some data .
mysql> DELETE FROM t1 WHERE id=9;
Query OK, 1 row affected (0.06 sec)
mysql> DELETE FROM t2 WHERE id=9;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
+----+------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
+----+------+
7 rows in set (0.00 sec)
Then backup apply log, application redo journal .
[[email protected] 3306]# innobackupex --apply-log --redo-only
/root/backup/all_backup
[[email protected] 3306]# innobackupex --apply-log --redo-only --incremental /root/backup/all_backup --incremental-dir=/root/backup/inc_backup_1/
[[email protected] 3306]# innobackupex --apply-log --incremental /root/backup/all_backup --incremental-dir=/root/backup/inc_backup_2/
--redo-only It refers to the application of redo If there are uncommitted transactions in the log, do not rollback , This parameter should be added when there are other incremental backups to be attached .
Finished application redo After the log , We will clean up the database with lost data , And remove the data file , Create a new data directory .
[[email protected] 3306]# mysqladmin -uroot -p12345678 -S /tmp/mysql-3306.sock shutdown
[[email protected] 3306]# cd /dbase/
[[email protected] dbase]# mv 3306 3306bak
[[email protected] dbase]# mkdir 3306
[[email protected] dbase]# cd 3306/
[[email protected] 3306]# mkdir binlog data logs redo relaylog tmp undo
[[email protected] 3306]# **chown -R mysql:mysql ***
Execute the recovery command .
[[email protected] 3306]# innobackupex --defaults-file=/etc/mysql/my-3306.cnf --copy-back /root/backup/all_backup
When the recovery is complete , Modify the permissions of the data file , Start the database .
[[email protected] 3306]# touch logs/err.log
[[email protected] 3306]# **chown -R mysql:mysql ***
[[email protected] 3306]# mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf --user=mysql &
Verify whether the data is recovered .
mysql> USE aa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_aa |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
| 9 | ii |
+----+------+
9 rows in set (0.02 sec)
mysql> SELECT * FROM t2;
+----+------+
| id | name |
+----+------+
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | ff |
| 7 | gg |
| 8 | hh |
| 9 | ii |
+----+------+
8 rows in set (0.00 sec)
You can see that the data before we deleted the data has been recovered .
summary
This article describes MySQL There are two common backup and recovery methods for databases , One is logical backup mysqldump, One is physical backup XtraBackup.
When a logical backup is initiated, a global read lock will be added first , Then back up non InnoDB surface , Also on InnoDB The table opens a consistent snapshot read , Dangfei InnoDB Table backup complete 、 Backup instance pos Information acquisition completed 、 After the consistency snapshot is successfully started , The logical backup will release the global read lock , Therefore, the time of data in the logical backup is the time when the backup is initiated .
Physical backup is done first copy InnoDB Tabular ibd file , meanwhile copy A new generation of redo file ,copy Add a global read lock to the database after completion , Then go to copy Not InnoDB Table data file and frm file ,copy After completion, release the global read lock to complete the backup . The use of redo Roll forward the data to the time when the backup is completed , Therefore, the time of data in the physical backup is the time when the backup ends .
Logical backup is applicable to the import and export of database or table data with a small amount of data . Physical backup is suitable for databases with large amount of data , Physical backup has two methods: full backup and incremental backup , Initiating a full backup consumes a lot of performance , The full quantity can be used + Incremental backup , For example, one full dose on Monday , Do an increment every day from Tuesday to Sunday . Because incremental backup is copy Differential data , So for some large trading volume OLTP system , Too much data has been modified , May not be suitable for incremental backup .
in addition , Because backup is not always on , If we want to restore data to a point in time between backups , Because both physical backup and logical backup can record the log location corresponding to the backup data , We can do this on the basis of backup , Flexible use binlog To recover the data compensation .
边栏推荐
- Use the transparent [x] cross button image in the dialog
- Open function
- Web automation: web control interaction / multi window processing / Web page frame
- Subnet partition operation
- Enjoy yuan mode -- a large number of flying dragons
- Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
- Simpledateformat thread unsafe
- 刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
- Packaging_ Conversion between basic type and string type
- Sleep revolution - find the right length of rest
猜你喜欢

Haitai Advanced Technology | application of privacy computing technology in medical data protection

Bean lifecycle flowchart

红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定

Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter

Vant component used in wechat applet

JMeter response assertion

Power apps Guide

JMeter parameterization

Adding subscribers to a list using mailchimp's API V3

网络安全审查办公室对知网启动网络安全审查
随机推荐
Background operation retry gave up; KeeperErrorCode = ConnectionLoss
Packaging_ Conversion between basic type and string type
Where is 5g really powerful? What is the difference with 4G?
等保备案是等保测评吗?两者是什么关系?
JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
Handling of garbled JMeter response data - three solutions
Wechat applet custom tabbar
红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
Minimum cost and maximum flow (template question)
What does virtualization mean? What technologies are included? What is the difference with private cloud?
Nifi fast authentication configuration
Address mapping of virtual memory paging mechanism
Microsoft Certification (dynamic 365) test
Format method and parse method of dateformat class
在Dialog中使用透明的【X】叉叉按钮图片
VIM usage
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
Splicing audio files with ffmpeg-4.3
Php-pdo parameter binding problem
Smooth live broadcast | analysis of key technologies for live broadcast pain points