mysql backup and recovery (mysqldump)

Backup type

  • Full backup: all the current state of the database is backed up

Insert picture description here

  • Incremental backup: only the increased part of the state is backed up.
    Insert picture description here

  • Differential backup: After a full backup, the data that should be backed up so far
    Insert picture description here

Backup method:

  • Cold standby: Suspend service, read and write operations cannot be performed
  • Warm preparation: read operation is executable, write operation is not
  • Hot standby: read and write operations can be performed

Backup tool

mysqldump is a backup tool that connects to the mysql server through the mysql protocol and backs up the state obtained after querying each database with the select statement.

mysqldump syntax

  • mysqldump [OPTIONS] database [tables] : Back up the database or table
  • mysqldump [OPTIONS] –B DB1 [DB2 DB3...]: Back up multiple databases
  • mysqldump [OPTIONS] –A [OPTIONS]: Back up all databases

Common options

  • -A --all-databases: Back up all databases, which contains create database

  • -B,--databases db_name: Specify the database to be backed up, including create database

  • -E , --events:Back up all event scheduler related

  • -R,--routines: Backup-related stored procedures and stored functions

  • --triggers: Triggers related to the backup table

  • --master-data[=#]

    • 1: Add a record before the backed up data as a CHANGE MASTER TO statement, non-comment, do not specify #, the default is 1
    • 2: CHANGE MASTER TO statement recorded as a comment.
    • This option will automatically turn off the –lock-tables function and automatically turn on the –lock-all-tables function
  • -F,--flush-logs: Back up the log forward. After the table is locked, execute the flush logs command to generate a new binary file. With -A, the database will be refreshed multiple times. Dump and log refresh will be executed at the same time. You should use --flush-logsand -x,--master-dataor at the same time -single-transaction. Refresh only once

  • --compact: Remove the comment, suitable for debugging, not suitable for production

  • -d,--no-data: Only backup table structure

  • -t,--no-create-info:Only backup data, not create table

  • -n, --no-create-db:Create database is not backed up, it can be overwritten by -A or -B

  • --flush-privileges: Need to use when backing up mysql or related permissions

  • -f, --force: Ignore the SQL error and continue execution

  • -q, --quick: Do not cache queries, output directly, speed up backup

Backup and restore example

Here I used a version of mariadb that has not been installed. Start over.
(1) Run mysql_secure_installation
(2) Create a directory for storing binary logs and backup files

[root@localhost ~]# mkdir /data/mysql/binlog -pv

[root@localhost ~]# mkdir /data/mysql/backup

[root@localhost ~]# chown -R mysql.mysql /data

(3) Configure my.cnf

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=/data/mysql/binlog/master  # 开启二进制日志
innodb-file-per-table=ON     #数据库数据分开存放
skip-name-resolve=ON       #跳过反向解析
autocommit=0              #取消自动提交事务

(4) Import the database hellodb

[root@localhost ~]# mysql -uroot -pydong < hellodb_innodb.sql 

(5) Full backup database status

[root@localhost ~]# mysqldump -uroot -pydong -A -F --single-transaction --flush-privileges --master-data=1 > /data/mysql/backup/fullback_`date +%F`.sql

[root@localhost ~]# ls /data/mysql/backup/
fullback_2020-03-01.sql

(6) Delete all directories under /var/lib/mysql/

[root@localhost ~]# rm -rf /var/lib/mysql/*

(7) Performing the restore When
restoring, we need to pay attention. We should stop the service when restoring, or prohibit all users from connecting in, in order to prevent data errors. Keep a backup on this machine, and there should be backups elsewhere

[root@localhost ~]# systemctl stop mariadb
[root@localhost ~]# mysql  < /data/mysql/backup/fullback_2020-03-01.sql 

(8) Check whether to restore

[root@localhost ~]# ls /var/lib/mysql
aria_log.00000001  hellodb  ib_logfile0  mariadb-relay-bin.000001  master.info  mysql.sock          relay-log.info
aria_log_control   ibdata1  ib_logfile1  mariadb-relay-bin.index   mysql        performance_schema  test

MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

Incremental backup

(1) First use flush logs to refresh the log file

MariaDB [hellodb]> flush logs;
MariaDB [hellodb]> show master logs;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| master.000001 |       264 |
| master.000002 |       264 |
| master.000003 |      7695 |
| master.000004 |       264 |
| master.000005 |     30760 |
| master.000006 |   1038814 |
| master.000007 |    520959 |
| master.000008 |       245 |   #以后所写入的数据通通回到08里面

(2) Perform some simple operations on the students table

MariaDB [hellodb]> INSERT students VALUES (27,'Guan yin' ,102,'F',3,4);
MariaDB [hellodb]> SELECT * FROM students WHERE Stuid>25; 
+-------+----------+-----+--------+---------+-----------+
| StuID | Name     | Age | Gender | ClassID | TeacherID |
+-------+----------+-----+--------+---------+-----------+
|    26 | Ru lai   | 101 | M      |       1 |         2 |
|    27 | Guan yin | 102 | F      |       3 |         4 |
+-------+----------+-----+--------+---------+-----------+
MariaDB [hellodb]> DELETE FROM students WHERE Stuid=26;

MariaDB [hellodb]> SELECT * FROM students WHERE Stuid>25; 
+-------+----------+-----+--------+---------+-----------+
| StuID | Name     | Age | Gender | ClassID | TeacherID |
+-------+----------+-----+--------+---------+-----------+
|    27 | Guan yin | 102 | F      |       3 |         4 |
+-------+----------+-----+--------+---------+-----------+

(3) Back up the operation just added.

[root@localhost ~]# mysqlbinlog /data/mysql/binlog/master.000008 > /data/mysql/backup/inc_`date +%F-%T`.sql

[root@localhost ~]# ll /data/mysql/backup
总用量 516
-rw-r--r-- 1 root root 520869 3月   1 15:44 fullback_2020-03-01.sql
-rw-r--r-- 1 root root   1980 3月   1 17:20 inc_2020-03-01-17:20:01.sql

(4) Delete all files under /var/lib/mysql

[root@localhost ~]# rm -rf /var/lib/mysql/*

(5) First, restore the full backup

[root@localhost ~]# mysql < /data/mysql/backup/fullback_2020-03-01.sql 
MariaDB [hellodb]> SELECT * FROM students ORDER BY Stuid  DESC LIMIT 1; 
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng | 100 | M      |    NULL |      NULL |
+-------+-------------+-----+--------+---------+-----------+
#只到25号的学生。

(6) Increase the restoration of binary logs

[root@localhost ~]# mysql -uroot -pydong < /data/mysql/backup/inc_2020-03-01-17\:20\:01.sql

MariaDB [hellodb]> SELECT * FROM students WHERE Stuid >= 25;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng | 100 | M      |    NULL |      NULL |
|    27 | Guan yin    | 102 | F      |       3 |         4 |
+-------+-------------+-----+--------+---------+-----------+
 

PS

  • When doing restoration, you must let the database deny everyone access. So as to avoid conflict
  • The database automatically installed by yum is mysql.socketplaced /var/lib/mysqlunder. Therefore, if all the files under /var/lib/mysql disappear, you need to restart the database and let the service automatically generate the socket file.
  • When doing incremental backups, you must refresh the log. If you do not refresh the log, you have to remember which node is incremental from.
  • Clean up the binary log regularly.
  • The backup file must be put in the host computer, and put another copy elsewhere
  • mysqldump is suitable for scenarios where the database is not large. If the amount of data is large, you can combine the -q option to increase the backup speed.

Guess you like

Origin blog.csdn.net/qq_44564366/article/details/104579152