MySQL-- full amount, incremental backup and recovery (theory section)

The importance of data backup

在生产环境中,数据的安全性是至关重要的,任何数据的丢失都可能产生严重的后果
造成数据丢失的原因:
    程序错误
    人为错误
    计算机失败
    磁盘失败
    灾难和偷窃

Classification database backup

Physical backup:

对数据库操作系统的物理文件(如数据文件,日志文件等)的备份

Physical backup is divided into an offline backup (cold backup) and online backup (hot standby)

冷备份:是在关闭数据库的时候进行的
热备份:数据库处于运行状态,这种备份方法依赖于数据库的日志文件

Logical backup:

对数据库逻辑组件(如表等数据库对象)的备份

From the point of view of the database backup strategy, backup can be divided into

完全备份:每次对数据进行完整的备份
差异备份:备份那些自从上次完全备份之后被修改过的文件
增量备份:只有那些在上次完全备份或者增量备份后被修改的文件才会被备份

MySQL full backup

完全备份是对整个数据库的备份,数据结构和文件结构的备份
完全备份保存的是备份完成时刻的数据库
完全备份是增量备份的基础

The advantages of a full backup

备份与恢复操作简单方便

The shortcomings of a full backup

数据存在大量的重复
占用大量的备份空间
备份与恢复时间长

mysqldump backup library

MySQL database backup can be used in various ways

直接打包数据库文件夹,如/usr/local/mysql/data
使用专用备份工具mysqldump

mysqldump command

MySQL自带的备份工具,相当方便对MySQL进行备份
通过该命令工具可以将指定的库,表和全部的库到处为SQL脚本,在需要恢复时可进行数据恢复

mysqldump command to make a full backup of a single library

mysqldump -u 用户名 -p [密码] [选项] [数据库名] > /备份路径/备份文件名

Single database backup examples

mysqldump -u root -p auth > /backup/auth.sql
mysqldump -u root -p mysql > /backup/mysql.sql

mysqldump command to make a full backup of multiple libraries

mysqldump -u 用户名 -p [密码] [选项] --database 库名1 [库名2]... > /备份路径/备份文件名

Examples of multi-database backup

mysqldump -u root -p --databases auth mysql > /back/databases-auth-mysql.sql

Perform a full backup of all libraries

mysqldump -u 用户名 -p [密码] [选项] --all-databases > /备份路径/备份文件名

There are examples of multi-backup library

mysqldump -u root -p --opt --all-databases > /backup/all-data.sql 

mysqldump backup table

In time production environment, there is a maintenance operation on a specific table, this time mysqldump also play a major role

Using a backup table operation mysqldump

mysqldump -u 用户名 -p [密码] [选项] 数据库名 表名 > /备份路径/备份文件名

Examples backup table

mysqldump -u root -p mysql user > /backup/mysql-user.sql

Database recovery

Use mysqldump backup command to export the SQL script, during the data recovery method can be used to import about

source命令
mysql命令

To use the source database recovery

登录到MySQL数据库
执行source备份sql脚本的路径

Examples of source recovery

MySQL [(none)]> source /backup/all-data.sql

Use the mysql command to recover data

mysql -u 用户名 -p [密码] < 库备份脚本的路径

mysql command to restore example

mysql -u root -p < /backup/all-data.sql

Recovery operation table

You can use the same source or mysql command when restoring the table

source operation and a recovery operation of the recovery table is the same library

When backup file contains only the backup table, not including the statement creation library, library name must be developed, and the target database must exist

mysql -u 用户名 -p [密码] < 表备份脚本的路径
mysql -u root -p mysql < /backup/mysql-user.sql

In a production environment, you can use shell scripts to automate regular backups

MySQL backup ideas

Backup a regular basis, develop a backup plan or strategy, and strict compliance with

Except when a full backup, open the MySQL server log function is very important

完全备份加上日志,可以对MySQL进行最大化的还原

Use the same and understandable backup file name

推荐使用库名或者表名加上时间的命名规则

MySQL incremental backup

Use mysqldump perform a full backup of the existing problems

备份数据中有重复数据
备份时间与恢复时间长

Incremental backup is added after the last backup or a backup from a file or content change

Incremental Backup features:

没有重复数据,备份量不大,时间短
恢复麻烦:需要上次完全备份及完全备份之后所有的增量备份才能恢复,而且要对所有增量备份进行逐个反推恢复

MySQL does not provide direct incremental backup method

Incremental backups can be achieved indirectly through the binary log (binary logs) MySQL provided

MySQL binary log for backups significance:

 Save the binary log all updates or operation of the database may be updated
 binary log recording starts after the start the MySQL server, and the file reaches the set size max_ binlog_size received flush logs or re-create a new log file command
 just regular implementation flush logs method to re-create a new log file generated binary sequence and in a timely manner to save these old logs to a safe place to complete a period of incremental backups

MySQL database incremental recovery

General Recovery:

MySQL-- full amount, incremental backup and recovery (theory section)

Location-based recovery:

就是将某个起始时间的二进制日志导入数据库中,从而跳过某个发生错误的时间点实现数据的恢复

Based on the point in time recovery

使用基于时间点的恢复,可能会出现在一个时间点里既同时存在正确的操作又存在错误的操作,所以我们需要一种更为精确的恢复方式

Incremental recovery

General Recovery:

mysqlbinlog [--no-defaults]增量备份文件 | mysql -u用户名 -p

Location-based recovery:

Restore the data to the specified location

mysqlbinlog --stop-position=操作'id' 1进制日志 | mysql -u用户名 -p 密码

Began to recover data from the specified location

mysqlbinlog --start-position=操作'id'二进制日志 | mysql -u用户名 -p 密码

Based on the time point of recovery:

As of the beginning of the log to a point in time recovery

mysqlbinlog [--no-defaults] --stop-datetime='年-月-日 小时:分钟:秒'二进制日志 | mysql -u用户名 -p 密码

Recovering from a point in time to the end of the log

mysqlbinlog [--no defaults] --start-datetime='年-月-日 小时:分钟:秒'二进制日志 | mysql -u用户名 -p 密码

From the point in time recovery to a point in time

mysqlbinlog [--no defaults] --start-datetime='年-月-日 小时:分钟:秒' --stop-datetime='年-月-日 小时:分钟:秒'二进制日志 | mysql -u用户名 -p 密码

To view binary log file (decoding)

mysqlbinlog --no-defaults --base64-output=decode-rows -V mysql-bin.000002 > /opt/ bak. txt

thanks for reading! !

Guess you like

Origin blog.51cto.com/14080162/2453854