MySQL full and incremental backups

MySQL full backup and recovery

  With the continuous expansion of office automation and e-commerce, enterprise information systems for the dependence of more and more important, and the database as a very important role in the information system. In particular, some of the database requires very high reliability industries such as banking, securities, telecommunications, etc., if unplanned downtime or data loss occurs, the loss is very important. This database administrators must be customized for specific business requirements detailed database backup and disaster recovery strategies, and rigorous testing of every possible situation by simulating a failure. And protect the reliability of the data.

The importance of data backup

    The main purpose of disaster recovery backup, the backup application can also test the rollback data modification, query historical data, and auditing. We will produce the operation and maintenance of perspectives on classification and methods of backup and recovery.

    In the critical value of enterprise data, data protection of the business of running the business, and therefore safety and reliability of operation and maintenance of data is a top priority, any loss of data are likely to have serious consequences for enterprises . Causes of data loss are as follows:

  ● program error

  ● human error

  ● operation failed

  ● disk failure

  ● disaster (fire, earthquake) and theft

Second, the type of database backup

Physical and logical point of view: backup can be divided into physical and logical backup backup

Physical backup: backup physical file system, database operations (such as data files, log files, etc.). It can be divided into physical backup offline backup (cold backup ) and online backup (hot standby) . This type of backup is important for large databases need to quickly recover when the problem occurred.

1, cold backup: the database is shut down when done

2, hot backup: database is running, this method relies on the backup database log file

MySQL提供了自带工具mysqlhotcopy 实现了MyISAM引擎的热备份。
percona公司的Xtrabackup工具实现了对InnoDB表的热备份。

3, warm standby: locking the database tables (read but not write) is performed in a state

flush tables with read lock;

Logical backup: backup database logic components (e.g., database objects such as tables) is expressed as a logical structure of the database (create database, create table statement, etc.) and content information (insert statement or separate text file). This type of backup to apply a small amount of data can be edited or a table structure of data values, or to recreate the data on a different machine architecture.

From the point of view of the database backup strategy: Backup can be divided into a full backup, differential backup and incremental backup

Full backup: every time a complete backup of the data, namely backup, database backup file structure and the structure of the entire database, saving the backup completion time of the database is the basis for the differential backup and incremental backup.

Advantages: simple backup and recovery operations easy

Disadvantage: there are a lot of repeated data; take up a lot of space; long backup and recovery time

Differential backup: backup that since the last full backup of all files that have been modified, the start time of the backup is a backup since the last full backup data volume is growing. When restoring data, just restore the last full backup and the most recent differential backup.

Incremental backup: only those files that were modified since the last full backup or incremental backup after will be backed up. Over time of the last full backup or incremental backups for point in time, back up only the data changes between this and therefore small amount of data backed up, small footprint, fast backup speed. But when recovery is required from the last full backup has played the last incremental backup in order to restore, such as a particular intermediate backup data corruption will result in loss of data.

Logical backup and recovery Logical

The biggest advantage logical backup and recovery logic that, for different table storage engine, may be employed 同样的逻辑备份方法to produce a copy of the file. Adopted 同样的恢复方法to restore the data to a correct state. If you have multiple database storage engines, data backup and recovery logical choice would be simpler. Logical backup commonly used select ... into outfile , and mysqldump tool. The former produced a copy of the file to the specified text file format, which produces a copy of the file can be either a specified text file format can also be a SQL script file. Logical recovery tools are commonly used load data in file as well  mysqlimport.

Use of select ...into outfilebackup data

Syntax is as follows: the SELECT  语句 INTO outfile  文本文件 text file options and parameters are as follows:

fields terminated by '字符串' : 字符串分割符,默认是制表符'\t'
fileds escaped by ’字符‘ : 转义字符,默认是’\‘
fileds [optionally] enclose by 字符’‘: 字段引用符,负责向字段值两端加上字段引用符。如果使用optionally 选项,则表示字符串类型上添加字段分隔符。
lines starting by '字符串',每条记录前添加该字段。
lines terminated by ’字符串‘,每条记录后添加该字符串,默认是换行符 ‘\n’

E.g:

mysql> select * from classes into outfile 'data_back' fields terminated by '|';

The results generated in the data directory, the corresponding directory database. You can directly use the cat command.

Recover table data:

1, using the load data infile ... quickly read data from a text file in a specified format to a database table.

Use mysqldump database backup

MySQL database dump mysqldump also comes with commonly used tools, mysqldump produce a copy of the file, there are two scenarios:

1, the specified file is a text file format

2, is a SQL script file can be executed

Use the following three kinds:

1, back up multiple databases specified.

 mysqldump -u root -p --databases choose test > roverliang.sql

2, back up all databases

mysqldump -u root -p --all-databases choose test > roverliang.sql

3, some backup in the specified database tables

mysqldump -u root -p  choose test > roverliang.sql

Mysqldump is complete parameter:

mysqldump -u USER_NAME -p PASSWORD [其他选项] DB [其他数据库库表]

--default-character-set: Character Set

--single-transaction : Export set Affairs

--no-data : SQL script exported, will contain only create statement creates the table.

--add-drop-table : Script exported, contain drop table if exists

--routinesExporting stored procedures and functions

--eventsExport Events

--triggersExport Triggers

Importing data files mysqldump

一句命令就能搞定,在mysql终端下执行:
命令格式:
source FILE_PATH
source ~/demo.sql 

MySQL physical backup and hot backup

This section describes two hot backup tool: mysqlhotcopy with  Xtrabackup;

Wherein mysql mysqlhotcopy is carrying hot backup tool, but can only MyISAM engine data backup, the backup data can be Xtrabackup InnoDB MyISAM engine and the engine, complicated operation.

MySQL database storage engines of different tables, corresponding to the file type is not the same. MyISAM storage engine to an example, each table has three documents will have, respectively, a table structure definition file (FRM), a file index table (MYI), the table data file (MYD). So when a backup requires only a physical copy the three files will be able to complete the backup. The InnoDB engine in order to maintain security affairs, will have much more complex. If you read the text before the log, then it is clear there will be frm each InnoDB table definition files, InnoDB data and indexes are in a file inside called table spaces file (file sharing table space, exclusive table space). Redo log (redo) recorded in ib logfile0 and ib logfile1 in. Rolling log (Use the undo) is recorded in the file table space, the shared space table file (ibdata1) or exclusive tablespace files (IBD) in. About InnoDB MySQL transaction logs details to view the system log. So making a backup of InnoDB table engine table, several files mentioned above should be backed up to.

Have previously mentioned, cold, hot, warm backup is whether nature can continue to provide services, depending on the level of service provided, only the cold, hot, warm points.

 

    Common MySQL database backup methods

1. Physical cold backup
2. dedicated backup tools
   the mysqldump, mysqlhotcopy works
3. Incremental backup binary log
4. third-party tool backup
   Percona XtraBackup

 

Physical cold backup and recovery:

Physical cold backup is generally packaged with tar command directly to the database folder (data directory), and the need to stop before the backup library.

Backup database; create a / backup directory as the backup database path, create a backup file using tar. Backup of the entire database folders full backup
[root @ localhost ~] # systemctl STOP mysqld.service
[root @ localhost ~] # mkdir / Backup
[root @ localhost ~] # tar ZCF / Backup / mysql_all - $ (DATE% F + ) .tar.gz / usr / local / MySQL / Data /
[the root @ localhost ~] -l # LS / Backup /
total amount of 736
-rw-R & lt - r--. 1 751 542 the root the root. 8 dated 15 08:40 mysql_all -2018-08-15.tar.gz

restore the database; perform the following operations database file / usr / local / mysql / data / bak transferred to the next directory, a simulated fault.
[the root @ localhost ~] # mkdir / BAK
[the root @ localhost ~] # Music Videos / usr / local / MySQL / Data / / BAK /
perform the following operations to recover data from a backup file
[the root @ localhost ~] # mkdir Restore /
[ root @ localhost ~] # tar zxf /backup/mysql_all-2018-08-15.tar.gz -C restore /
[root@localhost ~]# mv restore/usr/local/mysql/data/ /usr/local/mysql/
[root@localhost ~]# systemctl start mysqld.service

1, direct packaging database folder, the location of the source package / usr / local / mysql / data /, the position of the package rpm / var / lib / mariadb /

Example:

[root@localhost ~]# /usr/local/mysql/bin/mysqld.sh start

Starting MySQL............... SUCCESS!

[root@localhost ~]# netstat -lnpt | grep :3306

tcp6       0      0 :::3306                 :::*                    LISTEN      1630/mysqld        

[root@localhost ~]# mysql -u root -p123456

mysql> create database auth;

Query OK, 1 row affected (0.00 sec)

 

mysql> use auth;

Database changed

mysql> create table user(name char(10) not null,ID int(48));

Query OK, 0 rows affected (0.04 sec)

 

mysql> insert into user values('crushlinux','123');

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from user;

+------------+------+

| name       | ID   |

+------------+------+

| crushlinux |  123 |

+------------+------+

1 row in set (0.00 sec)

 

mysql> exit

Bye

 

[root@localhost ~]# /usr/local/mysql/bin/mysqld.sh stop

Shutting down MySQL.. SUCCESS!

 

[Root @ localhost ~] # yum -y install xz # compress better overall performance

[root@localhost ~]# mkdir backup

[root@localhost ~]# tar Jcf backup/mysql_all-$(date +%F).tar.xz /usr/local/mysql/data/

tar: Removing leading `/' from member names

[root@localhost ~]# ls -l backup/

The total amount of 728

-rw-r--r-- 1 root root 742476 12月 14 23:31 mysql_all-2018-12-14.tar.xz

Analog data loss:

[root@localhost ~]# mkdir bak

[root@localhost ~]# mv /usr/local/mysql/data/* bak/

Data recovery:

[root@localhost ~]# mkdir restore

[root@localhost ~]# tar xf backup/mysql_all-2018-12-14.tar.xz -C restore/

[root@localhost ~]# mv restore/usr/local/mysql/data/* /usr/local/mysql/data/

[root@localhost ~]# /usr/local/mysql/bin/mysqld.sh start

Starting MySQL......... SUCCESS!

[root@localhost ~]# mysql -u root -p123456

mysql> select * from auth.user;

+------------+------+

| name       | ID   |

+------------+------+

| crushlinux |  123 |

+------------+------+

1 row in set (0.00 sec)



Complete backup and recovery

Using a dedicated backup tools mysqldump

         MySQL built-in backup tool mysqldump, you can easily make a backup of MySQL. Database, data table or all of the libraries can be exported through the command tool for SQL scripts, easy to use the command on a different version of the MySQL server. For example, when the need to upgrade the MySQL server, you can use the mysqldump command of the original library information to export and then import it directly into the upgraded MySQL server.

(1) Make a full backup of a single library

Format: mysqldump -u username -p [password] [options] --databases [database name]> / backup path / backup file name

Example:

[root@localhost ~]# mysqldump -uroot -p123456 --databases auth > backup/auth-$(date +%Y%m%d).sql

mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@localhost ~]# cat backup/auth-20181214.sql

(2) Make a full backup of multiple libraries

Format: mysqldump -u username -p [password] [options] --databases library name 1 [library name 2] ...> / backup path / backup file name

Example:

[root@localhost ~]# mysqldump -uroot -p123456 --events --databases mysql auth > backup/mysql+auth-$(date +%Y%m%d).sql

[root@localhost ~]# cat backup/mysql+auth-20181214.sql

(3) a complete backup of all libraries

Format: mysqldump -u username -p [password] [options] --opt --all-databases> / backup path / backup file name

Example:

[root@localhost ~]# mysqldump -uroot -p123456 --events --opt --all-databases > backup/mysql_all.$(date +%Y%m%d).sql

[root@localhost ~]# cat backup/mysql_all.20181214.sql

// - opt faster backups, using a backup when the amount of data

[root@localhost ~]# cat backup/mysql_all.20160505.sql

(4) section of the library table full backup

Format: mysqldump -u username -p [password] [options] Lists database name> / backup path / backup file name

Example:

[root@localhost ~]# mysqldump -uroot -p123456 auth user > backup/auth_user-$(date +%Y%m%d).sql

[root@localhost ~]# cat backup/auth_user-20181214.sql

(5) to the backup table structure

Format: mysqldump -u username -p [password] -d Lists database name> / backup path / backup file name

Example:

[root@localhost ~]# mysqldump -uroot -p123456 -d mysql user > backup/desc_mysql_user-$(date +%Y%m%d).sql

[root@localhost ~]# cat backup/desc_mysql_user-20181214.sql

After using mysqldump backup, restore the database

1, source command

Log in to the MySQL database, execute the backup source sql script path

Example:

[root@localhost ~]# mysql -uroot -p123456

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| auth               |

| mysql              |

| performance_schema |

| test               |

| usr                |

+--------------------+

6 rows in set (0.00 sec)

 

mysql> drop database auth;

Query OK, 1 row affected (0.12 sec)

 

mysql> source backup/mysql_all.20181214.sql

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| auth               |

| mysql              |

| performance_schema |

| test               |

| usr                |

+--------------------+

6 rows in set (0.00 sec)

 

2, mysql command

Path mysql -u user name -p [password] <backup script: Format

           mysql -u user name -p [password] library name <path table backup script

Example:

[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'

+--------------------+

| Database           |

+--------------------+

| information_schema |

| auth               |

| mysql              |

| performance_schema |

| test               |

| usr                |

+--------------------+

[root@localhost ~]# mysql -uroot -p123456 -e 'drop database auth;'

[root@localhost ~]# mysql -uroot -p123456 < backup/mysql_all.20181214.sql

[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'

+--------------------+

| Database           |

+--------------------+

| information_schema |

| auth               |

| mysql              |

| performance_schema |

| test               |

| usr                |

+--------------------+

 

[root@localhost ~]# mysql -uroot -p123456 -e 'drop table auth.user;'

[root@localhost ~]# mysql -uroot -p123456 auth< backup/auth_user-20181214.sql

[root@localhost ~]# mysql -uroot -p123456 -e 'select * from auth.user;'

+------------+------+

| name       | ID   |

+------------+------+

| crushlinux |  123 |

+------------+------+

Incremental Backup

Features:
with full backups, incremental backups there is no duplication of data, not the amount of backup time is short, but recovered in trouble, you need the last incremental backup after a full backup and a full backup to restore, but also for all incremental backups one by one thrust reverser recovery, mysql does not provide a direct method for incremental backups may be provided by the MySQL binary log I (binary logs) indirectly achieve incremental backup.
Binary log:
To make incremental backups of MySQL, we must first open a binary function, open the implementation of MySQL binary logging there are many, the most common is to add a "log-bin = / file in the mysqld MySQL configuration file entries path / filename "prefix, such as; log-bin = / usr / local / mysql / mysql-bin, then restart mysqld.service service, you can see the binary files in the specified path. By default, the binary log file name extension is a six-digit numbers, such as; mysql-bin.000001

[root @ localhost ~] # vim /etc/my.cnf
## to modify the configuration file, enable binary function

ID. 1 =-Server
log-bin = / usr / local / MySQL / MySQL-bin
:! WQ
[the root @ localhost ~] # systemctl the restart mysqld
[the root @ localhost ~] -l # LS / usr / local / MySQL / MySQL -bin. *
-rw-RW. 1 ---- 120 MySQL MySQL /usr/local/mysql/mysql-bin.000001 10:35 on August 15
-rw-RW 34 is MySQL MySQL. 1 ---- 15 August 10 : 35 /usr/local/mysql/mysql-bin.index
the MySQL incremental recovery:
conventional incremental recovery in three ways:
a general recovery, resume position based on the point in time recovery

General Recovery:
the recovery of all the binary log contents of all backups, the format command is as follows

mysqlbinlog [-no-defaults] incremental backup files | mysql -u root -p

Location-based recovery:
database administrator when the database operation error may have both operating in the same point in time there are proper operation, through recovery based on the location can be more precise,
as shown in the following format command.
Formats: recover the data to the specified location

mysqlbinlog --stop-position = '459' binary log | mysql -u root -p ******

Format II: began to recover data from a specified location

mysqlbinlog --start-position = '459' binary log | mysql -u root -p ******

Time-based recovery points:
skip an error point in time data recovery, and recovery point based on the time it can be divided into three cases.
Format 1: Up to a certain point in time to recover from the beginning of the log

mysqlbinlog [-no-defaults] --stop-datetime = 'year - month - day hours: minutes: seconds' binary log | mysql -u user -p password

Format 2: Recovery from a point in time to the end of the log

mysqlbinlog [-no-defaults] --start-datetime = 'year - month - day hours: minutes: seconds' binary log | mysql -u user -p password

Format 3: to a certain point in time to recover from a point in time

mysqlbinlog [-no-defaults] --start-datetime = 'year - month - day hours: minutes: seconds' --stop-datetime = 'year - month - day hours: minutes: seconds' binary log | mysql -u user -p password

 

Original articles published 0 · won praise 27 · views 80000 +

Guess you like

Origin blog.csdn.net/yimenglin/article/details/104774738
Recommended