Mysql database full backup and incremental backup and recovery

mysql data backup:

Data backup method

  • Physical backup:
  • Cold backup: Cold backup refers to backup after the database is closed, applicable to databases of all modes
  • Hot standby: generally used to ensure the normal and uninterrupted operation of the service. Two machines are used as service machines, one is used for actual database operation applications, and the other obtains data from the former in real time to maintain data consistency. If the current machine is turned off , the backup machine immediately replaces the current machine and continues to provide services
  • Common commands for cold standby: cp tar scp ...
  • Logical backup:
  • mysqldump //backup command
  • mysql/restore data command

1. Physical backup and recovery:

1.1. Backup operation:

实例1:
mkdir -p /data/mysql
cp -r /var/lib/mysql/ /data/mysql/mysql.bak
实例2:
tar -zcvf /data/mysql/mysql.tar.gz  /var/lib/mysql
实例3:
#在备份数据库创建备份目录:
mkdir /data
#备份数据
scp -r /var/lib/mysql  192.168.2.20:/data/mysql.bak

2. Mysqldump backup:

  • Before the mysqldump backup is complete, all tables will be locked, making it impossible to write.

2.1. Only back up the table, not the data itself:

备份mysql数据库中的所有表,但是不会自动生成创建mysql数据库的语句:
[root@www ~]# mysqldump -uroot -p1234  mysql  > /root/mysql.sql

2.2. Backup database and tables:

备份mysql数据库中的所有表,并且会生成创建mysql数据库的SQL语句,也就是导入时不需要先创建数据库:
[root@www ~]# mysqldump -uroot -p1234 --databases mysql  > /root/mysql.sql

2.3. Backup multiple databases:

备份数据库MySQL、ys到/root/ys_mysql.sql
[root@www ~]# mysqldump -uroot -p1234 --databases mysql ys  > /root/ys_mysql.sql

2.4. Backup all databases:

[root@www ~]# mysqldump -uroot -p1234 --all-databases >/root/all.sql
或者:
[root@www ~]# mysqldump -uroot -p1234 -A >/root/all.sql

2.5. Back up the mysql database and record the pos point:

[root@www ~]# mysqldump -uroot -p1234 --master-data mysql > /root/mysql.sql

2.6. Back up the database and refresh the log:

[root@www ~]# mysqldump -uroot -p1234 --master-data --flush-logs mysql > /root/mysql.sql

2.3. Database recovery:

[root@www ~]# mysqldump -uroot -p1234 --databases ys > /root/ys.sql
首先把ys数据库删除
mysql> drop database ys;
Query OK, 31 rows affected, 2 warnings (0.06 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| mysql                |
| sys                 |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
## 恢复数据库ys
[root@www ~]# mysql -uroot -p1234 < /root/ys.sql 
[root@www ~]#  mysql -uroot -p1234
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| sys                |
| mysql              |
| ys                 |
+--------------------+
4 rows in set (0.00 sec)
 
 或者是:
 mysql> source /root/ys.sql;   ---source 命令导入数据库需要先登录到数库终端:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| sys                |
| mysql              |
| ys                 |
+--------------------+
4 rows in set (0.00 sec) 

3. Incremental backup and recovery

  • Features of Incremental Backup and Recovery
  • The advantage of incremental backup is that there is no duplicate data, the backup volume is small, and the time is short. But the disadvantages are also obvious. It needs to establish the last full backup and all the increments after the full backup to restore.

3.1. Overview of binlog logs:

  1. What is binlog log?
  2. Also known as binary log
  3. A type of mysql service log file
  4. Log all SQL statements except queries
  5. Can be used for data backup and recovery
  6. Necessary conditions for configuring mysql master-slave synchronization

3.2. Startup log

configuration item use
server_id=Number Specify the id value (1-255)
log_bin[=directory name/file name] Enable binlog log
max_binlog_size=value m Specify binlog log file capacity (default 1G)
[root@localhost ~]# vim /etc/my.cnf
修改内容如下:
[mysqld]
.....
log_bin
server_id=100
[root@localhost ~]# systemctl restart mysqld
###查看binlog日志是否开启
登录数据库
mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| localhost-bin.000001 |      154 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  • Binlog related files are stored in /var/lib/mysql/ by default
  1. hostname-bin.index index file
  2. hostname-bin.000001 first binary
  3. hostname-bin.000002 Second binary
    ...
  • Under normal circumstances, the binlog binary file has a default capacity of 1G, which exceeds the capacity of recreating a new binlog binary file. The method to generate a new binlog log file is as follows:
1.重启mysql服务
systemctl   restart  mysqld
或者:
mysql  -root -p密码  -e 'flush logs'
或者:
mysqldump  --flush-logs

3.3. Incremental backup

  • Regularly execute the flush logs method to recreate new logs, generate binary file sequences, and save these files to a safe place in time to complete an incremental backup for a period of time
##创建数据库
create database school default charset=utf8mb4;
##创建表格
use school;
create table stuinfo(
                       -> id int primary key auto_increment,
                       -> name char(20) not null,
                       -> sex enum('boy','girl') not null,
                       -> age int unsigned not null,
                       -> likes set('run','go fishing','Listen to the music','Play basketball') default'run,play basketball');
##往stuinfo表格里插入数据
insert into stuinfo values(1,'bob','boy',25,'run,go fishing');
##查看表格数据:
select * from stuinfo;
+----+------+-----+-----+----------------+
| id | name | sex | age | likes          |
+----+------+-----+-----+----------------+
| 1  | bob  | boy | 25  | run,go fishing |
+----+------+-----+-----+----------------+
##退出数据库并使用mysqldump完全备份school数据库并重新创建一个binlog文件。
exit    ---退出数据库
mkdir /data ---创建存放数据的目录
mysqldump -uroot -p1234  --flush-logs -B school > /data/$(date +%Y-%m-%d)-school.sql
ls /data/
2023-02-14-school.sql
##进入数据库继续往表格里插入数据
use school;
insert into stuinfo values(2,'tom','boy',28,'run,play basketball');
select * from stuinfo;
+----+------+-----+-----+---------------------+
| id | name | sex | age | likes               |
+----+------+-----+-----+---------------------+
| 1  | bob  | boy | 25  | run,go fishing      |
| 2  | tom  | boy | 28  | run,Play basketball |
+----+------+-----+-----+---------------------+
##退出数据库生成增量备份文件
exit;    ---退出数据库
mysql -uroot -p1234 -e "show master status;flush logs"    ----查看当前的binlog文件名称,然后创建一个新的binlog文件
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| localhost-bin.000002 |      436 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
##把localhost-bin.000002文件拷贝到/data下并修改当前日期作为增量备份文件
cp -r /var/lib/mysql/localhost-bin.000002 /data/
mv /data/localhost-bin.000002 /data/$(date +%Y-%m-%d)-localhost-bin.000002
ls /data/
2023-02-14-localhost-bin.000002  2023-02-14-school.sql

3.4. Data recovery

##删除school的所有表
drop table stuinfo;
##查看还有其他表格
show tables;
+------------------+
| Tables_in_school |
+------------------+
+------------------+
##退出数据库,恢复数据
quit;
mysql -uroot -p1234 < /data/2023-02-14-school.sql  ---先恢复完全备份的文件
mysqlbinlog /data/2023-02-14-localhost-bin.000002 | mysql -uroot -p1234  --恢复增量备份的部分。

3.5. Restore the data in the specified range

  • grammar:
mysqlbinlog  选项  binlog日志名称 | mysql -uroot -p密码

insert image description here

3.5.1. View binlog log files

[root@localhost ~]# mysqlbinlog /data/2023-02-14-localhost-bin.000002

It can be seen that this log format is not easy to understand

3.5.2. Modify the log record format

##查看当前日志记录格式
mysql> show variables like "binlog_format";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)
三种记录格式:
1.statement    报表模式
2.row          行模式
3.mixed        混合模式

##修改日志记录格式:
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
....
binlog_format=mixed
[root@localhost ~]# systemctl restart mysqld
###查看是否修改成功
mysql> show variables like "binlog_format";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.01 sec)

3.5.3. Restore specified range of data:

##往stuinfo表格中插入数据:
insert into stuinfo values(3,'grace','girl',20,'run,play basketball');
insert into stuinfo values(4,'andy','boy',23,'play basketball');
select * from stuinfo;
+----+-------+------+-----+---------------------+
| id | name  | sex  | age | likes               |
+----+-------+------+-----+---------------------+
| 1  | bob   | boy  | 25  | run,go fishing      |
| 2  | tom   | boy  | 28  | run,Play basketball |
| 3  | grace | girl | 20  | run,Play basketball |
| 4  | andy  | boy  | 23  | Play basketball     |
+----+-------+------+-----+---------------------+
##删除grace这个数据
delete from stuinfo where name='grace';
select * from stuinfo;
+----+------+-----+-----+---------------------+
| id | name | sex | age | likes               |
+----+------+-----+-----+---------------------+
| 1  | bob  | boy | 25  | run,go fishing      |
| 2  | tom  | boy | 28  | run,Play basketball |
| 4  | andy | boy | 23  | Play basketball     |
+----+------+-----+-----+---------------------+
##查看当前的binlog日志文件名称;
show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| localhost-bin.000006 | 1095     |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
### 恢复grace的数据信息
[root@localhost ~]# mysqlbinlog /var/lib/mysql/localhost-bin.000006   ---查看binlog日志内容
# at 302   ---起始pos值
#230214 17:27:39 server id 100  end_log_pos 449 CRC32 0x2dbf2580        Query   thread_id=3     exec_time=0     error_code=0
use `school`/*!*/;
SET TIMESTAMP=1676366859/*!*/;
insert into stuinfo values(3,'grace','girl',20,'run,play basketball')
/*!*/;
# at 449
#230214 17:27:39 server id 100  end_log_pos 480 CRC32 0x3116b5d3        Xid = 21
COMMIT/*!*/;      ---回车操作
# at 480          ---结束pos值
#230214 17:28:12 server id 100  end_log_pos 545 CRC32 0x0b918b41        Anonymous_GTID  last_committed=1        sequence_number=2 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 545
[root@localhost ~]# mysqlbinlog --start-position=302 --stop-position=480 /var/lib/mysql/localhost-bin.000006 | mysql -uroot -p1234
或者
[root@localhost ~]# mysqlbinlog --start-datetime="2023-02-14 17:27:39" --stop-datetime="2023-02-14 17:28:12"  /var/lib/mysql/localhost-bin.000006 | mysql -uroot -p1234
###查看是否恢复:
select * from stuinfo;
+----+-------+------+-----+---------------------+
| id | name  | sex  | age | likes               |
+----+-------+------+-----+---------------------+
| 1  | bob   | boy  | 25  | run,go fishing      |
| 2  | tom   | boy  | 28  | run,Play basketball |
| 3  | grace | girl | 20  | run,Play basketball |
| 4  | andy  | boy  | 23  | Play basketball     |
+----+-------+------+-----+---------------------+

Guess you like

Origin blog.csdn.net/weixin_45625174/article/details/121159207